Domanda

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

È stato utile?

Soluzione

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

  • Do the database query in a SwingWorker.
  • Add a PropertyChangeListener to the SwingWorker
  • When the SwingWorker's state property is changed to SwingWorker.StateValue.DONE, have the GUI get the new data from the SwingWorker and populate the ListModel on the EDT
  • And then have your ListModel fire its fireContentChanged(...) method.
  • The ListModel should extend AbstractListModel.
  • Note that if you're able to use a DefaultListModel, you don't even have to call the fireContent....() method since the DefaultListModel will do that for you.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top