简体   繁体   English

当Java中的ListModel更改时,如何更新JList?

[英]How can I make JList update when ListModel changed in java?

I have a JList and I use a DefaultListModel to store the list entries. 我有一个JList,并使用DefaultListModel来存储列表条目。 I have a button on that panel. 我在该面板上有一个按钮。 When the button is clicked, I add new entry to DefaultListModel . 单击按钮时,我将新条目添加到DefaultListModel。

button actionPerformed: 按钮操作已执行:

My problem is, after I did the operation on my DefaultListModel, the content of JList doesn't change, I'm wondering that do I need to call a sort of refresh method on JList after I make changes on the ListModel? 我的问题是,在对DefaultListModel进行操作之后,JList的内容没有更改,我想知道在对ListModel进行更改后是否需要在JList上调用某种刷新方法?

public void actionPerformed(ActionEvent e) {
        ModifyXMLFile.create(FileList.listModel);
        FileList.fileList1.revalidate();
    }

JList Class: JList类:

public class FileList {
public static DefaultListModel listModel;
public static WebList fileList1 = null;
public static Component getGui(File[] all) {
    listModel = new DefaultListModel();
    for(File file:all){
      listModel.addElement(file);
    }
    final WebList fileList = new WebList(listModel);
    fileList1=fileList;
    fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    fileList.setCellRenderer(new FileRenderer(!vertical));

    fileList.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {


    });     
}

尝试调用DefaultListModel的方法

protected void fireContentsChanged(Object source, int index0, int index1)

1) remove code line panel.updateUI(); 1)删除代码行panel.updateUI(); this code line is about Look And Feels 此代码行是关于外观的

2) Swing is single threaded and updates to the Swing GUI must be done on EDT, otherwise contents or changes to the GUI are not visible or contents isn't refreshed or freeze 2) Swing是单线程的 ,必须在EDT上完成对Swing GUI的更新,否则将看不到GUI的内容或更改,或者未刷新或冻结内容

3) you have look at SwingWorker for loading JList's Item on the background task, then output from SwingWorker to the GUI will be done on EDT 3)您已经查看了SwingWorker在后台任务上加载JList's Item ,然后将在EDT上完成从SwingWorker到GUI的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM