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) {
});
}
1) remove code line
panel.updateUI();this code line is about Look And Feels2) 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 Itemon the background task, then output fromSwingWorkerto the GUI will be done on EDT