在 JTable 中为数据库外键添加组合框
我正在做一个 DatabaseManager Proyect,在其中显示 JTable 中数据库表的所有数据。现在我想插入一个功能,以便表的外键与外部表的值一起显示为组合框。所以我想知道哪种是最优化的方法,我知道代表前键的列将填充具有完全相同值的组合框,但每个组合框都有一个特定的默认起始值。这是我现在只在没有组合框的情况下填充数据的代码:
private Collection<Map<String, String>> allData;
Object[] rowToAdd = new Object[manager.get((String) DatabaseJList.getInstance().getSelectedObject()).getDataManager().getColumnNumber()];
for (Map<String, String> rowz: allData)
{
rowx = rowz.values();
int i = 0;
for (String str : rowx)
{
rowToAdd[i] = str;
i++;
}
tableModel.addRow(rowToAdd);
}
因此,由于 rowToAdd 是一个对象数组,我可以创建一个组合框并将其放入其中吗?有什么建议如何做到这一点吗?
非常感谢大家。
I am doing a DatabaseManager Proyect where I display all the data of a db table in a JTable. Now I want to insert a feature so that the foreign keys of a table are displayed as a ComboBox with the values of the foreign table. So I was wondering which is the most optimal way to do it, I know that the Column which represent the foreing key is going to be filled with comboBoxes with the exact same values, but every each of them will have a specific default starting value. Here is the code that I have right now to just fill the data without comboBoxes:
private Collection<Map<String, String>> allData;
Object[] rowToAdd = new Object[manager.get((String) DatabaseJList.getInstance().getSelectedObject()).getDataManager().getColumnNumber()];
for (Map<String, String> rowz: allData)
{
rowx = rowz.values();
int i = 0;
for (String str : rowx)
{
rowToAdd[i] = str;
i++;
}
tableModel.addRow(rowToAdd);
}
So since rowToAdd is an array of Objects, can I just create a comboBox and put it inside?Any suggestions how to do this?
Thanks a lot everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不将数据添加到模型中。您只需指定组合框用作特定的编辑器。请参阅 Swing 教程中有关使用组合框作为编辑器的部分 作为一个工作示例。
You don't add the data to the model. You just specify that a combo box is to be used as the editor for the specific. See the section from the Swing tutorial on Using a Combo Box as an Editor for a working example.