JCombobox containing enum values inside a table
I have a class that contains an Enum with values. (names) In another class, I would like to inject inside the table a JCombobox cell type that will use these enum values. my problem is to concatenate between string values and enum. for example enum class:
enum item_Type {entree, main_Meal, Dessert, Drink}
for example 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 in the dataValues array I cannot use aaa (enum jcombobox). How can i do this?
early.
a source to share
You need to set TableCellEditor to JTable
in order to display the combo box.
TableColumn column = table.getColumnModel().getColumn(2);
column.setCellEditor(new DefaultCellEditor(aaa));
In your array, dataValues
just use a placeholder for the combo box:
String dataValues[][] = {{ "0", "entree", "0" }};
You will of course need to customize the column editor after creating the table:
String dataValues[][] = {{ "0", "entree", "0" }};
JTable table = new JTable(dataValues, columnNames);
TableColumn column = table.getColumnModel().getColumn(2);
column.setCellEditor(new DefaultCellEditor(aaa));
I highly recommend that you take a look at How to use tables if you haven't already. He explains this in more detail and includes sample code.
a source to share