gpt4 book ai didi

java - JList 不响应 listModel.addElement(

转载 作者:行者123 更新时间:2023-11-30 09:28:42 24 4
gpt4 key购买 nike

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

最佳答案

However there is a problem. The list clears (before the reset, there was other content. This content does go away), but the new content does not show up. And, from the output, I can see that 6 elements have been added. But they do not show up in the list. Why is this?

没有人知道,只有Concurency in Swing可能是个问题,确保所有事件都在美国东部时间结束,否则为了更好的帮助,请尽快发布 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;
}
}
}

关于java - JList 不响应 listModel.addElement(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com