JTable.columnMoved方法的错误

时间:2012-11-23 07:09:05

标签: java swing jtable tablecelleditor

javax.swing.JTable有一个bug, 如果我们在编辑空值单元格时对表进行排序, 并且其列类没有带有“new Object [] {new String()}”参数的构造函数,例如。 Long.class, JTable将抛出异常。

我认为因为javax.swing.JTable尝试删除单元格编辑器两次; 所以我打算将JTable.columnMoved方法覆盖为:

@Override
public void columnMoved(TableColumnModelEvent e) {
    if (isEditing() && !getCellEditor().stopCellEditing()) {
        if(getCellEditor() != null) { // In javax.swing.JTable, no this check point
            getCellEditor().cancelCellEditing();
        }
    }
    repaint();
}

我觉得这还不够好,因为它对代码阅读器不友好,他们可能很了解JTable,并且不喜欢我这样的子类。 有更好的解决方案吗? 非常感谢。

运行以下代码时,双击一个单元格(不输入任何内容),然后单击标题,将显示异常。

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    @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},
                {null, null}
            },
            new String [] {
                "Title 1", "Title 2"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.Long.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

3 个答案:

答案 0 :(得分:4)

我可以使用您提供的代码重现该问题。我责怪JTable#stopCellEditing实施

    public boolean stopCellEditing() {
        String s = (String)super.getCellEditorValue();
        // Here we are dealing with the case where a user
        // has deleted the string value in a cell, possibly
        // after a failed validation. Return null, so that
        // they have the option to replace the value with
        // null or use escape to restore the original.
        // For Strings, return "" for backward compatibility.
        if ("".equals(s)) {
            if (constructor.getDeclaringClass() == String.class) {
                value = s;
            }
            super.stopCellEditing();
        }

        try {
            value = constructor.newInstance(new Object[]{s});
        }
        catch (Exception e) {
            ((JComponent)getComponent()).setBorder(new LineBorder(Color.red));
            return false;
        }
        return super.stopCellEditing();
    }

您输入第一个if,其中super.stopCellEditing被调用。忽略此调用的返回值。然后,newInstance调用抛出异常,该异常被捕获,但无论返回值是false,都会返回super.stopCellEditing。这对我来说听起来不对。我会使用您提供的代码将Oracle的错误记录下来

答案 1 :(得分:3)

我看不出任何问题,任何错误,TableCellEditor在排序和列重新排序时被正确取消,

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TableWithTimer {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private JScrollPane scroll = new JScrollPane();
    private JTable myTable;
    private String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
    private Object[][] data = {{null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null}};
    private DefaultTableModel model;

    public TableWithTimer() {
        model = new DefaultTableModel(data, head) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class<?> getColumnClass(int colNum) {
                switch (colNum) {
                    case 0:
                        return Integer.class;
                    case 1:
                        return Double.class;
                    case 2:
                        return Long.class;
                    case 3:
                        return Boolean.class;
                    default:
                        return String.class;
                }
            }
        };
        myTable = new JTable(model);
        myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        myTable.setGridColor(Color.gray);
        myTable.setFillsViewportHeight(true);
        myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        myTable.setAutoCreateRowSorter(true);
        scroll.setViewportView(myTable);
        frame.add(scroll, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableWithTimer tableWithTimer = new TableWithTimer();
            }
        });
    }
}

答案 2 :(得分:1)

问题 - 由@Robin正确分析 - 有几个方面:

  • stopCellEditing错误地触发了editStopped,即使返回false
  • 也是如此
  • 如果构造函数
  • 接受空值,则stopCellEditing触发两次

虽然第二个“只是”违反合同,但第一个会产生严重后果:

  • 如OP的问题所述:如果客户端代码试图真正终止编辑,则抛出NPE,即第一次停止和失败取消。具有讽刺意味的是,最明显的外观是在jdk7 table.columnMoved中 - 以前(最多jdk6)不正确的removeEditor被Right-Thing取代。
  • 如果编辑以编辑时删除的有效值开始编辑,则
  • 可能导致数据损坏:editingStopped事件触发表将tableModel中的有效值替换为null ...

有足够的理由通过GenericEditor中的不同逻辑修复JXTable(该修复也可用于自定义GenericEditor,不与swingx耦合:只需c&amp; p核心GenericEditor并用以下内容替换其stopCellEditing):

@Override
public boolean stopCellEditing() {
    String s = (String) super.getCellEditorValue();
    // JW: changed logic to hack around (core!) Issue #1535-swingx
    // don't special case empty, but string contructor:
    // if so, by-pass reflection altogether
    if (constructor.getDeclaringClass() == String.class) {
        value = s;
    } else { // try instantiating a new Object with the string 
        try {
            value = constructor.newInstance(new Object[] { s });
        } catch (Exception e) {
            ((JComponent) getComponent()).setBorder(new LineBorder(
                    Color.red));
            return false;
        }
    }
    return super.stopCellEditing();
}