Closing a substage

2019-08-10 02:13发布

问题:

What I'm trying to do I think is exactly what Meg is talking about here: JavaFX2 : Closing a stage (substage) from within itself

When I try to implement JewelSea's answer I get the "nonstatic method getSource() cannot be referenced from a static context."

So I have my secondary window (scene) created in Scene Builder with a simple controller class that has basically one function: tie the button to a close() event handler. Here's the code I have:

public class ProductNotFoundController
    implements Initializable {

    @FXML //  fx:id="closeButton"
    private Button closeButton; // Value injected by FXMLLoader


    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {

        closeButton.setOnAction(new EventHandler<ActionEvent> () {
            @Override
            public void handle(ActionEvent t) {
//                ProductNotFound.avisoClose();
                Node source;
                source = (Node) ActionEvent.getSource();
                Stage stage = (Stage) source.getScene().getWindow();
                stage.close();
            }
        });

    }

}

Can someone please tell me what I'm doing wrong? And/or where should I put the close() method?

回答1:

Replace ActionEvent by t, getSource is a non-static method. jewelsea was using actionEvent the instance of the ActionEvent class.



标签: javafx-2