JCombobox 包含表内的枚举值
我有一个包含带有值的枚举的类。 (姓名) 在其他课程中,我想在表中输入将使用这些枚举值的 JCombobox 单元格类型。 我的问题是在字符串值和枚举之间进行组合。 例如枚举类:
<块引用>枚举 item_Type {主菜、main_Meal、甜点、饮料}
例如表类: setTitle("添加新项目" ); 设置大小(300, 80); setBackground( 颜色. 灰色 );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
//new JComboBox(item_Type.values());
JComboBox aaa = new JComboBox();
aaa = new JComboBox(item_Type.values());
TableColumn sportColumn = table.getColumnModel().getColumn(2);
// Create columns names
String columnNames[] = {"Item Description", "Item Type", "Item Price"};
// Create some data
String dataValues[][] = {{ "0", aaa, "0" }};
// Create a new table instance
table = new JTable( dataValues, columnNames );
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
我知道在 dataValues 数组中我不能使用 aaa (枚举 jcombobox)。 我怎样才能做到这一点?
提前致谢。
I have a class containing Enum with values. (names)
In other class I would like to enter inside a table a cell type of JCombobox that will use these enums values.
my problem is to combain between string values and the enum.
for example the enum class:
enum item_Type {entree, main_Meal, Dessert, Drink}
for example the table class:
setTitle("Add new item" );
setSize(300, 80);
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
//new JComboBox(item_Type.values());
JComboBox aaa = new JComboBox();
aaa = new JComboBox(item_Type.values());
TableColumn sportColumn = table.getColumnModel().getColumn(2);
// Create columns names
String columnNames[] = {"Item Description", "Item Type", "Item Price"};
// Create some data
String dataValues[][] = {{ "0", aaa, "0" }};
// Create a new table instance
table = new JTable( dataValues, columnNames );
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
I know that at the dataValues array I cant use aaa (the enum jcombobox).
How can I do that?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置一个 TableCellEditor在
JTable
上显示组合框。在您的
dataValues
数组中,只需为组合框使用占位符:当然,您需要在创建表之后设置列编辑器:
我强烈建议您查看如何使用表格教程,如果你还没有的话。它更详细地解释了这一点,并包含示例代码。
You need to set a TableCellEditor on the
JTable
to display the combo box.In your
dataValues
array, just use a placeholder for the combo box:You will, of course, need to set the column editor after creating the table:
I highly recommend that you take a look at the How to Use Tables tutorial, if you haven't already. It explains this in greater detail, and includes sample code.