Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

Combo Box Table Editor

Posted by Rob Camick on June 28, 2009

When using a JComboBox as an editor in a column of a JTable the same combo box is used for all rows in the column. There may be times when you want different rows to use a different combo box.

Two approaches I’ve seen to solve this problem would be:

  • override the getCellEditor() method of JTable to return the appropriate editor. The row and column are passed as parameters to this method so you would use those values (along with any other table data, if necessary) to determine which editor to use.
  • create a custom TableCellEditor and override the getTableCellEditorComponent() method to return the appropriate editor. Again, the row, column and all table data is available to help in determining which editor to use.

Theoretically you could have a different editor for every row in the table using either of the above approaches. However my proposed solution, which is based on using a custom editor, will assume that the choice of the editor to be used is based on some other data in the table. That is, the value from another column in the same row will be used to determine the editor. As a simple example you could have a table with two related columns like “Month” and “Day”. The editor to be used to select a “Day” can only be determined once the “Month” has been selected.

The ComboBoxTableEditor is a custom editor that uses a single combo box as the editor but it allows you to specify different ComboBoxModels for different values in the related column. Using the ComboboxTableEditor is straight forward. First, you need to specify the related column, which is done when the ComboBoxTableEditor is created. Secondly, you need to map possible values from this column to a specific ComboBoxModel. This is done by using the addModel() method.


From the image above you can see that the first column contains 3 values: “Color, Fruit, Shape”. The image also shows the unique editor for the “Shape” type. To create the custom editor for the second column we need to create 3 separate models, one for each value. The code would be something like:

ComboBoxTableEditor editor = new ComboBoxTableEditor(0);
table.getColumnModel().getColumn(1).setCellEditor( editor );

String[] items1 = { “Red”, “Blue”, “Green” };
editor.addModel(“Color”, items1);

String[] items2 = { “Circle”, “Square”, “Triangle” };
editor.addModel(“Shape”, items2);

String[] items3 = { “Apple”, “Orange”, “Banana” };
editor.addModel(“Fruit”, items3);

Now, as the values of the first column are dynamically changed, the model used by the combo box editor in the second column will also be changed. Hopefully, this class will help when you need to use multiple combo boxes in a single column. If the class doesn’t do exactly what you want maybe you can customize the behaviour by overriding the getModelForRow() method. Good luck.

Note, to make the Webstart demo a little(?) more realistic it uses code from the “Table Cell Listener” and “Combo Box Prompt” entries. You can find out more information about these classes from the links below.

Try The Demo

Launch – Using Java™ Web Start (JRE 6 required)

Get The Code

See Also

Table Cell Listener
Combo Box Prompt

Related Reading

Java Tutorials: Concepts: Editors and Renderers


4 Responses to “Combo Box Table Editor”

  1. Ark said

    works great. thank you
    is it possible to pop-up the controlled combo box when selection is made in the related one?
    I tried using TableCellListener and call ((JComboBox) editor.getComponent()).showPopup() but get:
    java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    thanks again

  2. Sudheer said

    Hi Rob,

    I am unable to determin how to use this feature. Can you send me a working example. Thanks!! in advance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: