JTable with multiple elements in one cell

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;
    }
}

Answers


@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.


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).


Need Your Help

Best Free Text Editor Supporting *More Than* 4GB Files?

editor text-editor text-files large-files

I am looking for a text editor that will be able to load a 4+ Gigabyte file into it. Textpad doesn't work. I own a copy of it and have been to its support site, it just doesn't do it. Maybe I ne...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.