Reputation: 811
I know this isn't the only question about filling JLists, but I didn't find the answer in another SO thread.
I've used the Netbeans GUI builder to create my GUI. The JList is added to a scrollpane, if I hardcode the content of the JList everything is showed fine.
jList1.setModel(new javax.swing.AbstractListModel() {
public String[] strings = {"1", "2", "etc..."};
@Override
public int getSize() {
return strings.length;
}
@Override
public Object getElementAt(int i) {
return strings[i];
}
});
But if I try to add items dynamically via SwingWorker
, nothing appears.
JList jList1 = new javax.swing.JList();
DefaultListModel info = new DefaultListModel();
....
jList1.setModel(info);
....
public void FillList(final String subject) {
worker = new SwingWorker() {
@Override
protected Object doInBackground() {
info.addElement(subject);
return 0;
}
@Override
protected void done() {
}
};
worker.execute();
}
I just want to show the subjects in the JList for visual purposes, the rest is done in the background.
Any help is appreciated,
Thanks!
Upvotes: 3
Views: 541
Reputation: 109813
Swing is single threaded and all output to the visible GUI must be done on EDT
you have an issue with Concurency in Swing
output from SwingWorkers methods doInBackground() doesn't notified EventDispatchThread
then any changes in ListModel isn't visible in the Swing GUI
have to override publish()
or process()
, these two methods quite quarenteed that output will be doe on EDT
Upvotes: 3