首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netbeans,空DefaultListModel

Netbeans,空DefaultListModel
EN

Stack Overflow用户
提问于 2012-04-24 07:26:22
回答 2查看 1.3K关注 0票数 2

为什么myList是空的?

从输出中,我可以看到myList没有正确的大小,而messages却有。

输出:

代码语言:javascript
复制
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

MessagesJFrame:

代码语言:javascript
复制
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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-24 07:32:36

将模型添加到JList的位置?为了使JList能够使用和显示模型持有的数据,必须通过JList的构造函数或setModel(...)方法将模型添加到JList中。你看过JList tutorials了吗?如果没有,这是你应该首先看的地方,因为它将解释如何使用JLists,然后是更多。

编辑1个

DefaultListModel copyInto(...)方法的作用与您认为的相反。它将数据从模型复制到数组中。

根据API:

将此列表的组件复制到指定的数组

票数 3
EN

Stack Overflow用户

发布于 2012-04-24 08:02:34

在最狭义的意义上,以下代码填充模型:

固定方法:

代码语言:javascript
复制
public MessagesJFrame() {
    initComponents();
    EnumNNTP nntp = EnumNNTP.INSTANCE;
    List<Message> messages = null;
    try {
        messages = nntp.getMessages(false);
    } catch (Exception ex) {
        LOG.severe("didn't get messages");
    }
    for (Message m : messages) {
        myList.addElement(m);
    }
    LOG.log(Level.INFO, "messages {0}", messages.size());
    LOG.log(Level.INFO, "myList {0}", myList.size());
}

然而,myList.copyInto(messages.toArray());有什么问题呢?

上面的代码输出相同大小的消息和myList。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10289678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档