2012-04-23 54 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