gpt4 book ai didi

java - JTable 在 GUI 中为空,控制台显示已填充的 DefaultTableModel

转载 作者:行者123 更新时间:2023-11-30 06:30:42 25 4
gpt4 key购买 nike

使用 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;
}
}

最佳答案

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

编辑

明确一点,问题是你可能认为

defaultTableModel = MessageUtils.getDataTableModel();

将调整您在 JTable 上设置的模型,但它不会。它只是让您的 defaultTableModel 字段引用另一个 TableModel 然后是原始的。如果您只是将此行移动到构造函数的第一行,您的代码将起作用,因为您实际上将 MessageUtils.getDataTableModel() 的结果设置为 TableModel你的JTable

关于java - JTable 在 GUI 中为空,控制台显示已填充的 DefaultTableModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com