2012-04-23 34 views
2

为什么myList是空的?Netbeans,empty DefaultListModel

从输出我可以看到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 

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 
} 
+0

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 – 2012-04-24 00:25:53

+0

啊,Netbeans的GUI搭建器与SSCCE有些不兼容:( – Thufir 2012-04-24 03:35:52

+0

不是,它们只是你理解如何让他们服从你的问题,而不是让'尾巴摇动狗',因为现在看起来就是这样,只需要在代码中定义'List' – 2012-04-24 03:53:00

回答

3

你在哪里的模型添加到JList中?为了使JList能够使用和显示由模型保存的数据,必须通过JList的构造函数或其方法将模型添加到JList中。你有没有通过JList tutorials?如果不是的话,这是你应该看的第一个地方,因为它会解释如何使用JList,然后解释一些。

编辑1
的DefaultListModel copyInto(...)方法做什么,你认为它的对面。它将模型中的数据复制到数组中。

每API:该列表的

复制组件到指定的数组

+0

与setModel(...)不同的是,如何正确填充显示为空的DefaultListModel。 – Thufir 2012-04-24 00:10:48

+0

@Thufir:请参阅编辑回答 – 2012-04-24 00:25:53

+0

ohhhhh,谢谢,对不起,有点愚蠢的问题。你只需迭代一个集合并为每个对象调用'addItem'来添加到DefaultListModel?没有“批量添加”? – Thufir 2012-04-24 00:32:39

0

在最窄意义,下面的代码填充模型:

固定方法:

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的大小相同。

+0

我在这里看不到你的错误的解释。 – 2012-04-24 00:11:33

+0

'INFO:myList 0'在使用'copyInto'时显示为空,但使用上面的代码非空。 – Thufir 2012-04-24 00:20:30

+0

请参阅编辑我的答案 – 2012-04-24 00:26:15