1

My problem is that I want to have a itemSateChanged listener on a combo box which is inside a jTable. When I change the values of the combo box I want data added to a cell in the respected row of the jTable.

This is the code where the combo box is created.

public void setUpSectionColumn(JTable table, TableColumn statusColumn) {
    //Set up the editor for the sport cells.
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Lending");
    comboBox.addItem("Childen Lending");
    comboBox.addItem("Reference");
    comboBox.addItem("Special Collection");

    statusColumn.setCellEditor(new DefaultCellEditor(comboBox));

    //Set up tool tips for the sport cells.
    DefaultTableCellRenderer renderer =
            new DefaultTableCellRenderer();
    renderer.setToolTipText("Click for combo box");
    statusColumn.setCellRenderer(renderer);
}

This is the method I have come up with for the listener which does not work.

public void fillTable2() {
    jTable2.getModel().addTableModelListener(new TableModelListener() {
        public void tableChanged(TableModelEvent evt) {
            int row = jTable2.getSelectedRow();
            String section;
            if (row == -1) {
                section = "Lending";
            } else {
                section = jTable2.getValueAt(row, 3).toString();
            }
            int accessNo = bdao.getLastAccessNo(section);
            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
            Object[] rowdata = {Integer.toString(accessNo), "", ""};
            model.addRow(rowdata);
        }
    });
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Gayan Jayasingha
  • 752
  • 2
  • 17
  • 33
  • yes is possible, but in most cases isn't there some reason to override JComboBox and with ItemListener, I'd be override setValueAt in DefaultTableModel, generate value for concrete row – mKorbel Sep 23 '13 at 21:05

1 Answers1

2

As discussed here, you can use a JComboBox as a TableCellEditor. There's an example here using DefaultCellEditor.

image

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