JavaFX Stage close event handler

守給你的承諾、 提交于 2020-01-04 02:18:49

问题


I have a Stage in JavaFX that can be closed in multiple ways, either by clicking the red (X) or by a Button which calls stage.close()

Regardless of how the Stage is closed, I would like to perform an action before (or as) it is closed.

If I use the following code:

myStage.setOnCloseRequest( event -> {System.out.println("Closing Stage");} );

then the handler is called when I click the (X) but not when I call myStage.close()

This is the same issue that this question talks about (with a key difference): JavaFX: Stage close handler

The difference is that he wants to call a handler as the entire application is closed, and therefore can override the Application's stop() method. However I'm not closing the entire application, just one stage. And Stage does not have a stop() method to override.

Thanks for any help.


回答1:


Thanks to comments by VGR, the solution I was looking for really was as simple as replacing setOnCloseRequest with setOnHiding:

myStage.setOnHiding( event -> {System.out.println("Closing Stage");} );


来源:https://stackoverflow.com/questions/44548460/javafx-stage-close-event-handler

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