Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

java - Opening a new FXML attached to the current stage - JavaFX

This is an extension of an SO question in which a file chooser opens and is attached to the primary stage.

How would one go about opening an FXML stage attached to the the primary stage?

The code below loads the FXML and replaces the primaryStage (current stage). How can I load it as an attached window instead?

            Stage stage = (Stage)((Node) event.getTarget()).getScene().getWindow();
            Parent parent = null;
            try {
                parent = FXMLLoader.load(getClass().getResource("/gui/GUpdater-progress.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene scene = new Scene(parent,600,400);
            stage.setResizable(false);
            stage.setTitle("GUpdater");
            stage.setScene(scene);
            stage.show();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given your previous question, I imagine that by "attach" you mean something like what the File Chooser does (when there is a parent, the File Chooser will follow it around).

The first thing you need is the "main window", which you can get via

       Window ownerWindow = ((Node) event.getTarget()).getScene().getWindow();

The next thing is to actually load your new stage.

       Stage stage = new Stage();
       stage.initModality(Modality.APPLICATION_MODAL);
       stage.initOwner(ownerWindow);
       Parent root = FXMLLoader.load(getClass().getResource("/gui/GUpdater-progress.fxml"));
       Scene scene = new Scene(root, 600, 400);
       stage.setTitle("GUpdater");
       stage.setScene(scene);
       stage.show();

The key is the stage.initOwner(ownerWindow) part. This new stage you are creating is "owned by" the original window, which is ownerWindow.

In addition, if you want to achieve a feel more like the File Chooser's, you should use

       stage.initStyle(StageStyle.UNDECORATED);

Before setScene() is called. This will remove the border.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...