2013-12-10 26 views
0

如何知道列是否已被我的TableModel子類覆蓋。我想使表列的一個是Date數據類型和它遞減理清,但我不確定列的數據類型,因爲當我打印出來,他們都給予輸出:JXTable日期列

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

回答

2

關於錶款:

  • 你第一次initizialize表與MyTableModel例。
  • 覆蓋模型後,兩行代碼設置新的DefaultTableModel實例。

看看下面的評論:

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()); 

它將打印類TableColumnJTable.getColumn()方法返回,而不是列表模型的類。它應該是:

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

我正在使用netbeans並且這些代碼是自動生成的。 有什麼辦法可以解決嗎? –

+1

有兩種選擇:1)在'initComponents()'方法之後的構造函數中設置MyTableModel實例。 2)擺脫GUI構建器,手工製作GUI。代碼將更簡單和更清晰,並且您將學習許多關於Swing GUI構建器隱藏給你的東西:) @YOLO – dic19

+0

我該如何設置模型,我正在粘貼'jtbSchedule.setModel(new MyTableModel( ...'。有沒有其他步驟必須提前,我想學習使用我自己的數據類型創建我自己的模型日期 –