1

I like to create a cell with a checkbox and one or two optional textfields.

If I click on the checkbox the textfields get visible. I tried to make an ASCII picture how it should look like:

[ ]
[X] [some string]
[X] [value1] [value2]

I know how to create a custom renderer but I am not sure how to return multiple elements. If I have a renderer for a checkbox I return only the JCheckBox:

class BooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource
{
    public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus, int row, int column) {
        // doing some stuff...
        return this;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180

4 Answers4

4

You have to return a single component (which may be a JPanel in your case) which holds multiple other components.

See the follwing link where a CellRenderer returns multiple entries (it is a ListCellRenderer, but the basics are the same).

uhm
  • 278
  • 3
  • 12
4

@Thomas et al. are correct about distinguishing between the model and the view. Your model includes a boolean state and some corresponding text; your view is a JCheckBox that can be toggled and text that can be changed. You'll need both a renderer to display the current state of the model and an editor to allow changes to the state.

In this example, the class Value holds the selected state and the underlying data, a Double value. The DataModel, which implements TableModel, manages a List<Value>. Note that both the editor and renderer use a common java.text.DecimalFormat. The related class java.text.MessageFormat may be useful in composing your check box's text.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

You'd have to represent that structure in your table model as well, i.e. the cell's value should already be a composite of the boolean flag and the other values.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Do I really need to do this? I can store all the values in one cell using a separator: checkbox|textfield1|textfield2. Maybe not very nice but that would work for me. – PiTheNumber Dec 01 '11 at 11:18
  • @PiTheNumber checkboxes etc. are representations, not values. Thus you can't store them using a separator. Of course you could use a string representation and parse that string every time you need to access a field but I'd say that creating a wrapper object would be far easier to write, understand and maintain. – Thomas Dec 01 '11 at 11:55
1

Your Renderer class will have to return a parent component (as uhm said, probably a JPanel) on which you have your other objects: checkbox, textfields, etc. Your tableModel will have to hold the values that the Renderer uses to portray each of those components. For example, the model may be setup with a List of data objects, and each entry in the list holds a Boolean to indicate if the checkBox is selected, a String of the textField data, etc. You cannot have a Table visually portray a useful Swing object if the associated model isn't storing it someplace.

Colby
  • 459
  • 3
  • 9