I know changing cell backgrounds in jtable is done by creating a new cellrenderer class. I've done that. I've read about DefaultTableRenderer "color memory" issue, but I can't figure out how to work around it for my particular purpose.
My goal is simple enough: When a button is clicked, change the background color of all selected cells in the jtable.
I have the adequate method calls set up for the event, but I can't get the renderer to work the way I want it to.
I have all selected cells stored in an arraylist of TableCells (a class containing row, column, and the cell's text string data). Here is my code for getTableCellRendererComponent inside of my CustomCellRenderer.
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
for(TableCell c: selectedCells)
{
if(c.row ==row && c.col == column)
{
this.setBackground(Color.black);
}
else
{
this.setBackground(Color.BLUE);
}
}
return this;
}
This code sets the background of all table cells' backgrounds to blue. Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.
Thanks.
How about giving your renderer class a boolean variable, say btnClicked that is initialized to false but set to true in the button's ActionListener, a listener which also instructs the table to repaint itself. Then in the renderer itself, you could use the selected property to see if a cell is selected or not. Perhaps something like:
Also regarding:
What is this "color memory issue that you speak of?
Edit 1
Here is a compilable example of what I meant. I'm still not sure what you mean by the color memory issue though, sorry.