我的JavaFX应用程序出现问题。它分为几个部分 - 主要阶段和阶段作为对话。当我关闭主阶段时,setonclosedrequest按预期被触发,但是对话框的setoncloserequest永远不会被触发。我做错了什么?这是我的代码:
主要应用程序:
public class TestApp extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLDialogLoader loader = new FXMLDialogLoader("de/pasa/fxml/Dialog.fxml");
loader.setController(new DialogController(primaryStage));
loader.loadPrimaryStage(primaryStage, "Main App");
primaryStage.setOnCloseRequest(e->{
System.out.println("Do something before Close");
});
}
}
控制器:
public class DialogController implements Initializable{
@FXML private Button bOpen;
private Stage parentStage = null;
public DialogController(Stage parentStage) {
this.parentStage=parentStage;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
bOpen.setOnAction(e->{
FXMLDialogLoader loader=new FXMLDialogLoader("de/pasa/fxml/Test.fxml", parentStage);
Stage dlgStage=loader.loadDialog();
dlgStage.setOnCloseRequest(t->{
//never reached - not fired, why ?
System.out.println("Do something before Dialog closed");
});
});
}
}
FXMLLoader:
public class FXMLDialogLoader {
private String _fxmlPath=null;
private String _title=null;
private Object _controller=null;
private ResourceBundle _bundle=null;
private Stage _parentStage=null;
private Stage _primaryStage=null;
private Stage stage=null;
private StageStyle _style=null;
private String _iconPath=null;
private LoggerEX _log = LoggerEX.getLogger(FXMLDialogLoader.class);
public FXMLDialogLoader(String fxmlPath){
this(fxmlPath,null);
}
public FXMLDialogLoader(String fxmlPath,Stage parentStage){
this(fxmlPath,parentStage,"Dialog",null,null,StageStyle.UTILITY);
}
public FXMLDialogLoader(String fxmlPath,Stage parentStage,String title,Object controller,ResourceBundle bundle,StageStyle style){
_fxmlPath=fxmlPath;
_parentStage=parentStage;
_controller=controller;
_bundle=bundle;
_title=title;
_style=style;
}
public Stage loadDialog(){
FXMLLoader loader=new FXMLLoader(getClass().getClassLoader().getResource(_fxmlPath));
loader.setController(_controller);
loader.setResources(_bundle);
try {
Parent root=loader.load();
Scene scene=new Scene(root);
if(_primaryStage==null){
stage=new Stage();
}
else{
stage=_primaryStage;
}
if(_iconPath!=null){
stage.getIcons().add(new Image(new FileInputStream(new File(_iconPath))));
}
stage.setTitle(_title);
stage.setScene(scene);
if(_primaryStage==null){
stage.initModality(Modality.WINDOW_MODAL);
}
stage.initStyle(_style);
if(_primaryStage==null){
stage.initOwner(_parentStage);
}
if(_primaryStage==null){
stage.showAndWait();
}
else{
stage.show();
}
stage.centerOnScreen();
}
catch(IOException ex){
ex.printStackTrace();
_log.error(ex);
}
return stage;
}
public Stage getDialogStage(){
return stage;
}
public Stage loadPrimaryStage(Stage primaryStage,String title){
return loadPrimaryStage(primaryStage,title,null);
}
public Stage loadPrimaryStage(Stage primaryStage,String title,String iconPath){
_primaryStage=primaryStage;
setTitle(title);
setIconPath(iconPath);
setStageStyle(StageStyle.DECORATED);
_primaryStage.centerOnScreen();
loadDialog();
return _primaryStage;
}
public void setIconPath(String iconPath){
_iconPath=iconPath;
}
public void setTitle(String title){
_title=title;
}
public void setStageStyle(StageStyle style){
_style=style;
}
public void setController(Object controller){
_controller=controller;
}
public void setResourceBundle(ResourceBundle bundle){
_bundle=bundle;
}
}
答案 0 :(得分:1)
您正在调用showAndWait()
来显示对话框,正如方法名称所暗示的那样,该对话框在返回之前一直等到对话框被关闭。只有在对话框被解除之后,您才会注册onCloseRequest
处理程序:到那时处理事件为时已晚。