I have a table view that lists user's friends and I need to update it every 5 seconds with data that I retrieve from database.
This is the code I use:
Main.java
private List<Friend> userFriends;
fx controller:
ObservableList<FriendWrapper> friendList = FXCollections.observableList(
new ArrayList<FriendWrapper>());
private void updateFriendList() {
new Thread(new Runnable() {
public void run() {
while (Params.loggedUser != null) {
Main.setUserFriends(Params.dao.listUserFriends(Params.loggedUser));
friendList.clear();
for (Friend friend : Main.getUserFriends()) {
friendList.add(new FriendWrapper(friend.getFriendName(), friend.getOnline(), friend.getFriendId(), friend.getWelcomeMessage()));
}
Params.dao.updateOnlineStatus(Params.loggedUser, 3);
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "updateFriendList").start();
}
Friend
is database model. FriendWrapper
is object used for table rows.
however I get IllegalStateException: Not on FX application thread
on line friendList.clear();
How can I change the items of TableView from a thread running in the background?