Mixing Swing/FX: can't dispose a dialog from fxml controller

时光怂恿深爱的人放手 提交于 2019-12-05 21:56:59

Bug alert - remove the controller from the FXML-file and set it in the code instead.

FXMLLoader fxmlLoader = new FXMLLoader(DisposeExample.class.getResource("DisposeController.fxml"));
fxmlLoader.setController(DisposeExample.this);
parent = (FlowPane)fxmlLoader.load();

Unfortunately this will also destroy the FX-CPU-heating on these cold days ;-)

anything wrong with the example?

a resounding YES - citing Martin's (very quick and clear :-) comment to the issue:

The problem is in your fxml file.

"fx:controller" attribute takes the class and creates a new instance of it (using the default contructor). The default constructor of your DisposeExample class posts a new Runnable that will load the same fxml file again a create yet another instance of DisposeExample class.

You should either use a different class for your controller or set the controller manually using the setController() call or using a controller factory (setControllerFactory). Otherwise, there is no way for FXMLLoader to know that you wanted to use your particular DisposeExample object.

So why are you not executing the dispose on the swing thread?

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