mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
Was that a question?
You create row containing a Color.RED as its value and it's displayed as "java.awt.Color[r=255,g=0,b=0]", which is exactly what you would expect since the default renderer calls toString() on the value Object.
To achieve the result you described in your first post I would expect to see some piece of code in the getTableCellRendererComponent method that tests the row number and sets the background color according to the row number.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
n the first parameter for setDefaultRenderer, put the class literal for the Class that you want to override rendering. I.e., if your data consist all of strings, you can put
myTable.setDefaultRenderer(String.class, new CustomRenderer());
If your data also consists of values with BigDecimal or Integer as classes, you have to invoke that method several times for each class type (BigDecimal.class or Integer.class in each case).
And finally, to change the background color you do this in your renderer:
class CustomRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(new java.awt.Color(255, 72, 72));
return c;
}
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
only other thing i can say is try
Color.RED
instead of
new java.awt.Color(255,72,72)
you may need to add
import java.awt.color;
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
This is my code now , it still prints me java.awt.Color[r=255,g=72,b=72]
I already explained this - did you read my earlier post?You create row containing a Color.RED as its value and it's displayed as "java.awt.Color[r=255,g=0,b=0]", which is exactly what you would expect since the default renderer calls toString() on the value Object.
model.addRow(new Object[]{"",new java.awt.Color(255, 72, 72)});
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073