Minutis
Minutis

Reputation: 1202

JTable, JComboBox dynamic values

I have JTable. In it I have few columns. In 3 of them I have JComboBox (each column's each cell has unique JComboBox - i.e. whole column 1 - "apple", "bannana"; column 2 - "red", "blue"; column 3 - "cat", "dog"). At program start I want them to be empty. Later after few action events I'm filling them. The thing is, it's not enough to add JComboBox with same values for each column. Because after adding JComboBox to JTable, you have to implement TableCellRenderer and extend DefaultCellEditor or else you can't see JComboBox in table unless you click on it (TableCellRenderer fault) or instead of comboBox you get just Object.toString() line in cell (DefaultCellRenderer fault). So how should I handle JComboBox, TableCellRenderer and DefaultCellEditor so I can dynamicly change values of whole JComboBox which is in JTable.

Upvotes: 3

Views: 2640

Answers (3)

mKorbel
mKorbel

Reputation: 109813

please to read Using a Combo Box as an Editor, because JTable know JComboBox for Renderer and Editor too, examples here, and example about EachRowEditor

Upvotes: 2

vextorspace
vextorspace

Reputation: 934

It seems to me you would have to override both the TableCellRenderer and DefaultCellEditor to return the same JComboBox. So you could have a JComboBox passed as an argument to the TableCellRenderer and DefaultCellEditor and then have that JComboBox returned by the getRenderer or getEditor routines of said classes.

Upvotes: 0

Costis Aivalis
Costis Aivalis

Reputation: 13728

There is one more thing you should consider. It is using a model. The model will to hold the apples and bananas and the cats and dogs. JTable and JComboBox have respectively AbstractTableModel and ComboBoxModel. Check the tutorial for examples.

Upvotes: 3

Related Questions