简体   繁体   English

JTable,JComboBox动态值

[英]JTable, JComboBox dynamic values

I have JTable . 我有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"). 在其中3个我有JComboBox (每个列的每个单元格都有唯一的JComboBox - 即整个第1列 - “apple”,“bannana”;第2列 - “红色”,“蓝色”;第3列 - “猫”,“狗” )。 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. 问题是,为每个列添加相同值的JComboBox是不够的。 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). 因为在将JComboBoxJTable ,你必须实现TableCellRenderer并扩展DefaultCellEditor ,否则你不能在表中看到JComboBox ,除非你点击它( TableCellRenderer错误)或代替comboBox你只得到单元格中的Object.toString()行( DefaultCellRenderer故障)。 So how should I handle JComboBox , TableCellRenderer and DefaultCellEditor so I can dynamicly change values of whole JComboBox which is in JTable . 那么我应该如何处理JComboBoxTableCellRendererDefaultCellEditor这样我就可以动态地改变JTable中整个JComboBox值。

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 . JTable和JComboBox分别具有AbstractTableModelComboBoxModel 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. 在我看来,你必须覆盖TableCellRenderer和DefaultCellEditor以返回相同的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. 因此,您可以将一个JComboBox作为参数传递给TableCellRenderer和DefaultCellEditor,然后通过所述类的getRenderer或getEditor例程返回该JComboBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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