2017-10-12 23 views
0

在我的应用程序中,有一个主窗口和一个辅助窗口用于一些更改。当用户想关闭应用程序(=主窗口)时,我想要求用户确认是否关闭更改的辅助窗口以避免丢失更改。JavaFx:我的主窗口因为次要的开放窗口而要求确认关闭

此代码位于我的主类中。

我的问题是,如果辅助窗口打开或不打开,我无法从我的主类中检查。

注意:辅助窗口正在我的maincontroller类上创建。

谢谢!

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); 

     Scene scene = new Scene(root); 
     scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
     primaryStage.getIcons().add(new Image("image.png")); 
     primaryStage.setTitle("Κάβα ποτών"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() { 
      @Override 
      public void handle(WindowEvent event) { 
      event.consume(); 
      closing(); 
      } 
     }); 

    } 

    public void closing() { 
     //if(the secondary window is closed) 
       //Platform.exit(); 
     //else 
       //ask confirmation with confirm alert 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

欢迎来到Stack Overflow!考虑通过点击旁边的复选标记来接受下面的答案。这将有助于@HungryCosmos通过让他们知道更多的代表和未来用户搜索类似的问题,让他们知道这个问题有一个可接受的答案。 – MMAdams

回答

1

使用isShowing方法也确定是否SecondaryStage打开。

要使用它,您应该在创建阶段时将SecondaryStage分配给变量,并通过将其设置为公共或提供获取方来提供对其的访问。

And this is how to access controller from your Main class.

希望它能帮助。

+0

它的工作原理! Thx男人!最后比我想象的更容易! :P –