如何在 JTable 单元格中使用 JList?
我想要一种简单的方法将 JList 放入 JTable 的列中。我已经有了 JLists 和表格,但是当放入表格中时,Jlists 显示为字符串,这是正常的,因为我使用 DefaultTableModel。我已将 getColumnClass() 重写为:
public Class<? extends Object> getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
但这只是格式化整数和浮点值。
我认为 setValueAt() 和 getValueAt() 也应该被重写,以便在调用 JList.getSelectedValues() 时返回字符串数组em>,但我不知道怎么做。
我还希望单元格可编辑,以便用户可以从 JList 中选择一个或多个选项。编辑一行后,我使用“保存”按钮将更改保存在数据库中,因此我认为我不需要 ListSelectionListener,JList.getSelectedValues() 工作得很好。
我知道这是一个常见问题,但我在这里找不到答案。如果这是重复的,请告诉我,我将删除它。
I would like a simple way to put a JList in a column of a JTable. I already have the JLists and the table, but when put in the table, the Jlists are displayed as Strings, which is normal, because I use DefaultTableModel. I have overriden the getColumnClass() as:
public Class<? extends Object> getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
but this just formats the integer and float values.
I suppose that setValueAt() and getValueAt() should also be overriden, in order to return am array of Strings when I call JList.getSelectedValues(), but I can't figure out how.
I also want the cells to be editable, so the users can choose one or more option from the JList. After editing a row, I use a Save button to save the changes in a database, so I don't think I need a ListSelectionListener, JList.getSelectedValues() works just fine.
I know this is a common question, but I couldn't find an answer here. If this is a duplicate, please let me know and I will delete it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经做到了。对于需要同样东西的每个人,这就是我所做的:
1)我创建了一个 JScrollTableRenderer,并设置了我需要显示 JList 的列以使用此渲染器
JScrollTableRenderer 类内容:
2)我创建了一个 JScrollTableEditor ,并设置我需要显示 JList 以使用此编辑器的列
JScrollTableEditor 类内容:
3)我在 JTable 模型中添加了此方法:
I've done it. For everyone who needs the same thing, here is what I've done:
1)I have created a JScrollTableRenderer, and set the column I needed to show the JList to use this renderer
The JScrollTableRenderer class content:
2)I have created a JScrollTableEditor, and set the column I needed to show the JList to use this editor
The JScrollTableEditor class content:
3)I added this method in the JTable model: