Domanda

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);
        }
    });
}
È stato utile?

Soluzione

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

image

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top