简体   繁体   中英

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

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.

请阅读使用组合框作为编辑器 ,因为JTable也知道渲染器和编辑器的JComboBox, 这里有示例,以及关于EachRowEditor的示例

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM