在jtable的自定义选项中编辑

时间:2014-03-09 19:28:50

标签: java swing exception-handling jtable tablecelleditor

在使用netbeans中的“自定义代码”选项更改jtable中的一些代码后,主要方法会像“非法表达”一样使用错误。

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(Accept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Accept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Accept.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Accept.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 Accept().setVisible(true);
        }
    });
}

这里发生了什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

首先创建自己的Cell Editor

public class MyCellEditor extends DefaultCellEditor{

    public MyCellEditor() {
        super(new JTextField());
    }

    @Override
    public void setClickCountToStart(int count) {
        // TODO Auto-generated method stub
        super.setClickCountToStart(1);
    }   
}

然后将MyCellEditor分配给您的表,就像:

table.setDefaultEditor(Object.class, new MyCellEditor());

希望有所帮助。