From: Jeff E. <je...@ad...> - 2002-03-22 16:44:17
|
Ashish, The default list cell renderer renders the objects in the list with the result of toString. Instances of Python classes do not return the result of __repr__ or __str__ for the Java toString method. You could write a replacement cell renderer that calls the __str__ method for PyInstances. Perhaps something like in Jython: class PyInstanceListCellRenderer(DefaultListCellRenderer): def getListCellRendererComponent(self,list, \ value, index, selected, focus): return DefaultListCellRenderer.getListCellRendererComponent(self,\ list,`value`,index,selected,focus) myList = JList(cellRenderer=PyInstanceListCellRenderer()) Jeff Emanuel BillWorker 2i wrote: > Okay, I just have modified your code to include a few more classes. If you > run the script you will see that only for PyClass5, the one that inherits > from java.lang.Object, the JList shows a proper string in the list. > > Ashish > > ---------- code --------- > import java > import javax.swing as swing > > class PyClass1: > pass > > class PyClass2: > def __repr__(self): > return 'PyClass2' > > class PyClass3: > def __str__(self): > return 'PyClass3' > > class PyClass4: > def toString(self): > return 'PyClass4' > > class PyClass5(java.lang.Object): > def toString(self): > return 'PyClass5' > > > class TestModel(swing.AbstractListModel): > def __init__(self): > self.classList = [PyClass1(), PyClass2(), PyClass3(), PyClass4(), > PyClass5()] > > def getElementAt(self, i): > return self.classList[i] > > def getSize(self): > return len(self.classList) > > > frame = swing.JFrame('test') > jlist = swing.JList(TestModel()) > frame.contentPane.add(jlist) > frame.pack() > frame.visible = 1 > > > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |