0

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.

Community
  • 1
  • 1
adeena
  • 4,027
  • 15
  • 40
  • 52
  • Yes, I added it to the question. If I could close the question with the response "Answer found" I would. – adeena Feb 27 '14 at 16:19
  • You can answer your own question, but you will have to wait for sometime before you can answer your own question ! :D – ItachiUchiha Feb 27 '14 at 16:20
  • Just wondering as you are using Java 8; Why don't you implement the `handle()` method as a lambda expression, if you are using Java 8 anyway? – skiwi Feb 27 '14 at 21:27
  • I haven't started the mastery of lambda expressions yet... it's on the to do list! – adeena Feb 27 '14 at 23:13

0 Answers0