Search code examples
javaswingjtableitemlistener

getSelectedRow on a combobox cell editor


I need a listener on a CombobBox which is a cellEditor on a JTable. This listener must give me the new selected value and the row id.

Problem with my below solution is that the listner is linked to all rows, so when I change one ComboBox value in one row, then move to another row (with a different combo value) an event is raised, but the selected row has not yet changed. How can I get rid of this case ?

Thanks

column = jTableCheck.getColumnModel().getColumn(9);
JComboBox comboBox = new JComboBox(comboGenre);
    comboBox.addItemListener(new ItemListener(){
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                int row = jTableCheck.getSelectedRow();
                Popup.info(e.getItem() + " SELECTED, row="+row);
            }
        }
    });
column.setCellEditor(new DefaultCellEditor(comboBox));

Solution

  • In fact, I already used a TableCellListener on another table, but forgot about that!

    I found out a usefull class here: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/