Java Question

Live updating ListView JavaFX

I have class:

public class Friends implements Runnable{
private ObservableList<String> friendsList;

public Friends() {
this.friendsList = FXCollections.observableArrayList();
}

public ObservableList<String> getList(){
return friendsList;
}
public void start(){
//run thread here
}
@Override
public void run() {
//update friendList here
}


}

In controller i write this:

Friends vf = new Friends();
ListView_1.setItems(vf.getList());
vf.start();


After that ListView update every second, but i have exception like this:
Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5.
.....

Having read the manual, I understood that we need to refresh UI in FX thread. I have used Platform.runLater(), but UI is slowing down by the end of the stream.

Sorry for my poor English.

rli rli
Answer

Most likely your Friends.run runs on a separate thread and updates friendsList there. But this is not allowed. You must update friendList on the FX application thread using Platform.runLater(() -> { friendsList.setAll(newValue); }).

You can build newValue in the background thread but setting friendsList MUST be done on the FX application thread.