2
votes

I have a JList and I use a DefaultListModel to store the list entries. I have a button on that panel. When the button is clicked, I add new entry to 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?

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

JList Class:

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) {


    });     
}
2
You don't need to do anything. Please post an SSCCE. - JB Nizet
Did you set the model to the list with setModel()? - Jakub Zaverka

2 Answers

3
votes

Try to call the method of DefaultListModel

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

1) remove code line 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

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