Jef
Jef

Reputation: 811

Can't addElement to JList

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

Answers (1)

mKorbel
mKorbel

Reputation: 109813

Upvotes: 3

Related Questions