【问题标题】:JXTable date columnJXTable 日期列
【发布时间】:2013-12-10 15:20:32
【问题描述】:

我如何知道该列是否已被我的TableModel 子类覆盖。我想将表列之一设为Date 数据类型并按降序对其进行排序,但我不确定该列的数据类型,因为当我打印它们时它们都会给出输出:

class org.jdesktop.swingx.table.TableColumnExt

这是我的代码:

public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        for (int i = 0; i < jtbSchedule.getColumnCount(true); i++) {
            System.out.println("column " + i + ": " + jtbSchedule.getColumn(i).getClass());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jtbSchedule = new org.jdesktop.swingx.JXTable(new MyTableModel());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jtbSchedule.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jtbSchedule);

        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(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(118, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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 jScrollPane2;
    private org.jdesktop.swingx.JXTable jtbSchedule;
    // End of variables declaration                   

    public class MyTableModel extends DefaultTableModel {

        @Override
        public Class getColumnClass(int col) {
            if (col == 3) {
                return java.util.Date.class;
            } else {
                return super.getColumnClass(col); // return the appropriate class for every column
            }
        }
    }
}

【问题讨论】:

    标签: java swing overriding swingx


    【解决方案1】:

    关于表模型:

    • 第一次使用MyTableModel 实例初始化表。
    • 覆盖模型后的两行设置一个新的DefaultTableModel 实例。

    看看下面的cmets:

      jtbSchedule = new org.jdesktop.swingx.JXTable(new MyTableModel());// Here you set a MyTableModel instance
      ...
      jtbSchedule.setModel(new javax.swing.table.DefaultTableModel(
           new Object [][] {
               {null, null, null, null}
           },
           new String [] {
               "Title 1", "Title 2", "Title 3", "Title 4"
           }
       )); // But here you override the table model setting a DefaultTableModel instance
    

    无论如何这个方法:

    System.out.println("column " + i + ": " + jtbSchedule.getColumn(i).getClass());
    

    它将打印JTable.getColumn()方法返回的TableColumn的类,而不是表模型的列类。应该是:

    for(int i = 0; i < jtbSchedule.getModel().getColumnCount(); i++){
        System.out.println("column " + i + ": " + jtbSchedule.getModel().getColumnClass(i));
    }
    

    【讨论】:

    • 我使用的是 netbeans,这些代码是自动生成的。有什么办法可以解决吗?
    • 您有两个选择:1) 在initComponents() 方法之后的构造函数中设置MyTableModel 实例。 2) 摆脱 GUI 构建器并手工制作 GUI。代码会更简单、更干净,你会学到很多关于 GUI 构建器隐藏给你的 Swing 的东西 :) @YOLO
    • 如何设置模型,我正在粘贴jtbSchedule.setModel(new MyTableModel(...的整块。是否有其他步骤必须提前进行,我想学习使用我自己的数据类型日期创建自己的模型
    • 好的,我找到了这个网站java2s.com/Code/Java/Swing-JFC/TablewithacustomTableModel.htm 并意识到它可以用abstractModel 来实现.. 虽然对表格模型感到困惑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2023-03-07
    • 2014-06-07
    • 2011-12-04
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多