0

I am trying to add two jcombobox in two row cells using DefaultTableModel addrow() method. Like this:

DefaultTableModel dtm = new DefaultTableModel();
JComboBox jcb1 = new JComboBox;
JComboBox jcb2 = new JComboBox;
JComboBox[] row={jcb1,jcb2};
dtm.addRow(row);
myTable.setModel(dtm);

It happens that the table takes the whole thing and displays into each cell the JComboBox.toString, the properties of each JComboBox instead of displaying the combobox itself the object. Can anyone help me? Thanks

Crofuncio
  • 65
  • 1
  • 4

1 Answers1

1

To set the combo box as an editor on a column, you have to get the column using:

TableColumn comboColumn = table.getColumnModel().getColumn(1);

Instead of 1, put the actual column index (0 is for the first column, 1 is for the second etc.). And then:

comboColumn.setCellEditor(new DefaultCellEditor(comboBox));

Also, consider creating your own class that will extend AbstractTableModel and then using your class as table model instead of DefaultTableModel - maybe you don't have to do this, but it will give you more flexibility with defining behavior of jTable.

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38