2

我的 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 个元素。但它们没有出现在列表中。为什么是这样?

4

1 回答 1

2

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

没有人知道,只有Swing 中的 Concurency可能是问题,确保所有事件都将在 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;
        }
    }
}
于 2012-12-16T20:57:57.467 回答