We help IT Professionals succeed at work.

JList -> ListModel, adding items

rossryan
rossryan used Ask the Experts™
on
Hello All,

Having some trouble with JLists. Here's how it goes.

I have created (visually, with NetBeans) a JList. And I need to update (on demand) the items displayed in the JList. I can't find a way to do this.

Everywhere I look, code says to create a new Jlist, with a listdefaultmodel, and jScollpane. It's not doing anything for me. What I want to do is update an already existing JList (given that it was created visually, I don't think setting the existing JList variable to a new JList is going to do anything, but I tried anyway, and yes, it does nothing).

So, how do I update an already existing JList?

Thanks,
Ryan
private javax.swing.JList jList_Trips;
private javax.swing.JScrollPane jScrollPane_Trips;
 


private void RefreshList() {

        DefaultListModel data = new DefaultListModel();
        jList_Trips = new JList(data);
        jScrollPane_Trips = new JScrollPane(jList_Trips);

    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
private void RefreshList() {

        DefaultListModel data = new DefaultListModel();
        jList_Trips.setModel(data);
    }

alternatively keep the model as a member var and add new elements to it