简体   繁体   中英

Firing itemStateChanged event on Combo box inside jTable

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);
        }
    });
}

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

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM