使用 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。
框架:
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;
}
}