Closing a substage

故事扮演 提交于 2019-12-24 15:14:49

问题


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.



来源:https://stackoverflow.com/questions/16451573/closing-a-substage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!