0

I have a JTable with 6 columns. for column 0 and 1 i have created a comboBox as editor. I want all the cells in the same row be edited when user select an item from combobox of column 0.

Does any one know if it is the best way or is there other way much better?

JTable table = new JTable();// a table with 6 columns
TableColumn column0 = tabel.getColumnModel().getColumn(0);
comboBox = new JComboBox(summary.getGenerics());
column0.setCellEditor(new DefaultCellEditor(comboBox));
column0.setCellRenderer(new DefaultTableCellRenderer());
comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
              String selectedItem = comboBox.getSelectedItem().toString();
               table.setValueAt("class" , table.getSelectedRow() , 1);

            }
        });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
michdraft
  • 556
  • 3
  • 11
  • 31

1 Answers1

2

This example overrides getValueAt() to condition the value returned by a dependent column based on the selection in a column having a JComboBox editor. Any dependent column should be non-editable, and any TableModelListener should be notified, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045