DefaultListModel and jList1.setModel not working

510 Views Asked by At

I have been working on JList. I have created a DefaultListModel to add elements to the jList. Everything works fine when used normally, but, when I try to call a method from a thread to update list, it is stuck. Still don't know what's the problem. Below are the codes of jList, method and thread.

Code used for DefaultListModel and jList:

DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("Element 1");
jList1.setModel(model);

Method to update jList in MainClass:

public void UpdateList()
{   
    DefaultListModel<String> model = new DefaultListModel<>();
    model.addElement("Element 1");
    jList1.setModel(model);
}

Thread for calling update method outside MainClass:

class Second extends Thread {
    public void run() {
        Thread.sleep(5000);
        MainClass a = new MainClass();
        a.UpdateList();
    }
}
0

There are 0 best solutions below