4

javax.swing.JTable には、null 値のセルの編集中にテーブルをソートするとバグがあり、その列クラスには「new Object[] { new String() }」パラメーターを持つコンストラクターがありません。Long.class、JTable は例外をスローします。

javax.swing.JTable がセル エディタを 2 回削除しようとするためだと思います。そこで、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 をよく知っており、このような私のサブクラスを好まないかもしれません。より良い解決策はありますか?どうもありがとう。

次のコードを実行する場合、1 つのセルをダブルクリックし (何も入力しないでください)、ヘッダーをクリックすると、例外が表示されます。

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
}
4

3 に答える 3

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, wheresuper.stopCellEditingを入力します。この呼び出しの戻り値は無視されます。次に、newInstance呼び出しは例外をスローします。例外はキャッチfalseされますが、戻り値が何であっても返されますsuper.stopCellEditing。これは私には正しくないように思えます。あなたが提供したコードでバグをOracleに記録します

于 2012-11-23T07:43:43.130 に答える
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();
            }
        });
    }
}
于 2012-11-23T07:39:29.567 に答える
1

この問題 - @Robin によって正しく分析された - には、いくつかの側面があります。

  • false を返す場合でも、stopCellEditing が誤って editStopped を起動する
  • 空の値がコンストラクターによって受け入れられた場合、stopCellEditing が 2 回発生する

2 つ目は契約に「ただ」違反しているのに対し、1 つ目は深刻な結果をもたらします。

  • OPの質問に記載されているように、クライアントコードが編集を実際に終了しようとするとNPEをスローします。つまり、最初に停止し、失敗するとキャンセルされます。皮肉なことに、最も目に見える外観は jdk7 table.columnMoved にあります。以前 (jdk6 まで) の不適切な removeEditor は、Right-Thing に置き換えられています。
  • 編集中に削除された有効な値で編集が開始された場合、データの破損につながる可能性があります。editingStopped イベントは、テーブルをトリガーして、tableModel の有効な値を null に置き換えます ...

GenericEditor のさまざまなロジックによって JXTable を修正する十分な理由があります (修正はカスタム GenericEditor でも同様に使用できます。swingx との結合はありません: GenericEditor のコアを c&p し、その 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();
}
于 2012-12-05T16:55:25.527 に答える