简体   繁体   中英

New Stage in Javafx is coming to center

We are using more than one stage(window) in JavaFX for a desktop standalone application. So when we drag or move any of the stage(window) form one point to another and clicking a certain button to navigate to the other page means the new stage is coming to center again(default position). Is there any methods to set the future stage(future windows) also in the previous stage location or point(previous windows). Please help.

You can control stage location before showing it. Just remember last stage coordinates and create new one in the same point:

public class FXStages extends Application {

    private int counter = 0;

    @Override
    public void start(final Stage stage) {
        Button btn = new Button();
        btn.setText("Reopen");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                stage.close();
                Stage newStage = new Stage();
                newStage.setX(stage.getX());
                newStage.setY(stage.getY());
                start(newStage);
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, 300, 250);
        stage.setTitle("Stage " + counter++);
        stage.setScene(scene);

        stage.show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM