java - Netbeans,空 DefaultListModel

标签 java swing user-interface netbeans jlist

为什么 myList 是空的?

从输出中我可以看到 myList 的大小不正确,但 messages 的大小却正确。

输出:

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:

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吗? ?如果没有,这是您应该首先查看的地方,因为它将解释如何使用 JList 以及其他一些内容。

编辑 1
DefaultListModel copyInto(...) 方法的作用与您想象的相反。它将数据从模型复制到数组中。

根据 API:

Copies the components of this list into the specified array

关于java - Netbeans,空 DefaultListModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289678/

相关文章:

java - JTabbedPane - 选项卡更改后聚焦最后一个聚焦组件

php - 缓存页面资源

user-interface - Xamarin 表单 : How display activityindicator instantly while in async task?

java - 如何将控制台中的输出记录到文件中?

java - 如何将二维数组的一行或一列传递给java中的函数

java - JDialog 大小未渲染

java - 使用Swing,我想画几个点

java - Shaker排序的条形演示一次对所有条形进行排序-Java

java - ..... Spring Boot中构造函数的参数0

java - 如何从公历字符串中获取日期?