spacey Posted October 2, 2004 Share Posted October 2, 2004 Hi, I wrote a program that uses a JTable. I wanted each row to be alternating colours. I did this by over-riding the: getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) method in an extended version of the javax.swing.table.DefaultTableCellRenderer Class with code that sets the background based on if the 'row' variable passed to the method is even or odd. Then I call: setDefaultRenderer(Class columnClass, TableCellRenderer renderer) on the JTable object. For example: myTable.setDefaultRenderer(Integer, alternatingRowRender); myTable.setDefaultRenderer(String, alternatingRowRender); Anyway, the problem is that in my table there is a Boolean column which is rendered by default as a check box. If I do: myTable.setDefaultRenderer(Boolean, alternatingRowRender); Then instead of a check box, I get the text 'true' or 'false' (with the alternating row colors). What I want is to keep it rendering the Booleans as a checkbox (as in the DefaultTableCellRenderer Class), but also incorporate alternating row colors. Any help would be greatly appreciated! :yes: Link to comment Share on other sites More sharing options...
0 Mouton Posted October 2, 2004 Share Posted October 2, 2004 Have u tried a getColumnClass() on that column to know what's it's type ? Link to comment Share on other sites More sharing options...
0 spacey Posted October 2, 2004 Author Share Posted October 2, 2004 Yea, I know its Boolean, so the default render for a Boolean type is the checkbox, but if I over-ride it using my render then its just text. Ideally I just want to extend the default render that does the checkbox somehow so the background is dependant on the row number. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 2, 2004 Share Posted October 2, 2004 If u override an actual method, u could try to call the overridden method before or after your code with super.something() Link to comment Share on other sites More sharing options...
0 em_te Posted October 3, 2004 Share Posted October 3, 2004 Rather than overriding DefaultTableCellRenderer, you could just write a proxy that serves up the existing cell renderer. But before returning it, modify its background. Something like: public void getTableCellRendererComponent( ? ? ? ? ? JTable table, Object value, ? ? ? ? ? boolean isSelected, boolean hasFocus, ? ? ? ? ? int row, int column) { TableCellRenderer r = table.getDefaultRenderer( ? ? ?table.getColumnClass(column)); Component c = r.getTableCellRendererComponent(table, value, ? ? ?isSelected, hasFocus, row, column); if(row & 1 == 1)c.setBackground(Color.GRAY); else c.setBackground(Color.WHITE); return c; } And of course you should cache the references instead of getting them every time like mine does. Note, I haven't tried this before so it may or may not work. Link to comment Share on other sites More sharing options...
Question
spacey
Hi, I wrote a program that uses a JTable. I wanted each row to be alternating colours. I did this by over-riding the:
getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
method in an extended version of the javax.swing.table.DefaultTableCellRenderer Class
with code that sets the background based on if the 'row' variable passed to the method is even or odd.
Then I call: setDefaultRenderer(Class columnClass, TableCellRenderer renderer) on the JTable object.
For example:
myTable.setDefaultRenderer(Integer, alternatingRowRender);
myTable.setDefaultRenderer(String, alternatingRowRender);
Anyway, the problem is that in my table there is a Boolean column which is rendered by default as a check box. If I do:
myTable.setDefaultRenderer(Boolean, alternatingRowRender);
Then instead of a check box, I get the text 'true' or 'false' (with the alternating row colors). What I want is to keep it rendering the Booleans as a checkbox (as in the DefaultTableCellRenderer Class), but also incorporate alternating row colors.
Any help would be greatly appreciated! :yes:
Link to comment
Share on other sites
4 answers to this question
Recommended Posts