JXTable date column

123 views Asked by At

How do I know whether the column has been override with my TableModel subclass. I want to make one of the table column to be Date data type and sort it out descendingly but I am unsure about the data type of the column since when I print them out they all give output:

class org.jdesktop.swingx.table.TableColumnExt

This is my code:

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

There are 1 answers

4
dic19 On BEST ANSWER

About the table models:

  • First time you initizialize the table with a MyTableModel instance.
  • Two lines after you override the model setting a new DefaultTableModel instance.

Take a look to the comments below:

  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

In any case this method:

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

It will print the class of the TableColumn returned by JTable.getColumn() method, not the column class of the table model. It should be:

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