Re: JTable and CellRenderer to change background colors
- From: Michael Rauscher <michlmann@xxxxxx>
- Date: Fri, 29 Apr 2005 00:00:16 +0200
Hi Christian,
Christian schrieb:
Hi!
Quick question. I'd like to set the background color for each cell in a JTable separately. Now I've already found out that this can be done using the DefaultTableCellRenderer class, the problem is that whenever I try to set a particular cell, the entire table gets flooded with the new color, because obviously the renderer I set serves the entire table. Now how can I assign one renderer for each cell in my table or is there a more convenient way to change the background colors? In the JAVA API I have a method getCellRenderer(int row, int column), but no method that allows me to set a CellRenderer for a particular cell.
You don't need to and IMHO you shouldn't do it this way: imagine you want to use 300 colors. Do you really want to create 300 different renderer objects?
Have a look at the TableCellRenderer interface. You'll see that the getTreeCellRendererComponent method takes the row and column indexes as arguments.
Therefore, all you basically need is one cell renderer, that takes care of the given "coordinates".
A very basic and dumb implementation could look like this:
public class ColorAwareTableCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent( JTable table,
Object v, boolean sel, boolean focus, int y, int x ) {
Component c;
// initialize c
if ( c != null ) {
if ( y == 5 && x == 3 )
c.setBackground( Color.BLUE );
// else if ...
}
}
}Usually, you don't want your cell renderer to create the component. You just want it to set the background color of a component. So, perhaps you want to "decorate" another cell renderer. Add the following to the class
private TableCellRenderer cellRenderer;
public ColorAwareTableCellRenderer(TableCellRenderer cellRenderer) {
if ( cellRenderer == this || cellRenderer == null )
throw new IllegalArgumentException();
this.cellRenderer = cellRenderer;
}Additionally replace "// initialize c" (see above) with
c = cellRenderer.getTableCellRendererComponent( table, v, sel,
focus, y, x );Now, you can use this cell renderer e.g. as the default renderer of a JTable for the Object class:
TableCellRenderer original;
JTable table;
// initialize table original = table.getDefaultRenderer(Object.class);
table.setDefaultRenderer( Object.class,
new ColorAwareTableCellRenderer(original) );This approach is very flexible and extensible. Perhaps, you want to implement a color model which greatly simplifies color management.
Perhaps, ...
Bye Michael
.
- References:
- JTable and CellRenderer to change background colors
- From: Christian
- JTable and CellRenderer to change background colors
- Prev by Date: Re: Difference between itemStateChanged and stateChanged for a JCheckBoxMenuItem
- Next by Date: Re: JTable and CellRenderer to change background colors
- Previous by thread: JTable and CellRenderer to change background colors
- Next by thread: Re: JTable and CellRenderer to change background colors
- Index(es):
Relevant Pages
|