Hi All, I have to use combobox as JTable cell editor. In the first cell from the combobox when some item is selected based on that the corresponding second column cell will be populated with different data. Exa : 1st combobox of the first column has the following data : "ABC","DEF" ABC has following data : "aaa", "bbb", "ccc" DEF has following data : "ddd", "eee", "fff"
So on selecting ABC from the 1st combobox in the 1st cell of the row, will lead to populate the 2nd combobox in the second cell of the same row with its corresponding data.
I hope i am able to explain the problem. How can i achieve this ?
In order to have one column of JComboBoxes within a JTable whose values depend on the selections made in another column of JComboBoxes, follow these instructions:
1. Create a table model class that extends AbstractTableModel and override the methods you'll need to maintain your table. The sun site does have good instructions on this step.
2. Create a custom TableCellRenderer. This renderer will display JComboBoxes on your table even when they are not selected. If you don't return this class for a cell, the value will appear as plain text. a. make a new class that extends JComboBox and implements TableCellRenderer b. the constructor must take a String for the items in the JComboBox that you'll send to the super() constructor
3. Create a custom JComboBox cell editor. This will allow the users to use JComboBox to select values within cells. a. make a new class that extends DefaultCellEditor b. must take String for items
4. Create a custom JTable! This is the most important step. You need to do this so you can override the 'getCellRenderer' and 'getCellEditor' methods within JTable to return the renderers and editors you choose. a. make new class that extends JTable b. override getCellRenderer to return the proper TableCellRenderer (the one you created in the correct columns) for "int row, int col" c. override getCellEditor to return the proper TableCellEditor (the one you created in the correct columns) for "int row, int col"
5. Create a data model that drives the table model. This data model must react accordingly when a value is selected within it. If one value is selected within the model and another must change, then the model should take care of this. It is much easier to do is this way! Keep your data separate from the view! This model should drive the display. Look up the MVC design pattern for more details. [ August 02, 2005: Message edited by: Peter Glass ]
Re your PM, I figured you would have both problems, so gave you the help for the first part first If you have the combo boxes working OK, I would say the next part depends on how exactly your data works.
Is the data in the second (and third etc) combo boxes static or dynamic? I.e. does 'ABC' always have the subchoices of 'aaa', 'bbb', and 'ccc', or do those subchoices depend on the contents of some other data structure? If the data is static, you would have to make two different celleditors: the initial one - a plain old combo box, and a second one that implements ActionListener and waits for changes from the first combo box, then populates itself with the appropriate values.
If the data is dynamic the approach will be similar but you will need to interface with your TableModel, which makes things more complicated.
Either way, I think you should make a class that implements both CellEditor and ActionListener, and either extends JComboBox or encapsulates an instance of it.
Stuart, in my example all the data within the table is dynamic. It gets loaded from other areas of the program and there is little control over it. But you can create a data model to drive the table that will prevent implementing ActionListeners with your CellEditors.
The data within the table model should be able to handle itself, so if there is a change in a value that affects another value, the model should be smart enought to change itself. This keeps it simple within the model as well as making the table simpler from the outside, and no unecessary ActionEvents flying about.