views:

276

answers:

1

Hello again.

I'd like to know if there is any way that I can update a Jlist after the user adds or removes an item to it and after a user sorts it. Is there any way that I can write a standardized method to update the display based on the order of items in an array or vector, and when the user remove or adds an object from the array that the JList is based on?

Thank you.

+1  A: 

Updates should be made to the ListModel, not the Array that was used to create the model.

However, if you want to refresh the list with completely new items or change the order of the items then you create a new DefaultListModel and use the setModel(...) method of the JList to update the view.

camickr
Thanks. When adding items to the DefaultListmMdel, do I have to use a for loop or can I pass in an array or Vector?
GLRockwell
Actually I keep forgetting the DefaultListModel doesn't accept an Array or Vector, so yes you would need to add each item individually. However, the DefaultComboBoxModel does accept an Array or Vector and it implements ListModel so you can use it instead.
camickr
Would the following code work? It doesn't seem to be displaying properly.public void updateLibrary(){ DefaultListModel dlm = new DefaultListModel(); //Display library items for(int l = 0; l < DatabaseHandler.nextAvailablePlaylistPos; l++){ dlm.add(l, DatabaseHandler.library[l].getArtist() + " - " + DatabaseHandler.library[l].getTitle()); } libraryD.setModel(dlm); }
GLRockwell
That's horribly formatted, sorry. The code can be seen here:http://pastebin.com/m52633f36
GLRockwell
Yes, this works. The problem lies elsewhere. Thanks.
GLRockwell