Click to See Complete Forum and Search --> : changing the color of a row in a JTable


geniez29
January 30th, 2003, 07:55 PM
how do i change the color of a row in a JTable..Wat's the source code to use?... Anybody knows....?

Thanks alot...

dlorde
January 31st, 2003, 06:32 AM
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

geniez29
February 3rd, 2003, 07:38 PM
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..

dlorde
February 4th, 2003, 06:07 AM
OK, here's an example off the top of my head:// 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());This is untested code, but it shows the basic technique.

The higher, the fewer...