-
Color cell in JTable
Hi,
I'm new of Java graphics and I've some problems with a JTable.
I need to change the color of cells(not of the text in the cell) only for the first 5 cells in my JTable. This color should change in a jToggleActionPerform method.
I know that a CustomTableCellRenderer is necessary but how can I use it in the method?
Is there anyone who could help me?
Thanks a lot
-
You may be able to achieve this without a custom renderer. Set a boolean field to the state of the JToggleButton in its actionPerformed, and repaint() the table. Then setBackground of the cell in a prepareRenderer override based on the boolean field value and the row, column parameters.
Here's a prepareRenderer override that colors table cells on the diagonal yellow: Code:
JTable table = new JTable(data, headers) {
@Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
JLabel label = (JLabel) super.prepareRenderer(renderer, row, column);
if (row == column) {
label.setBackground(Color.YELLOW);
} else {
label.setBackground(Color.WHITE);
}
return label;
}
}
db
-
Thanks a lot for your help.
I've not understood very well the question about the boolean field in jToggle button. Considering I work with NetBeans IDE and I don't create directly the jTable, how could I do for solving the question?
Thanks again
-
Learn how to code a GUI and dump the visual designer until you do that.
Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)
Or limit yourself to what you can do with the visual designer.
It's your choice.