How do I get the CellRow when there is an ItemEvent in the JComboBox within the cell

后端 未结 1 1505
挽巷
挽巷 2020-11-22 05:02

I have a JTable with a column containing a JComboBox.

I have an ItemListener attached to the JComboBox which acts upon any changes.

However, ItemListener doe

相关标签:
1条回答
  • 2020-11-22 05:48

    It sounds like you are Using a Combo Box as an Editor. If so, the TableCellEditor method, getTableCellEditorComponent(), includes the row as a parameter. There's a related example here.

    Addendum: To change a value in the same row you've edited, just have the model return the correct value for the "other column" based on the related values in that row. Alternatively, update the related value in your model's setValueAt() method before firing the update, as shown in the example.

    Addendum: Based on your example, the code below overrides the model's getValueAt() method to keep the dependent column synchronized with the item column.

    enter image description here

    import java.awt.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.io.IOException;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    
    /** @see http://stackoverflow.com/questions/7350445 */
    public class DependentColumn extends JFrame {
    
        private static final int DEPENDENT_COL = 1;
        private static final int ITEM_COL = 2;
        private static final String[] columnNames = {"Col 1", "Col 2", "Col 3"};
    
        public static void main(String args[]) throws IOException {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    DependentColumn dc = new DependentColumn();
                }
            });
        }
    
        public DependentColumn() {
            this.setTitle("Example");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create Model & Table
            DefaultTableModel model = new DefaultTableModel(columnNames, 0) {
    
                @Override
                public Object getValueAt(int row, int col) {
                    if (col == DEPENDENT_COL) {
                        return "C2:" + this.getValueAt(row, ITEM_COL);
                    } else {
                        return super.getValueAt(row, col);
                    }
                }
            };
            for (int i = 0; i < 16; i++) {
                model.addRow(new Object[]{"C1", "C2", "Item1"});
            }
            JTable table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(320, 120));
    
            //Create ComboBox
            String[] items = {"Item1", "Item2", "Item3"};
            JComboBox combo = new JComboBox(items);
            TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
            col.setCellEditor(new DefaultCellEditor(combo));
            combo.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        System.out.println(e.getItem() + " selected");
                    }
                }
            });
    
            this.add(new JScrollPane(table));
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题