JavaFX+MVC. How to hide/show multiple windows?

て烟熏妆下的殇ゞ 提交于 2020-01-06 02:00:07

问题


How to make that when i click on "Open second window" the main window be hide. And when i close second window, the main window be showed? I read this post, but i don't know how to implement it for my task. Thanks!

I have next code:
Main.java

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("First Stage");
        try {
            primaryStage.setResizable(false);

            Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

MainWiew.fxml

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane id="rootMain" prefHeight="418.0" prefWidth="691.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.MainController">
   <top>

   </top>
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <VBox fillWidth="false" prefHeight="400.0" prefWidth="394.0" spacing="8.0">
               <children>
                  <Button id="btnMainOne" fx:id="btnMainOne" mnemonicParsing="false" prefHeight="51.0" prefWidth="398.0" text="Open second window" textFill="#4460ff">
                     <font>
                        <Font name="Times New Roman Bold" size="20.0" />
                     </font>
                  </Button>

               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </VBox>
         </children>
      </AnchorPane>
   </center>
</BorderPane>

MainController.java

import java.io.IOException;  
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainController {
    @FXML
    private Button btnMainOne;

    @FXML
    private void initialize() {
        btnMainOne.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {

                try {
                    Stage stage = new Stage();
                    stage.setTitle("Second stage");
                    stage.setScene(new Scene(root, 450, 450));
                    stage.show();

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

    }
}

回答1:


You can get the primary window by calling getScene().getWindow(). You can call hide() on it to hide it after you show the new stage, and you can register an event listener with the new stage that shows the primary window when the new stage is hidden:

@FXML
private void initialize() {
    btnMainOne.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {

            Stage primaryStage = (Stage)btnMainOne.getScene().getWindow();

            try {
                Stage stage = new Stage();
                stage.setTitle("Second stage");
                stage.setScene(new Scene(root, 450, 450));

                stage.setOnHidden(e -> primaryStage.show());

                stage.show();

                primaryStage.hide();

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });

}


来源:https://stackoverflow.com/questions/35517260/javafxmvc-how-to-hide-show-multiple-windows

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