我在互联网上搜索了一些解决我问题的方法,但我似乎还没有想出任何办法,而且我真的一直在努力解决这个问题。
简而言之,我需要创建一个嵌入 JTable 的 JDialog。该 JTable 的列数量必须是动态的。另外,我希望 JTable 的前两行的每个单元格中都有组合框。第一行都具有相同的组合框,第二行都具有相同的组合框,但与第一行不同。从那里开始,我只需为一组其他行填充静态文本数据。所以表格的形式应该是:
组合,组合,组合,组合,...
组合,组合,组合,组合,...
文本,文本,文本,文本,...
我遇到了很多麻烦a)使列号动态(这将取决于传递的数组)和b)仅制作前两行组合框 - 唯一的方法我可以在任何地方找到制作整列组合框的地方。
如果有帮助的话,我正在使用 Netbeans IDE。提前非常感谢您抽出宝贵的时间,如果需要任何进一步的知识,请告诉我。
I've scoured the internet looking for bits and pieces of a solution to my problem, but I haven't seemed to come up with anything yet, and I've really been struggling to solve this.
In short, I need to create a JDialog that embeds a JTable. This JTable has to be dynamic in regards to its amount of columns. Also, I'd like the first two rows of the JTable to have combo boxes in each of their cells. Row one all has the same combo box, and row two all has the same combo box, different from row one. From there, I'm just filling in static text data for a set number of other rows. So the table should be of the form:
combo, combo, combo, combo, ...
combo, combo, combo, combo, ...
text, text, text, text, ...
I'm having a lot of trouble a) making the column number dynamic (it will depend on an array passed) and b) making just the first two rows combo boxes - the only way I can find anywhere makes an entire column combo boxes.
If it helps at all, I'm using Netbeans IDE. Thank you very much in advance for your time, and if any further knowledge is needed, just let me know.
发布评论
评论(2)
对于动态列数部分,这是由 DefaultTableModel列,或者更好的是,通过扩展 AbstractTableModel,并根据包含数据的数组实现
getColumnCount()
。对于组合部分,我猜您想使用组合来编辑表中的数据。您需要扩展 JTable 并重新定义 getCellEditor() 方法,以便在行为 0 或 1 时返回一个返回 JComboBox 的 TableCellEditor。并且您可以返回
super.getCellEditor(row, column)
如果该行大于 1。您还应该阅读 有关 JTable 的 Java 教程< /a>,其中包含有关您想做的所有事情的部分。
For the dynamic number of columns part, this is defined by the TableModel of your JTable. Use a DefaultTableModel with the appropriate number of columns, or, even better, implement your own table model by extending AbstractTableModel, and implement
getColumnCount()
based on the array containing your data.For the combo part, I guess you want to use a combo to edit the data in the table. You the need to extend JTable and redefine the getCellEditor() method in order to return a TableCellEditor returning a JComboBox if the row is 0 or 1. And you may return
super.getCellEditor(row, column)
if the row is bigger than 1.You should also read the Java Tutorial about JTable, which has sections about everything you want to do.