JList没有响应listModel.addElement(

时间:2012-12-16 20:45:57

标签: java swing jlist defaultlistmodel

我的Java Swing应用程序中有一个JList,当用户单击一个按钮时,列表清除,内容重置如下:

public void reset(ArrayList<String> content) {
    listModel.removeAllElements();
    System.out.println(content.size());
    for(int i = 0; i < content.size(); i++) {
       listModel.addElement(content.get(i));
       System.out.println("Added element " + content.get(i));
   }
}

列表初始化如下

listModel = new DefaultListModel();
list = new JList(listModel);

但是有一个问题。清单清除(在重置之前,还有其他内容。此内容确实消失了),但新内容未显示。而且,从输出中,我可以看到已添加了6个元素。但它们没有出现在列表中。这是为什么?

1 个答案:

答案 0 :(得分:2)

  

但是有一个问题。列表清除(重置之前,那里   是其他内容。这个内容确实消失了),但新的内容   没有出现。而且,从输出中,我可以看到6个元素   已被添加。但它们没有出现在列表中。这是为什么?

没有人知道,只有Concurency in Swing可能会出现问题,请确保所有事件都是在EDT上进行的,否则可以在SSCCE后更快地获得更好的帮助,例如

编辑

添加了@Robin

请求的String实例
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ListString extends JFrame {

    private static final long serialVersionUID = 1L;
    private DefaultListModel model = new DefaultListModel();
    private int i = 01;

    public ListString() {
        model.addElement(("one" + i++));
        model.addElement(("two" + i++));
        model.addElement(("three" + i++));
        model.addElement(("four" + i++));
        JList list = new JList(model);
        add(new JScrollPane(list));
        JButton btn = new JButton("Remove All Rows :");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                model.removeAllElements();
            }
        });
        add(btn, BorderLayout.SOUTH);
        JButton btn1 = new JButton("Add New Rows:");
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                model.addElement(("one" + i++));
                model.addElement(("two" + i++));
                model.addElement(("three" + i++));
                model.addElement(("four" + i++));
            }
        });
        add(btn1, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
        ListString frame = new ListString();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

在@trashgod

请求的JScrollPane中渲染JPanel
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ListPanel extends JFrame {

    private static final long serialVersionUID = 1L;
    private DefaultListModel model = new DefaultListModel();
    private int i = 01;

    public ListPanel() {
        model.addElement(createPanel("one" + i++));
        model.addElement(createPanel("two" + i++));
        model.addElement(createPanel("three" + i++));
        model.addElement(createPanel("four" + i++));
        JList list = new JList(model);
        list.setCellRenderer(new PanelRenderer());
        add(new JScrollPane(list));
        JButton btn = new JButton("Remove All Rows :");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                model.removeAllElements();
            }
        });
        add(btn, BorderLayout.SOUTH);
        JButton btn1 = new JButton("Add New Rows:");
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                model.addElement(createPanel("one" + i++));
                model.addElement(createPanel("two" + i++));
                model.addElement(createPanel("three" + i++));
                model.addElement(createPanel("four" + i++));
            }
        });
        add(btn1, BorderLayout.NORTH);
    }

    public static JPanel createPanel(String text) {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Item: "));
        panel.add(new JLabel(text));
        return panel;
    }

    public static void main(String[] args) {
        UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
        ListPanel frame = new ListPanel();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private class PanelRenderer implements ListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JPanel renderer = (JPanel) value;
            renderer.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
            return renderer;
        }
    }
}