How to put my own type into the table?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to put my own type into the table?

Piotr Kowalczyk

I found answer to that question in java, but I do not know how to make it work in jython. Can somone help to translate it for my? https://sites.google.com/site/drjohnbmatthews/tableI tried myself and ...
from javax.swing importJFrame,JPanel,JScrollPane,JTable,JButton,JCheckBox,AbstractCellEditorfrom javax.swing.table importAbstractTableModel,TableCellEditor,TableCellRendererfrom java.awt importGridLayout,Dimension from java.lang importString,Integer,Boolean,Classfrom java.util importArrayList,ListclassTableSortDemo: def __init__(self): frame =JFrame("TableSortDemo") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)#Create and set up the content pane. self.table =JTable(MyTableModel()) self.table.setPreferredScrollableViewportSize(Dimension(500,170)) self.table.setFillsViewportHeight(True) self.table.setDefaultRenderer(Class.forName("java.lang.Object"),ValueRenderer()); self.table.setDefaultEditor(Class.forName("java.lang.Object"),ValueEditor()); self.table.setAutoCreateRowSorter(True)#Create the scroll pane and add the table to it. scrollPane =JScrollPane(self.table);#Add the scroll pane to this panel. newContentPane =JPanel() newContentPane.add(scrollPane); newContentPane.setOpaque(True)#content panes must be opaque frame.setContentPane(newContentPane)#Display the window. frame.pack(); frame.setVisible(True);classValue:#implementsComparable<Value>{ def __init__(self,selected, value): self.selected = selected; self.value = value; def compareTo(self,v):return self.value.compareTo(v.value); def equals(self,v):returnTrue# TODO v instanceofValue&&this.value.equals(((Value) v).value); def hashCode(self):return self.value.hashCode();classMyTableModel(AbstractTableModel): MAX =8; names =["Number","Value"]; values =ArrayList();for i in range(MAX): values.add(Value(False,(i +1)*1.1)); def getRowCount(self):return self.MAX; def getColumnCount(self):return len(self.names) def getColumnName(self, col):return self.names[col]; def getValueAt(self,row,col):if col ==0:return row +1; elif col ==1:return values.get(row);else:returnnull; def getColumnClass(self,col):if col ==0:returnInteger elif col ==1:returnValueelse:returnnull;#Don't need to implement this method unless your table's editable. def isCellEditable(self,row, col):return col ==1; def setValueAt(self, aValue, row, col):if col ==1: values.get(row).selected = aValue; self.fireTableCellUpdated(row, col);classValueRenderer(TableCellRenderer): def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, col): v = value; chkbox =JCheckBox(df.format(v.value)) chkbox.setSelected(v.selected);if isSelected: setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground());else: setForeground(table.getForeground()); setBackground(table.getBackground());return chkbox;classValueEditor(TableCellEditor):#AbstractCellEditor vr =ValueRenderer(); def ValueEditor(): vr.addItemListener(self); def getCellEditorValue():return vr.isSelected(); def getTableCellEditorComponent(table,value,isSelected, row, col): v = value; vr.setSelected(v.selected); vr.setText(df.format(v.value));return vr; def itemStateChanged(e): self.fireEditingStopped();if __name__ =='__main__':TableSortDemo()
I get a window with empty table and:
Exception in thread "AWT-EventQueue-0"TypeError: can't convert <class __main__.Value at 0x2> tojava.lang.ClassThere is no errors is stack trace. I am runing program redirecting stack trace to SikuliLog.txt file (You can see it in img https://www.dropbox.com/s/8t8jn8e78yvqh1w/exception.png?dl=0 , first line in command window). Exception appears in command window anyway.
Regards Piotr

Wiadomość została wysłana przy pomocy AquaMail dla systemu Android
http://www.aqua-mail.com


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: How to put my own type into the table?

Stefan Richthofer
Hello Piotr,
 
I don't come with a solution, but have some remarks to your code, each of which might be the cause of trouble.
 
- class Value is supposed to implement "comparable", so you should have it extend that interface:
class Value(Comparable)
 
- you sometimes write "return null", but in Python this should be "return None"
 
- in class ValueEditor it appears that #AbstractCellEditor vr = ValueRenderer();
is out-commented. But the methods access vr. How shall this work?
 
- You frequently declare members in a class. Are you aware that this is the equivalent of static members in Java (i.e. class members)?
E.g. in the Java example vr is an object-member in class ValueEditor, while in your code it's a class member. To make it an object member,
you must implement __init__ and call "self.vr = ValueRenderer()"
I suggest you to look at the other cases and check whether you intend members to be class members or not.
 
Hope, some of this helps! For future questions, please take more care of formatting your code properly or attach it as a file rather than letting the mail program make a mess of it!
And the link https://sites.google.com/site/drjohnbmatthews/tableI does not work. I had to figure out the correct link, which is https://sites.google.com/site/drjohnbmatthews/table.
 
- Stefan
 
 
Gesendet: Mittwoch, 03. September 2014 um 15:41 Uhr
Von: "Piotr Kowalczyk" <[hidden email]>
An: [hidden email]
Betreff: [Jython-users] How to put my own type into the table?

I found answer to that question in java, but I do not know how to make it work in jython. Can somone help to translate it for my? https://sites.google.com/site/drjohnbmatthews/tableI tried myself and ...
from javax.swing importJFrame,JPanel,JScrollPane,JTable,JButton,JCheckBox,AbstractCellEditorfrom javax.swing.table importAbstractTableModel,TableCellEditor,TableCellRendererfrom java.awt importGridLayout,Dimension from java.lang importString,Integer,Boolean,Classfrom java.util importArrayList,ListclassTableSortDemo: def __init__(self): frame =JFrame("TableSortDemo") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)#Create and set up the content pane. self.table =JTable(MyTableModel()) self.table.setPreferredScrollableViewportSize(Dimension(500,170)) self.table.setFillsViewportHeight(True) self.table.setDefaultRenderer(Class.forName("java.lang.Object"),ValueRenderer()); self.table.setDefaultEditor(Class.forName("java.lang.Object"),ValueEditor()); self.table.setAutoCreateRowSorter(True)#Create the scroll pane and add the table to it. scrollPane =JScrollPane(self.table);#Add the scroll pane to this panel. newContentPane =JPanel() newContentPane.add(scrollPane); newContentPane.setOpaque(True)#content panes must be opaque frame.setContentPane(newContentPane)#Display the window. frame.pack(); frame.setVisible(True);classValue:#implementsComparable&lt;Value&gt;{ def __init__(self,selected, value): self.selected = selected; self.value = value; def compareTo(self,v):return self.value.compareTo(v.value); def equals(self,v):returnTrue# TODO v instanceofValue&amp;&amp;this.value.equals(((Value) v).value); def hashCode(self):return self.value.hashCode();classMyTableModel(AbstractTableModel): MAX =8; names =["Number","Value"]; values =ArrayList();for i in range(MAX): values.add(Value(False,(i +1)*1.1)); def getRowCount(self):return self.MAX; def getColumnCount(self):return len(self.names) def getColumnName(self, col):return self.names[col]; def getValueAt(self,row,col):if col ==0:return row +1; elif col ==1:return values.get(row);else:returnnull; def getColumnClass(self,col):if col ==0:returnInteger elif col ==1:returnValueelse:returnnull;#Don't need to implement this method unless your table's editable. def isCellEditable(self,row, col):return col ==1; def setValueAt(self, aValue, row, col):if col ==1: values.get(row).selected = aValue; self.fireTableCellUpdated(row, col);classValueRenderer(TableCellRenderer): def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, col): v = value; chkbox =JCheckBox(df.format(v.value)) chkbox.setSelected(v.selected);if isSelected: setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground());else: setForeground(table.getForeground()); setBackground(table.getBackground());return chkbox;classValueEditor(TableCellEditor):#AbstractCellEditor vr =ValueRenderer(); def ValueEditor(): vr.addItemListener(self); def getCellEditorValue():return vr.isSelected(); def getTableCellEditorComponent(table,value,isSelected, row, col): v = value; vr.setSelected(v.selected); vr.setText(df.format(v.value));return vr; def itemStateChanged(e): self.fireEditingStopped();if __name__ =='__main__':TableSortDemo()
I get a window with empty table and:
Exception in thread "AWT-EventQueue-0"TypeError: can't convert &lt;class __main__.Value at 0x2&gt; tojava.lang.ClassThere is no errors is stack trace. I am runing program redirecting stack trace to SikuliLog.txt file (You can see it in img https://www.dropbox.com/s/8t8jn8e78yvqh1w/exception.png?dl=0 , first line in command window). Exception appears in command window anyway.
Regards Piotr
 

Wiadomość została wysłana przy pomocy AquaMail dla systemu Android
http://www.aqua-mail.com

------------------------------------------------------------------------------ Slashdot TV. Video for Nerds. Stuff that matters. http://tv.slashdot.org/_______________________________________________ Jython-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/jython-users

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: How to put my own type into the table?

Stefan Richthofer
Okay, I realized that the broken link comes from the generally broken new-line characters in you mail. My mail interface rendered the "I" into the link and tricked me.
#AbstractCellEditor vr = ValueRenderer();
is also a new-line issue and probably shall read
#AbstractCellEditor
vr = ValueRenderer();
 
However, I still think it should be an object member rather than a class member and I noticed that you declared the methods in "ValueEditor" without a "self" argument.
I suspect, this breaks the interface-implementation mechanism. While most methods might work as static methods, itemStateChanged won't work at all since you
access self, but don't declare it. Try it like this:
 
class ValueEditor(TableCellEditor):
    #AbstractCellEditor
    def __init__(self):
        self.vr = ValueRenderer();
 
    def ValueEditor(self):
        self.vr.addItemListener(self)
 
    def getCellEditorValue(self):
        return self.vr.isSelected()
 
    def getTableCellEditorComponent(self, table, value, isSelected,  row,  col):
        v = value;
        self.vr.setSelected(v.selected)
        self.vr.setText(df.format(v.value))
        return self.vr
 
    def itemStateChanged(self, e):
        self.fireEditingStopped()
 
And maybe you must fix addItemListener-call somehow. I guess your class would have to implement ItemListener for this to work.
 
- Stefan
 
Gesendet: Mittwoch, 03. September 2014 um 18:44 Uhr
Von: "Stefan Richthofer" <[hidden email]>
An: "Piotr Kowalczyk" <[hidden email]>
Cc: [hidden email]
Betreff: Re: [Jython-users] How to put my own type into the table?
Hello Piotr,
 
I don't come with a solution, but have some remarks to your code, each of which might be the cause of trouble.
 
- class Value is supposed to implement "comparable", so you should have it extend that interface:
class Value(Comparable)
 
- you sometimes write "return null", but in Python this should be "return None"
 
- in class ValueEditor it appears that #AbstractCellEditor vr = ValueRenderer();
is out-commented. But the methods access vr. How shall this work?
 
- You frequently declare members in a class. Are you aware that this is the equivalent of static members in Java (i.e. class members)?
E.g. in the Java example vr is an object-member in class ValueEditor, while in your code it's a class member. To make it an object member,
you must implement __init__ and call "self.vr = ValueRenderer()"
I suggest you to look at the other cases and check whether you intend members to be class members or not.
 
Hope, some of this helps! For future questions, please take more care of formatting your code properly or attach it as a file rather than letting the mail program make a mess of it!
And the link https://sites.google.com/site/drjohnbmatthews/tableI does not work. I had to figure out the correct link, which is https://sites.google.com/site/drjohnbmatthews/table.
 
- Stefan
 
 
Gesendet: Mittwoch, 03. September 2014 um 15:41 Uhr
Von: "Piotr Kowalczyk" <[hidden email]>
An: [hidden email]
Betreff: [Jython-users] How to put my own type into the table?

I found answer to that question in java, but I do not know how to make it work in jython. Can somone help to translate it for my? https://sites.google.com/site/drjohnbmatthews/tableI tried myself and ...
from javax.swing importJFrame,JPanel,JScrollPane,JTable,JButton,JCheckBox,AbstractCellEditorfrom javax.swing.table importAbstractTableModel,TableCellEditor,TableCellRendererfrom java.awt importGridLayout,Dimension from java.lang importString,Integer,Boolean,Classfrom java.util importArrayList,ListclassTableSortDemo: def __init__(self): frame =JFrame("TableSortDemo") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)#Create and set up the content pane. self.table =JTable(MyTableModel()) self.table.setPreferredScrollableViewportSize(Dimension(500,170)) self.table.setFillsViewportHeight(True) self.table.setDefaultRenderer(Class.forName("java.lang.Object"),ValueRenderer()); self.table.setDefaultEditor(Class.forName("java.lang.Object"),ValueEditor()); self.table.setAutoCreateRowSorter(True)#Create the scroll pane and add the table to it. scrollPane =JScrollPane(self.table);#Add the scroll pane to this panel. newContentPane =JPanel() newContentPane.add(scrollPane); newContentPane.setOpaque(True)#content panes must be opaque frame.setContentPane(newContentPane)#Display the window. frame.pack(); frame.setVisible(True);classValue:#implementsComparable&lt;Value&gt;{ def __init__(self,selected, value): self.selected = selected; self.value = value; def compareTo(self,v):return self.value.compareTo(v.value); def equals(self,v):returnTrue# TODO v instanceofValue&amp;&amp;this.value.equals(((Value) v).value); def hashCode(self):return self.value.hashCode();classMyTableModel(AbstractTableModel): MAX =8; names =["Number","Value"]; values =ArrayList();for i in range(MAX): values.add(Value(False,(i +1)*1.1)); def getRowCount(self):return self.MAX; def getColumnCount(self):return len(self.names) def getColumnName(self, col):return self.names[col]; def getValueAt(self,row,col):if col ==0:return row +1; elif col ==1:return values.get(row);else:returnnull; def getColumnClass(self,col):if col ==0:returnInteger elif col ==1:returnValueelse:returnnull;#Don't need to implement this method unless your table's editable. def isCellEditable(self,row, col):return col ==1; def setValueAt(self, aValue, row, col):if col ==1: values.get(row).selected = aValue; self.fireTableCellUpdated(row, col);classValueRenderer(TableCellRenderer): def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, col): v = value; chkbox =JCheckBox(df.format(v.value)) chkbox.setSelected(v.selected);if isSelected: setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground());else: setForeground(table.getForeground()); setBackground(table.getBackground());return chkbox;classValueEditor(TableCellEditor):#AbstractCellEditor vr =ValueRenderer(); def ValueEditor(): vr.addItemListener(self); def getCellEditorValue():return vr.isSelected(); def getTableCellEditorComponent(table,value,isSelected, row, col): v = value; vr.setSelected(v.selected); vr.setText(df.format(v.value));return vr; def itemStateChanged(e): self.fireEditingStopped();if __name__ =='__main__':TableSortDemo()
I get a window with empty table and:
Exception in thread "AWT-EventQueue-0"TypeError: can't convert &lt;class __main__.Value at 0x2&gt; tojava.lang.ClassThere is no errors is stack trace. I am runing program redirecting stack trace to SikuliLog.txt file (You can see it in img https://www.dropbox.com/s/8t8jn8e78yvqh1w/exception.png?dl=0 , first line in command window). Exception appears in command window anyway.
Regards Piotr
 

Wiadomość została wysłana przy pomocy AquaMail dla systemu Android
http://www.aqua-mail.com

------------------------------------------------------------------------------ Slashdot TV. Video for Nerds. Stuff that matters. http://tv.slashdot.org/_______________________________________________ Jython-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/jython-users
------------------------------------------------------------------------------ Slashdot TV. Video for Nerds. Stuff that matters. http://tv.slashdot.org/_______________________________________________ Jython-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/jython-users

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users