如何将 JComboBox 添加到 JTable 单元格?
我正在尝试将 JComponent 添加到 JTable 单元格。我应该实现 CellRenderer 还是 CellEditor?
I am trying to add JComponents to JTable Cells. Do I Implement CellRenderer or CellEditor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的是一个自定义编辑器,它将返回 JComboBox(或您想要使用的任何组件)。您应该查看 Sun JTable 教程,它包含一个有关如何使用 JComboBox 作为编辑器的示例。如果您也想使用 JComboBox 作为渲染器,本教程也适用。
What you need is a custom editor which will return the JComboBox (or whatever component you want to use). You should check the Sun tutorial for JTable, it contains an example on how to use a JComboBox as an editor. If you want to use JComboBox as a renderer as well, the tutorial applies to that too.
您还可以使用 DefaultCellEditor 来完成此操作通过将 JComboBox(或 JCheckBox 或 JTextField)的实例传递给构造函数。
You could also do it with the DefaultCellEditor by passing in an instance of a JComboBox (or JCheckBox or JTextField) to the constructor.
1- 创建一个
JCombobox
并向其中插入所需的信息,如下所示:2- 创建一个
JTable
并为此表设置表格模式,如下所示:3-您可以将 JComboBox 放入 JTable Cells 中,如下所示:
1- Create a
JCombobox
and insert into it the information you want, like this:2- Create a
JTable
and set a Table Mode to this table, something like:3- You put the JComboBox in JTable Cells like this: