2013-12-18 91 views
1

我試圖在DefaultListModel的JList中顯示JPanel。一切都很好,所以我不確定哪裏出了問題。它實際上在某個時間點工作,但我必須在一個更大的項目中實現它,現在它停止工作。問題是最有可能在某處Jp_Diskussion,但我無法找到它...數據不會顯示在JList中

public class Jp_Diskussion extends JPanel { 
    private Jd_BeitragErstellen jd_beitr_erst; 
    private Jd_BeitragBearbeiten jd_beitr_bearb; 
    private JFrame parentFrame = (JFrame) this.getParent(); 
    private JComboBox comboBoxMA; 
    private JComboBox comboBoxTag; 
    private JButton btnFiltern; 
    private JLabel labelFiltern; 
    private JLabel labelOpt; 
    private JButton btnBeitragVerfassen; 
    private JScrollPane messageScroll; 

public Jp_Diskussion() { 
    createMessageList();  
} 

private void createMessageList() { 
    setBounds(new Rectangle(0, 0, 994, 624)); 
    setLayout(null); 
    add(getJScrollPane()); 
    add(getComboBoxTag()); 
    add(getComboBox_2()); 
    add(getBtnFiltern()); 
    add(getLabelFiltern()); 
    add(getLabelOpt()); 
    add(getBtnBeitragVerfassen()); 

} 

private JScrollPane getJScrollPane() { 
    if (messageScroll == null) { 

     MessageList m = new MessageList(); 

     m.getMessage("NAME AUTOR", "<html><body style='width: 690px;'>Lorem ipsum dolor sit amet.", "22.01.13", "SOA"); 
     m.getMessage("AUTOR NAME", "<html><body style='width: 690px;'>Lorem ipsum dolor sit amet.", "22.01.13", "SOA"); 

     messageScroll = new JScrollPane(m); 
     messageScroll.setBounds(20, 20, 954, 461);   
    } 
    return messageScroll; 
} 

private JButton getBtnBeitragVerfassen() { 

private JLabel getLabelFiltern() { 

private JLabel getLabelOpt() { 

private JComboBox getComboBoxTag() { 
private JComboBox getComboBox_2() { 
private JButton getBtnFiltern() { 
} 

JList的:

public class MessageList extends JList{ 

DefaultListModel messageModel = new DefaultListModel(); 
    JList messageList = new JList(messageModel); 
    MessageRenderer messageRenderer = new MessageRenderer(); 

    public MessageList(){ 
     messageList.setCellRenderer(messageRenderer); 

    } 

    public void getMessage(String author, String text, String date, String tag){ 
     messageModel.addElement(new Message(author, text, date, tag)); 
    } 

} 

Message類:

public class Message extends JPanel{ 

    JLabel lblAuthor = new JLabel(); 
    JLabel lblText = new JLabel(); 
    JLabel lblDate = new JLabel(); 
    JLabel lblTag = new JLabel(); 

public Message(String author, String text, String date, String tag){ 

    lblAuthor.setText(author); 
    lblText.setText(text); 
    lblDate.setText(date); 
    lblTag.setText(tag); 

    add(lblAuthor,BorderLayout.NORTH); 
    add(lblText,BorderLayout.WEST); 
    add(lblDate,BorderLayout.SOUTH); 
    add(lblTag,BorderLayout.EAST); 

    GroupLayout groupLayout = new GroupLayout(this); 

    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup() 
    .addGroup(groupLayout.createParallelGroup()  
    .addComponent(lblAuthor) 
    .addComponent(lblText) 
    .addComponent(lblTag)) 
    .addComponent(lblDate)) 
    ; 
    groupLayout.setVerticalGroup(groupLayout.createSequentialGroup() 
    .addGroup(groupLayout.createParallelGroup()  
    .addComponent(lblAuthor) 
    .addComponent(lblDate)) 
    .addComponent(lblText) 
    .addComponent(lblTag)) 
    ; 

    this.setLayout(groupLayout); 

} 
} 

我的渲​​染器:

public class MessageRenderer implements ListCellRenderer { 
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    Component component = (Component)value; 

    if (isSelected) { 
     component.setBackground(Color.RED); 
    } else { 
     component.setBackground(Color.GREEN); 
    } 
    return component; 
} 
} 
+1

良好的命名約定。 'private void createMessageList()'做了什麼?我想你可以調用'init ___ something'來代替。 – Thufir

+1

'JList messageList = new JList(messageModel);'因爲您剛聲明'public class MessageList extends JList',所以存在問題。你不需要'messageList'字段。嘗試完全刪除該變量。在你的'MessageList'類裏面,你可以通過做一些類似的事情(不確定)來引用默認的模型,this.getDefaultModel會返回一個很明顯的JList類型的默認模型, *。 – Thufir

+0

好的,就是這樣,我已經添加了解決方案作爲答案。 – cflx

回答

1
public class MessageList extends JList{ 

DefaultListModel messageModel = new DefaultListModel(); 

    MessageRenderer messageRenderer = new MessageRenderer(); 

    public MessageList(){ 
     this.setCellRenderer(messageRenderer); 
     this.setModel(messageModel); 

    } 

    public void getMessage(String author, String text, String date, String tag){ 
     messageModel.addElement(new Message(author, text, date, tag)); 
    } 

} 
+1

看起來不錯。因爲'MessageList'擴展了'JList'它帶有'DefaultListModel',所以你不需要聲明一個。相反,嘗試(類似於)'this.getDefaultModel'將爲您返回一個'DefaultListModel'。我正在爲你寫一個徹底的答案! LOL – Thufir

+0

感謝所有的努力;-) – cflx

+0

沒有問題:)良好的問題。 – Thufir