- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 myList 是空的?
从输出中我可以看到 myList 没有正确的大小,但消息有。
输出:
Apr 23, 2012 4:28:42 PM net.bounceme.dur.nntp.MessagesJFrame <init>
INFO: messages 11
Apr 23, 2012 4:28:42 PM net.bounceme.dur.nntp.MessagesJFrame <init>
INFO: myList 0
package net.bounceme.dur.nntp;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.swing.DefaultListModel;
public class MessagesJFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(MessagesJFrame.class.getName());
DefaultListModel<Message> myList = new DefaultListModel<Message>();
public MessagesJFrame() {
initComponents();
EnumNNTP nntp = EnumNNTP.INSTANCE;
List messages = null;
try {
messages = nntp.getMessages(false);
} catch (Exception ex) {
LOG.severe("didn't get messages");
}
myList.copyInto(messages.toArray());
LOG.log(Level.INFO, "messages {0}", messages.size());
LOG.log(Level.INFO, "myList {0}", myList.size());
}
/**
* 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();
jList1 = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jList1.setModel(myList);
jScrollPane2.setViewportView(jList1);
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(13, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(129, 129, 129))
);
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(158, 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(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() {
new MessagesJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration
}
最佳答案
您在哪里将模型添加到 JList?为了使 JList 能够使用和显示模型持有的数据,必须通过 JList 的构造函数或其 setModel(...)
将模型添加到 JList。方法。您是否经历过JList tutorials ?如果没有,那是您应该首先查看的地方,因为它将解释如何使用 JLists,然后是一些。
编辑 1
DefaultListModel copyInto(...)
方法的作用与您认为的相反。它将模型中的数据复制到数组中。
根据 API:
Copies the components of this list into the specified array
关于java - Netbeans,空的 DefaultListModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289678/
这是它: btnInsertL.addActionListener(new ActionListener() { @Override public void actio
我在这里做错了什么?我有一个显示我的列表的 GUI,在另一个类中,我想在单击按钮时添加到列表中。单击按钮时,它会询问名称,但永远不会添加。 //class with GUI public class
我正在尝试扩展 DefaultListModel,以便它可以排序和迭代。我在这里找到了一些代码 http://www.javalobby.org/java/forums/t94074.html .我现
有没有办法将数组中的所有字符串添加到 JList 中?我正在使用 DefaultListModel,但我不知道如何使用它。有没有办法只使用 addElement 然后添加数组?我试过了,但它不起作用。
我正在尝试一种不同的方法只是为了练习目的。我知道这看起来很荒谬。我的目标是在构造函数初始化时检索数字数组。但我做不到。我怎样才能做到这一点? public class Myclass extends
我有一个程序需要更新 JList 的内容,它是另一个线程上的 DefaultListModel。由于内容的数量可能会不时发生变化,所以我只是在更新时清除所有内容并将新内容添加到DefaultListM
为什么 myList 是空的? 从输出中我可以看到 myList 没有正确的大小,但消息有。 输出: Apr 23, 2012 4:28:42 PM net.bounceme.dur.nntp.Mes
使用java版本9我有一些测试代码,用于从通过 DefaultListModel 传递引用创建的列表中删除项目。这就是我所做的。 创建 DefaultListModel 对象 通过调用 addElem
我正在 Eclipse 调试器中查看以下表达式 downsPanel.relsListModel.elementAt(4)==oldValue 和 downsPanel.relsListModel.c
我有这个 DefaultListModel DefaultListModel listModel; //constructor does the right hting... etc.. I skip
我正在使用 Swing 开发一个项目,并且有以下事件处理程序: private void txtInputKeyPressed(java.awt.event.KeyEvent evt) {
我正在尝试在 java 中组合两个 defaultListModel,这样我就可以将该模型设置为 JList。该程序基本上是使用 GUI 在购物车中添加和删除商品。当我按下添加按钮时,它不应该从购物车
这个问题已经有答案了: Java - Updating JList after changing an object (3 个回答) 已关闭 9 年前。 我有一个Jlist与 DefaultListM
我正在尝试显示一个项目列表,当用户单击某个项目时,清除该列表并显示另一个列表。 如果我运行它并单击显示列表上的第一个条目,程序就会因一长串运行时异常而终止。如果我删除clear()行(下面注释),它运
我有一个JList由 DefaultListModel 支持并有一个自定义CellRenderer . CellRenderer用于添加对 JCheckBox 的支持在我的JList . 当我尝试修改
我希望我已经很好地解释了我在这里所追求的内容。我在检索已放入 DefaultListModel 中的对象类型时遇到问题。 public class HiddenIntegerFieldListItem
我有一个 list : JList characterList = new JList(characterListModel); characterListModel = new DefaultLis
我正在研究DefaultListModel。我需要将所选项目移动到列表中的第一个或最后一个(不是上下)位置。如何在 DefaultListModel 中的第一个或最后一个位置移动所选项目而不交换? 最
这是我的代码: HashMap inst1 = new HashMap(instructorIO.getInstructors()); instListModel = new DefaultListM
我创建了一个类来存储两个属性 public class MailEntry { private String mail; private MailFormat format; // t
我是一名优秀的程序员,十分优秀!