验证setModel()?

时间:2016-10-19 17:47:10

标签: java swing

在Netbeans中,我有这个代码来填充JTable。

public void fillTable() {
    String col[] = {"Username", "Mail", "Birthday"};

    DefaultTableModel tableModel = new DefaultTableModel(col, 0);

    for (User u : this.uc.getL()) {
        String birthday = new SimpleDateFormat("dd/MM/yyyy").format(u.getBirthday());
        Object[] data = {u.getUsername(), u.getMail(), birthday};
        tableModel.addRow(data);
    }
    this.userTable.setModel(tableModel);
}

在四处寻找有关此问题的任何指导之后,我仍然有这张空白表格。

我可能做错了什么?

这里是主要的课程

package javaapplication5;

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class JavaApplication5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Frame.main(args);

}

}

这里是JTable的框架。

package javaapplication5;

import java.awt.BorderLayout;
import javax.swing.JLabel;  
import javax.swing.JScrollPane;  
import javax.swing.JTable;  
import javax.swing.table.DefaultTableModel;  

public class Frame extends javax.swing.JFrame {

/**
 * Creates new form Frame
 */
public Frame() {
    this.fillTable();
    initComponents();
}

public void fillTable() {
    String col[] = {"Username", "Mail", "Birthday"};

    DefaultTableModel tableModel = new DefaultTableModel(col, 0);

    Object[] data = {"MrTest", "Mr@Test.com", "22/10/1985"};
    tableModel.addRow(data);
    Object[] data2 = {"MrsTest", "Mrs@Test.com", "13/05/1987"};
    tableModel.addRow(data2);
    userTable = new JTable(tableModel);
    this.jsUser = new JScrollPane(userTable);
    this.jsUser.setVisible(true);
    this.userTable.setVisible(true);
    userTable.setModel(tableModel);
}


// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    lUser = new javax.swing.JLabel();
    jsUser = new javax.swing.JScrollPane();
    userTable = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    lUser.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
    lUser.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lUser.setText("test Jtable");
    getContentPane().add(lUser, java.awt.BorderLayout.NORTH);

    userTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Username", "Mail", "Birthday"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jsUser.setViewportView(userTable);

    getContentPane().add(jsUser, java.awt.BorderLayout.CENTER);

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

// Variables declaration - do not modify                     
private javax.swing.JScrollPane jsUser;
private javax.swing.JLabel lUser;
private javax.swing.JTable userTable;
// End of variables declaration                   

}

解决方案:

对于那些寻求理解的人,我想说我解决了我的问题。

这个部分分为两部分:模型的来源和模型本身的存在。

当我理解Netbeans有特殊性时,第一个问题就解决了。实际上,GUI生成了一个可以修改的模型。但要使用它,你应该在头部创建DefaultTableModel。所以我的功能现在看起来像这样。

  public void fillTable() {

    DefaultTableModel tableModel = (DefaultTableModel) userTable.getModel();

    for (User u : this.uc.getL()) {
        String birthday = new SimpleDateFormat("dd/MM/yyyy").format(u.getBirthday());

        tableModel.addRow(new Object[]{u.getUsername(), u.getMail(), birthday});
    }
}

第二部分更简单。我测试了表的列的存在。没有。在那里我理解另一件事:表应该在函数initComponents中完成,然后填充它的函数应该在之后完成,如下所示:

  public userAdmin() {
    this.userTable = new JTable();
    this.uc = new userController();
    this.uc.setuA(this);
    initComponents();
    this.fillTable();
}

现在可行。

0 个答案:

没有答案