Re: JTable and CellRenderer to change background colors



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

.



Relevant Pages

  • Re: setCellRenderer for specific cell in JTable?
    ... Making many single renderers for each cell type, ... specific renderer;) ... There are two reason why I wouldn't go for overriding getCellRenderer ... the cellrenderer returned should be specified by ...
    (comp.lang.java.gui)
  • Re: Changing the Background Color for One Cell in a JTable
    ... I have a table and want to be able to change the color of one cell in it. ... DefaultTableCellRenderer defRender = ... Do I have to create my own cell renderer to change a cell background ... in Java, you can't get there from here. ...
    (comp.lang.java.programmer)
  • Re: mutate an object or create a new one?
    ... intuitions about costs are wrong in the Java world. ... As a counterpoint to Chris's comments, note that Swing reuses objects in many cases, most notably renderers, for performance reasons. ... The table class defines a single cell renderer and uses it as a as a rubber-stamp for rendering all cells in the table; it renders the first cell, changes the contents of that cell renderer, shifts the origin to the new location, re-draws it, and so on. ...
    (comp.lang.java.programmer)
  • Re: Using JComboBox as a renderer in a JTable
    ... If you omit setting the cell renderer it will work. ... like a string except when you are editing it. ... plus an action listener on the combo box used as a cell editor. ...
    (comp.lang.java.gui)
  • Re: Using JComboBox as a renderer in a JTable
    ... If you omit setting the cell renderer it will work. ... plus an action listener on the combo box used as a cell editor. ... DefaultTableCellRenderer does) in the rendering combo box to be no-ops (see ...
    (comp.lang.java.gui)