how do i change the color of a row in a JTable..Wat's the source code to use?... Anybody knows....?
Thanks alot...
Printable View
how do i change the color of a row in a JTable..Wat's the source code to use?... Anybody knows....?
Thanks alot...
This is asked quite often here. If you do a search of this forum for something like "JTable row color change", you should find plenty of information.
Time wounds all heels
hi..i did a search of the forum..i found many similar queries regarding the changing of colors of rows in a JTable but i still do not understand..I know i have to use the TableCellRenderer but i'm not so familiar...Could u help me out again? Thanks alot..
OK, here's an example off the top of my head:This is untested code, but it shows the basic technique.Code:// Create a renderer to color a row
class MyTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
// let the superclass do all the work
Component renderer = super.getTableCellRendererComponent(
value, isSelected, hasFocus, row, column);
// Change background to the required color
// if this is the required row
if (row == requiredRow) {
renderer.setBackground(requiredColor);
}
return renderer;
}
...
// Set this renderer for all class types (i.e. columns) in the table
myTable.setDefaultRenderer( Object.class, new MyTableCellRenderer());
The higher, the fewer...