I have a program that checks every second in the database for newly inserted row and if a new record is found, I have to clear my listModel and retrieve all data again from the database and re-display it in the list.
itemListModel.clear();
ArrayList resultArrayList = DBQueries.getAllSubmittedSaleTransaction();
Iterator iterate = resultArrayList.iterator();
int i = 0;
while (iterate.hasNext()) {
Hashtable data = (Hashtable) iterate.next();
itemListModel.add(i, data);
this.itemList.addNotify();
this.itemList.validate();
this.itemList.repaint();
i++;
}
this.validate();
this.repaint();
but, the problem is, the repaint method sometimes work sometimes not.
Is there any smart way to accomplish this?
thanks in advance
You shouldn’t need to repaint the JList if you are updating the model, especially if your model calls
fireContentsChanged(...)after the new data has been added. Be sure to only change the model on the Swing event thread.Consider that you
SwingWorker.StateValue.DONE, have the GUI get the new data from the SwingWorker and populate the ListModel on the EDTfireContentChanged(...)method.fireContent....()method since the DefaultListModel will do that for you.