数据不会显示在JList中

时间:2013-12-18 12:52:35

标签: java jpanel jlist

我正在尝试使用DefaultListModel在JList中显示JPanel。一切都很好,所以我有点不确定什么是错的。它实际上在某个时候工作,但我必须在一个更大的项目中实现它,现在它停止工作。问题很可能出现在Jp_Diskussion中,但我找不到它......

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 个答案:

答案 0 :(得分: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));
     }

}