-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I have a Listbox filled with rows loaded from a database table. If there are changes in the table (deletions, modifications or additions of rows) I'd like to update my window by a method in java (somethings like refresh, redraw, invalidate). But it seems that the only possiblity is to remove the listbox and recreate it totally. Am I correct?
Stephan
A call to setModel() should do it. From the Javadoc:
* Sets the list model associated with this listbox. If a non-null model is
* assigned, no matter whether it is the same as the previous, it will
* always cause re-render.
Hi Matze2,
thank you for your reply. I tried it with "list.setModel(list.getListModel());" directly after the change has been saved to the database table. This didn't work, maybe because it was exactly the same as previous. But by defining a new ListModel with the same data loaded from the database it worked (list.setModel(new SimpleListModel(getAllRecords()));).
Stephan
You could also just update that particular item in the list model (if the retrieval of the list model data itself is expensive)
Something like
ListModelList lml = (ListModelList) listBox.getListModel(); int idx = lml.indexOf(obj); if (idx != -1) { { lml.set(idx,obj); }
Asked: 2011-02-13 14:04:45 +0800
Seen: 161 times
Last updated: Aug 21 '12