Skip to main content

PRoblem with CellRenderer and JXTable

2 replies [Last post]
roger_gl
Offline
Joined: 2009-02-15

It seems as if the first call to SetBackground determines the color to be used. The color setter class should color the first 3 rows green and the rest white. That works with JTable. JXTable colors all rows green.

Regards
Roger

public class NumericCellRenderer extends DefaultTableCellRenderer {

private static final long serialVersionUID = -8761879102484189786L;

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int rowIndex, int columnIndex) {

String v;

if (value == null) {
v = "";
} else {
DecimalFormat df = new DecimalFormat("###,###,##0.0");
v = df.format(((BigDecimal) value).doubleValue());
}

setHorizontalAlignment(JLabel.RIGHT);

if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
if (table instanceof JTableColored) {
JTableColored tc = (JTableColored) table;
ITableCellColorSetter cs = tc.getColorSetter();
setForeground(cs.getForgroundColor(rowIndex, columnIndex));
setBackground(cs.getBackgroundColor(rowIndex, columnIndex));
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
}

setText(v);

return this;
}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
roger_gl
Offline
Joined: 2009-02-15

I just disabled resetDefaultTableCellRendererColors. And everything is working now as expected.

Regards
Roger

kleopatra
Offline
Joined: 2003-06-11

Roger,

(still don't know how to format your code ;-)

Follow the SwingX wind of customizing content and visuals, that is use StringValue and Highlighters. If you insist on sticking to your old habits, please read the JXTable java doc, especially the part about hackAroundDTCR, and look into the wiki pages about rendering (there a handful of links to older threads covering some dirty details about core default renderers).

Cheers
Jeanette

just noticed that you never got a hint as to how to format, so here we go:

BTW, you can format code by taggin it with [ code ] .... [ /code ] - without the spaces. Please complain to the site admin that this isn't documented anywhere ;-) Please edit your post and add the tags, thanks

Message was edited by: kleopatra