简体   繁体   中英

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

Don't use an ItemListener on the combo box.

Instead you should be using a TableModelListener . An event will be fired whenever the data in the TableModel is changed. So you add the TableModelListener to the TableModel of your JTable.

The TableModelEvent will give your row/column of the cell that changed. You can get the changed value from the TableModel.

Or maybe you would want to use a Table Cell Listener which is similar to the TableModelListener except the code is only invoked when the value is actually changed and you use an Action to do the processing.

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/

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