JComboBox in JTable

I have a JComboBox in the 3rd and 4th columns of the JTable, but I don’t know how to get its elements ... the problem is not in the method of getting the elements, but in casting

JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor(); 

Can you help me?

+4
source share
3 answers

JComboBox enclosed in CellEditor . You should get a wrapped component, for example, when using DefaultCellEditor :

 DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor(); JComboBox combo = (JComboBox)editor.getComponent(); 
+5
source

Read this tutorial on how to use JCombobox as an editor in JTable.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

0
source

Try something like this:

  public void example(){ TableColumn tmpColum =table.getColumnModel().getColumn(1); String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" }; JComboBox comboBox = new JComboBox(DATA); DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox); tmpColum.setCellEditor(defaultCellEditor); tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox)); table.repaint(); } /** Custom class for adding elements in the JComboBox. */ class CheckBoxCellRenderer implements TableCellRenderer { JComboBox combo; public CheckBoxCellRenderer(JComboBox comboBox) { this.combo = new JComboBox(); for (int i=0; i<comboBox.getItemCount(); i++){ combo.addItem(comboBox.getItemAt(i)); } } public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) { combo.setSelectedItem(value); return combo; } } 
0
source

All Articles