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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TableCellRenderer still not working right

Status
Not open for further replies.

mbaranski

Programmer
Nov 3, 2000
421
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.setText(value.toString());
}
else
{
j.setText("");
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.
 
I think you need to specifically consult the row and column in the renderer to determine if that cell should be coloured. As it is right now, the setBackground and setForeground calls are made on every cell. Since the "last one you set" is probably the one with the focus, it's probably highlighted and so those are the colours you get.

Come to think of it, maybe just consulting hasFocus would be enough - colour the cell that has the focus only...
 
Wrong. You are correct that I call it for every cell, but notice that I make a function call (getHighlightedColor(value.toString())), instead of passing a static color. This returns different colors for different values. Coloring the cell that has the focus only is nonsense, I want to display patterns of colored cells.
 
Colouring the cell that has the focus is not nonsense, it is the logical conclusion given the very limited scope that you have provided us with, and lack of description to go with it.

Most people use a table for displaying rows and columns of data and using the Render to highligh a subset of that table, thus the conclusion on using hasFocus, people normally dont use Tables for making pretty patterns on the screen.

So I'm sure you can forgive idarke and his "nonsense", I know I can.

-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Thank you, jfryer.

So is this a JTable problem? Are you sure that the getColor() and getHighlightedColor() methods are returning the values you're expecting?
 
Try inserting a couple of
Code:
System.out.println
lines in the above code, one line after
Code:
j.setBackground
and one after
Code:
j.setForeground
to double check the values you are using.

And where is
Code:
j
being declared? If it is being declared outside the
Code:
getTableCellRendererComponent
method that might be causing a couple of problems. -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top