I have a JavaFx application in which I display a warning window using:
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = new ButtonType("No", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Optional<ButtonType> result = alert.showAndWait();
Since the alert is running showAndWait, it will therefore be displayed until the user clicks the "Yes" or "No" or "Close" dialog box.
Problem: I need to close this dialog programmatically from another place. To clarify, if, say, the user did not select an option within 20 seconds or did not say that this warning window was shown for some background process that just ended, and now I want to close this notification window by setting resultin be buttonTypeCancel, instead of having the user press any button. (Like a method disposein Swing)
How can i do this? I tried Event.fireevent ( https://stackoverflow.com/a/1652395/ ), but I cannot record the corresponding event.
Thanks in advance!
Edit: Including sample code -
- MainApp.java - Java class responsible for processing the application
- Controller.java - Corresponding controller file
- Design.fxml - FXML , MainApp.java Controller.java
Compute.java - java .
Compute { ;
public void function1{
Platform.runLater(new Runnable(){
public void run(){
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = new ButtonType("No", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
} else {
}
}
});
}
public void function2{
}
}
, ? Compute.java javafx, , .