Multiple cell renderers for a column in a JTable?
Asked Answered
A

2

3

Let's say I have the following JTable, which is displayed as soon as a button is pressed:

      | Name
------+------------
True  | Hello World
False | Foo Bar
True  | Foo
False | Bar

I want to render the cells that were initially true to a JCheckBox, and all cells that were initially false to not display anything (no JCheckBox). The user could check or un-check the JCheckBoxes in the cells that were initially true, which would do something to a chart I created.

Right now, my cell renderer displays JCheckBoxes in all cells, including those that were initially false (it displays those JCheckBoxes without check marks), but I want to not display anything in the latter. Here is my code:

protected class CheckBoxCellRenderer extends JCheckBox implements TableCellRenderer {

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    if (!(Boolean) tableModel.getValueAt(row, 0)) {
      NoCheckBoxCellRenderer renderer = new NoCheckBoxCellRenderer();
      return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
    this.setSelected((Boolean) tableModel.getValueAt(row, 0));
    return this;
  }

}

protected class NoCheckBoxCellRenderer extends DefaultTableCellRenderer {

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    this.setVisible(false);
    return this;
  }
}

In the if statement, I tried using this.setVisible(false) before using NoCheckBoxCellRenderer, but it wasn't working. I'm thinking about using multiple cell renderers to accomplish this task. Would it be possible to do so? Any advice would be greatly appreciated!

Autocratic answered 7/10, 2011 at 17:16 Comment(3)
You should use a static field for your NoCheckBoxCellRenderer object. Table cell renderers are loaded and discarded quickly and often, so streamlining these is pretty useful. Of course, premature optimizations being what they are...Acea
are you meaning this way #7046351Teucer
private static class *Renderer perhaps?Diastyle
O
5

Store Boolean.TRUE for the true values. Then store an empty String for the false values. You will then need to:

a) override the getCellRenderer(...) method to return the appropriate renderer for the data found in the cell.

b) make the cells containing the empty string non-editable:

JTable table = new JTable(data, columnNames)
{
    public TableCellRenderer getCellRenderer(int row, int column)
    {
        if (column == 0)
        {
            Class cellClass = getValueAt(row, column).getClass();
            return getDefaultRenderer( cellClass );
        }

        return super.getCellRenderer(row, column);
    }

    public boolean isCellEditable(int row, int column)
    {
        Class cellClass = getValueAt(row, column).getClass();

        if (column == 0 && cellClass instanceof Boolean)
        {
            return true;
        }
        else
        {
            return false;
        }

        return super.isCellEditable(row, column);
    }

};

Using this approach there is no need for custom renderers or editors.

Osswald answered 9/10, 2011 at 4:16 Comment(0)
A
3

Have getTableCellRendererComponent return a blank JLabel if the initial value was false.

Acea answered 7/10, 2011 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.