Search code examples
javajavafxobservablelist

More efficient method of updating observableList


Here's my setup:

I have a mySQL database with some data.

I have a makeshift server built where it downloads this data and turns it into an ObservableList and the client can make its own copy of this ObservableList, call it localList.

If I want to make a new entry, the requirements dictate that it must be done by sending the new entry to the SQL database, the server downloads it, the client pulls this update,THEN the client sees the update. (This is done since multiple clients will be accessing this database).

Here's my problem: Currently, the way I do it is when I make a new entry, the server clears its observableList, fills it again, and the client clears its localList and fills it again.

I do not have to worry about the server code (it will be replaced with a more sophisticated server), but I am wondering if my localList (from ObservableList) comes with some method that can compare to another list and make changes accordingly.

Instead of doing

ObservableList<myType> locallist = FXCollections.observableArrayList();

....
localList.clear();
for (myType element : serverList) {
    localList.add(element);
}

Each time I make a new entry, is there a more efficient way? Some type of update method possibly?

Basically the question boils down to:

Is there a way of setting an observableList<myType> equal to another without fully clearing the list and then adding the elements one by one.


Solution

  • Does

    Bindings.bindContent(localList, serverList);
    

    do what you need? (Javadocs). It would work if you keep the same list instance in serverList, and don't reassign the reference.