Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change table cell color in Java

Tags:

java

swing

jtable

I have read and implemented this Changing JTable cell color

What I'd like to know is how to actually use this code? I just want to change a table cell's colour when I click on it.

like image 791
pypmannetjies Avatar asked Dec 28 '22 17:12

pypmannetjies


2 Answers

In the code you refer to, you have a custom CellRenderer.

Once you added it to the table, all you need is to do the formatting in the appropriate place:

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);

        // Formatting here

        return c;
    }
}

A DefaultTableCellRenderer is nothing more or less than the component which will be used in the JTable, to paint the cells. To be more precise, in this case, the component is a JLabel (you can see this by checking sources from DefaultTableCellRenderer).

So all the formatting you should do is on the "c" object (or on "this", since the method actually returns the same component each time: itself). For example, c.setBackground() will allow you to set a background color.

The getTableCellRendererComponent() method which is overridden will be called for each cell of the JTable, with parameters telling you about the context. You know the table, the row, the column, the value which is supposed to be displayed, and you also know if the cell is selected or not, which could help with your case:

if (selected)
    c.setBackground(Color.YELLOW);

To go further, note that because you override the DefaultTableCellRenderer class, and use its own method, you have already some formatting done, like the background color, which is the one from the table. As such, you need only to define your own color when you need to. If not, you would have to take care about all cases, because since the same component is used, you would end with the color set once, and then applied to all consecutive cells, because nothing would have been done to change it.

I recommend you to read the sources from DefaultTableCellRenderer (and its uses in JTable), if you want to learn more about the way it is done and used.

like image 147
Gnoupi Avatar answered Dec 31 '22 05:12

Gnoupi


Does this mean the cell color changes forever, or does it reset once you click on another cell.

If you just want the color to change temporarily then the easiest way is to use the concepts presented in Table Row Rendering so you don't have to create multiple renderers for each type of data.

If you want the cell color to be permanent, then it is much more involved, because now you actually need to save the data for each cell that should be colored differently. Again the easiest approach is to use the approach from above and then maybe keep a Set of all the colored cells.

like image 35
camickr Avatar answered Dec 31 '22 05:12

camickr