I'm trying to do something similar to what the person was asking for here: Closing a substage
except that instead of clicking a button, I'm doing this with a menu item and "onAction" call.
The window I'm trying to close is a sub-window. My main FXML has a menuitem that when pressed, opens the window. Then the second.fxml has a menu item to close the window. The second.fxml has it's own controller ("FXMLSecondController.java")... but the stage was opened in the "FXMLPrimaryController.java".
My FXML menu item looks like this:
<MenuItem text="Close" onAction="#closeThisWindow" />
Then in FXMLSecondController.java I have:
@FXML
private void closeThisWindow(ActionEvent event) {
if (closeWindowDialog()) {
//close the window
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent aE){
Node source1;
source1 = (Node) aE.getSource();
Stage stage = (Stage) source1.getScene().getWindow();
stage.close();
}
};
} else {
}
}
(Note that "closeWindowDialog()" is a function that opens up a Yes/No dialog box and returns true/false)
I can't seem to access the stage from which I'm calling this menu item from...
PS. I'm working with JavaFX/Java 8 and NetBeans 8. No Scene Builder.
ANSWER:
Found the answer here: Unable to get Scene from MenuItem in JavaFX That was my problem.