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
#3 Ex:
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 ]