I am executing an SQL query, every x seconds using java.util.Timer. The purpose is to keep track of certain database field changes and update the UI in almost real time. Though, when these changes occur JavaFX throws the following error:
Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
I am using MVC model.
View:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
String status = dataAccessor.getOrderStatus(order.getorderID());
orderStatusLabel.setText("Order Status: " + status);
} catch (SQLException e) {
e.printStackTrace();
}
}
}, 0, 2000);
Model:
public String getOrderStatus(int orderID) throws SQLException {
Statement getStatus = conn.createStatement();
ResultSet rs = getStatus.executeQuery(
"SELECT status FROM the_order WHERE orderID = " + orderID);
rs.next();
return rs.getString(1);
}
Why is this happening?