GUI中的JTable为空,控制台显示填充的DefaultTableModel

时间:2012-04-24 04:54:19

标签: java swing model-view-controller user-interface netbeans

使用Netbeans GUI构建器,是否可能是值/引用问题的同步,线程或传递阻止将DefaultTableModel从实用程序类传递到JFrame?

在任何一种情况下,控制台输出都是相同的,代码只是从实用程序类复制/粘贴并放在JFrame中。但是,如果使用实用程序类,则没有GUI输出。

如何追踪JTable为空或空的原因?据我所知,DefaultTableModel填充了数据。

控制台输出:

init:
Deleting: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
Compiling 1 source file to /home/thufir/NetBeansProjects/SwingNNTP/build/classes
warning: [options] bootstrap class path not set in conjunction with -source 1.5
1 warning
compile:
run:
Apr 23, 2012 9:46:25 PM net.bounceme.dur.nntp.EnumNNTP <init>
INFO: SingletonNNTP..only once...
Apr 23, 2012 9:46:25 PM net.bounceme.dur.nntp.PropertiesReader getProps
INFO: NNTP.loadMessages...
Apr 23, 2012 9:46:25 PM net.bounceme.dur.nntp.EnumNNTP connect
INFO: SingletonNNTP.connect..
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.EnumNNTP setIndex
INFO: SingletonNNTP.setIndex..164
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.EnumNNTP page
INFO: SingletonNNTP.page..174
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.EnumNNTP setIndex
INFO: SingletonNNTP.setIndex..164
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.EnumNNTP getMessages
INFO: SingletonNNTP.getMessages..
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:26 PM net.bounceme.dur.nntp.MessageBean setFrom
INFO: MessageBean.setFrom..unknown <post@gwene.org>
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:24 PDT 2012The return of the euro crisis: Beyond battlefield medicine
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:24 PDT 2012North Korea’s gulag: Never again?
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:24 PDT 2012Nationalising YPF: Cristina scrapes the barrel
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Charlemagne: April showers on the euro
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Jihadist terrorism: Al-Qaeda is down, but far from out
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Justice for dictators: History rules
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Buttonwood: Feeling peaky
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Couples and housework: The ironing lady
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:23 PDT 2012Markets
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:22 PDT 2012The World Bank: Kim selection
Apr 23, 2012 9:46:27 PM net.bounceme.dur.nntp.MessageUtils getDataTableModel
INFO: unknown <post@gwene.org>Sun Apr 22 02:58:22 PDT 2012The Economist commodity-price index
BUILD SUCCESSFUL (total time: 13 seconds)

MessagesJFrame中注释掉的代码实际上有效,而将其传递给实用程序类会引入问题 - 显然是空的JTable。

的JFrame:

package net.bounceme.dur.nntp;

import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.swing.table.DefaultTableModel;

public class MessagesJFrame extends javax.swing.JFrame {

    private DefaultTableModel defaultTableModel = new DefaultTableModel();

    public MessagesJFrame() throws Exception {
        initComponents();
        MessageUtils.loadMessages();
        defaultTableModel = MessageUtils.getDataTableModel();
        /*
        EnumNNTP nntp = EnumNNTP.INSTANCE;
        List<Message> listOfMessages = nntp.getMessages(false);//nntp debug off
        Vector messages = new Vector();
        for (Message m : listOfMessages) {
            MessageBean messageBean = new MessageBean(m);
            messages.add(messageBean);
        }
        defaultTableModel.addColumn("from");
        defaultTableModel.addColumn("sent");
        defaultTableModel.addColumn("subject");
        for (Object o : messages) {  //awkward Vector manipulation
            MessageBean messageBean = (MessageBean) o;
            Vector messageBeanAsVector = messageBean.getVector();
            defaultTableModel.addRow(messageBeanAsVector);
        }*/
    }

    /**
     * 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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(defaultTableModel);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 819, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                .addContainerGap())
        );

        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(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MessagesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MessagesJFrame.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() {
                try {
                    new MessagesJFrame().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(MessagesJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

实用程序类:

package net.bounceme.dur.nntp;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.swing.table.DefaultTableModel;

public class MessageUtils {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(MessageUtils.class.getName());
    private static DefaultTableModel defaultTableModel = new DefaultTableModel();
    private static EnumNNTP nntp = EnumNNTP.INSTANCE;
    private static Vector messages = new Vector();

    public static void loadMessages() throws Exception {
        List<Message> listOfMessages = nntp.getMessages(false);//nntp debug off
        messages.clear();
        for (Message m : listOfMessages) {
            MessageBean messageBean = new MessageBean(m);
            messages.add(messageBean);
        }
        loadTableModel();
    }

    private static void loadTableModel() {
        defaultTableModel = new DefaultTableModel();
        defaultTableModel.addColumn("from");
        defaultTableModel.addColumn("sent");
        defaultTableModel.addColumn("subject");
        for (Object o : messages) {  //awkward Vector manipulation
            MessageBean messageBean = (MessageBean) o;
            Vector messageBeanAsVector = messageBean.getVector();
            defaultTableModel.addRow(messageBeanAsVector);
        }
    }

    public static DefaultTableModel getDataTableModel() {
        Vector vector = defaultTableModel.getDataVector();
        Iterator it = vector.iterator();
        while (it.hasNext()) {
            Vector v = (Vector) it.next();
            Iterator i = v.iterator();
            StringBuilder row = new StringBuilder();
            while (i.hasNext()) {
                row.append(i.next());
            }
            LOG.info(row.toString());
        }
        return defaultTableModel;
    }

    public static void setDataTableModel(DefaultTableModel dataTableModel) {
        MessageUtils.defaultTableModel = dataTableModel;
    }

    public static Vector getMessages() {
        return messages;
    }

    public static void setMessages(Vector messages) {
        MessageUtils.messages = messages;
    }
}

1 个答案:

答案 0 :(得分:5)

当然,你最终会得到一个空的JTable。您有两个名为defaultTableModel的实例,它们没有任何共同之处。您需要将UI中使用的defaultTableModel传递给实用程序类,并让实用程序类填充该特定模型(或者相反,设置实用程序类中使用的defaultTableModel实例作为JTable)的模型。

修改

为了清楚起见,问题是你可能认为

defaultTableModel = MessageUtils.getDataTableModel();

会调整您在JTable上设置的模型,但事实并非如此。只是让您的defaultTableModel字段引用另一个TableModel,然后是原始字段。如果您只是将此行移到构建器的第一行,那么您的代码将起作用,因为您实际上将MessageUtils.getDataTableModel()的结果设置为TableModel的{​​{1}}