I have a Jlist receiving elements from a model. the model has elements and when I remove the elements, I want the jlist to update after the removal from the model
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have a Jlist receiving elements from a model. the model has elements and when I remove the elements, I want the jlist to update after the removal from the model
Represent the model by an instance of ListModel. (Eg a DefaultListModel or an AbstractListModel) As the JList documentation states:
"A ListModel can be supplied directly to a JList by way of a constructor or the setModel method. The contents need not be static - the number of items, and the values of items can change over time. A correct ListModel implementation notifies the set of javax.swing.event.ListDataListeners that have been added to it, each time a change occurs. These changes are characterized by a javax.swing.event.ListDataEvent, which identifies the range of list indices that have been modified, added, or removed. JList's ListUI is responsible for keeping the visual representation up to date with changes, by listening to the model."
The How to Use Lists page of Oracle's Tutorial gives a worked example using a DefaultListModel in the section "Adding Items to and Removing Items from a List".