I am writing a program that can update it's self. I created a separate thread that checks the server for updates (as not not block the UI from updating). Once updates are done checking, and it has determined that a newer version is available, it should ask the user if he would like to get those updates.
Here is my shouldApplyUpdates
method:
public boolean shouldApplyUpdate() {
Alert updateAlert = new Alert(Alert.AlertType.CONFIRMATION);
updateAlert.setTitle(resourceBundle.getString("ui.gui.update.title"));
updateAlert.setContentText(resourceBundle.getString("ui.gui.update.message"));
Optional<ButtonType> ret = updateAlert.showAndWait();
return ret.get() == ButtonType.OK;
}
What it should do is prompt the user if he would like to apply updates, and if so, return true. The problem is, because this method is being called from another thread, an exception is thrown from not being on the JavaFX Application Thread (in my case, silently, so I had to surround the method with try/catch to see the exception).
Here is my update thread:
new Thread(new Task<Void>() {
@Override
protected Void call() throws Exception {
//Check for updates
...
//If updates are available, call shouldApplyUpdates()
if(shouldApplyUpdates()){
//Apply the updates
}
return null;
}
}).start();
//Create GUI and stuffs, all should be happening while updates are being checked
So what I need to so is create and show the dialog on the Application Thread, and then block the method from returning (the method is safe to be blocked because of the separate thread). Then after the user confirms his choice, return the method.
What is the best way to accomplish this?