Почему itemStateChanged вызывается только один раз, когда я перебираю столбцы TableCellEditor в каждой строке?

Я использую пользовательский CellEditor в столбце

ОБНОВЛЕНИЕ: добавлен SSCCE

public class TestJFrame extends javax.swing.JFrame {

    public TestJFrame() {
        initComponents();
        jTable1.getTableHeader().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(final MouseEvent e) {
                final JTableHeader tableHeader = (JTableHeader) e.getSource();
                final JTable table = tableHeader.getTable();
                final int column = tableHeader.columnAtPoint(e.getPoint());
                if (column == 1) {
                    TableCellEditor editor = table.getColumnModel()
                                                  .getColumn(column)
                                                  .getCellEditor();
                    for (int row = 0; row < table.getModel().getRowCount(); row++) {
                        if (table.getModel().isCellEditable(row, column)) {
                            editor.getTableCellEditorComponent(table, (e.getButton() == MouseEvent.BUTTON1),
                                                               false,
                                                               row,
                                                               column);
                            tableHeader.getTable().setValueAt((e.getButton() == MouseEvent.BUTTON1),
                                                              row,
                                                              column);
                        }
                    }
                }
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "name", "click me"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setResizable(false);
            jTable1.getColumnModel().getColumn(0).setPreferredWidth(100);
            jTable1.getColumnModel().getColumn(1).setCellEditor(new SwitchCellEditor(new EnableCheckbox(jTable1), jTable1));
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

    private static class EnableCheckbox extends JCheckBox {

        public EnableCheckbox(final JTable table) {
            addItemListener(new EnableCheckboxItemListenerImpl(table));
    }

        private final class EnableCheckboxItemListenerImpl implements ItemListener {

            private final JTable _table;

            public EnableCheckboxItemListenerImpl(final JTable table) {
                _table = table;

            }

            @Override
            public void itemStateChanged(ItemEvent e) {
                final JCheckBox checkBox = (JCheckBox) e.getSource();
                final int selectedRow = _table.rowAtPoint(checkBox.getLocation());
                final int selectedColumn = _table.columnAtPoint(checkBox.getLocation());

                // doStuff                
            }
        }
    }

    private class SwitchCellEditor extends DefaultCellEditor {

        private final JTable _table;
        private final JCheckBox _checkBox;

        public SwitchCellEditor(final JCheckBox checkBox, final JTable table) {
            super(checkBox);
            _checkBox = checkBox;
            _table = table;
        }

        @Override
        public final Component getTableCellEditorComponent(
                final JTable table,
                final Object value,
                final boolean isSelected,
                final int row,
                final int column) {

            Rectangle r = _table.getCellRect(row, column, false);

            if (_checkBox != null) {
                _checkBox.setLocation(new Point(r.x, r.y + r.height));
                return super.getTableCellEditorComponent(table, value, isSelected, row, column);
            }

            return super.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
    }
}

Метод itemStateChanged вызывается только на первом шаге итерации, но не на следующих шагах итерации.

Почему он так себя ведет и как заставить его вызывать itemStateChanged на каждом шаге итерации?


person mrt181    schedule 13.06.2014    source источник
comment
1. зависит от значения, хранящегося в XxxTableModel, 2. XxxListener не требуется, не вижу причин для уведомления об облаках в небе, 3. JTable/XxxTableModel знает JCheckBox как компонент рендеринга/редактора, 4. пожалуйста, в чем причина для редактора в JTableHeader (неподходящее место для редактора) и в этой форме, несколько раз здесь и без побочных эффектов, 5. убедитесь, что 99,99% читателей не понимают ни код, ни описание, 6. для лучшей помощи скорее опубликуйте SSCCE/MCVE, короткий, исполняемый, компилируемый с жестко заданным значением в локальной переменной для JTable/XxxTableModel   -  person mKorbel    schedule 13.06.2014
comment
Когда я нажимаю на TableHeader, я хочу установить/снять все флажки в строках.   -  person mrt181    schedule 13.06.2014
comment
(установить/снять все флажки в строках) TableHeader или RowHeader ???   -  person mKorbel    schedule 13.06.2014
comment
Для примера.   -  person trashgod    schedule 13.06.2014
comment
Изменен код на SSCCE   -  person mrt181    schedule 13.06.2014
comment
@trashgod ваша реализация устанавливает значение в таблице всякий раз, когда itemStateChanged вызывается для заголовка, но не вызывает itemStateChanged для каждого редактора в каждой строке.   -  person mrt181    schedule 13.06.2014


Ответы (1)


Ваша реализация... не вызывает itemStateChanged для каждого редактора в каждой строке.

Точно. JTable использует шаблон легковеса для редакторов, обновляя TableModel только после завершения редактора. Если вы хотите увидеть изменение состояния редактора, вы можете либо

  • Используйте подход из приведенного здесь примера, в котором редактор прослушивает JCheckBox, используемый соответствующим средством визуализации.

  • Добавьте CellEditorListener, упомянутый здесь.

person trashgod    schedule 13.06.2014