Change table cell color in Java

I read and implemented this JTable Cell Color Change

What would I like to know how to actually use this code? I just want to change the color of a table cell when I click on it.

+4
source share
3 answers

In the code you refer to, you have your own CellRenderer.

After you have added it to the table, you only need 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 than a component that will be used in JTable to draw cells. To be more precise, in this case the component is JLabel (you can see this by checking the sources from DefaultTableCellRenderer).

So, all the formatting you have to do is the object β€œc” (or on β€œthis”, since the method actually returns the same component every time: itself). For example, c.setBackground() allows you to set the background color.

The getTableCellRendererComponent() method, which is overridden, will be called for each JTable cell whose parameters tell you about this context. You know the table, row, column, value that should be displayed, and you also know if the cell is selected or not, which may help in your case:

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

To continue, note that since you override the DefaultTableCellRenderer class and use your own method, you have already done some formatting, for example, the background color, which is that of the table. Thus, you only need to define your own color when you need. If not, you will need to take care of all cases, because since the same component is used, you have to finish the color set once, and then apply to all consecutive cells, because nothing has been done to change it.

I recommend you read the sources from DefaultTableCellRenderer (and its use in JTable) if you want to know more about how this is done and used.

+8
source

Does this mean that the color of the cell changes forever, or does it reset after clicking on another cell.

If you want the color to change temporarily, the easiest way is to use the concepts presented in Table Row Rendering , you need to create several visualization tools for each data type.

If you want the cell color to be constant, then it is much more involved, because now you really need to save data for each cell, which should be colored differently. Again, the easiest approach is to use the approach from above, and then possibly save the set of all the color cells.

+2
source

I also fought when I wanted to colorize a specific cell in JTable. You can create a visualization of the table cell and send the / col line as parameters:

 class CustomRenderer extends DefaultTableCellRenderer { int col; int row; public CustomRenderer (int col, int row) { this.col = col; this.row = row; } 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); setForeground( (column == this.col && row == this.row) ? Color.red : Color.black ); return c; } } table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer(0, 1); table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer(1, 3); 
0
source

Source: https://habr.com/ru/post/1308989/


All Articles