Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TableCellRenderer Problem

Status
Not open for further replies.

mbaranski

Programmer
Nov 3, 2000
421
0
0
US
I have a table cell renderer class, that should color certian cells of a table and leave others the default. The problem is, all the cells take the properties of the last one I set! The text is correct in each of them, but if I set them all blue except the last one, they are all the color of the last one. Here is some of the code. Thanks for any help.

JTable jt = new JTable....

jt.setDefaultRenderer(Class.forName("java.lang.Object"), (TableCellRenderer)jt.getModel());

This is the renderer.

public class InstrumentTableModel extends AbstractTableModel implements TableCellRenderer

public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
if(value != null)
{
j = new JLabel(value.toString());
}
else
{
j = new JLabel("");
value = "";
}

j.setOpaque(true);
j.setBackground(Color.getColor(((InstrumentTableModel)table.getModel()).getHighlightedColor(value.toString())));

j.setForeground(Color.getColor(((InstrumentTableModel)table.getModel()).getHighlightedColor(value.toString())));

return j;
}
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Oh yes, the "good" old TableCellRenderer.
I don't know the exact solution, but i'm quite sure, that you need only one component (e.g. your JLabel) globally that is used by each cell.
As i said, i'm not sure, the TreeCellRenderer works this weird way, i guess the TableCellRenderer is similar.
Hope this helps as i have done a TreeCellRenderer 9 months ago...
Christoph
 
Making the JLabel global and setting the properties does not help. Does anyone know what the actual problem/solution is?
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top