For this you need some parameters. There is a method called stage.setOnShown() that will be called immediately after opening a new stage.
But remember the code below, it will open the second stage without any opportunity to close it, so you need to kill the application. This can be improved with a timer where windows will automatically close.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; public class TwoStage extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Open second stage"); btn.setOnAction((e) -> { Label l = new Label("I'm a second window"); Scene s = new Scene(l, 100, 100); Stage s1 = new Stage(StageStyle.TRANSPARENT); s1.centerOnScreen(); s1.setScene(s); s1.initModality(Modality.NONE); s1.setAlwaysOnTop(true); s1.setOnShown((e1) -> { primaryStage.requestFocus(); }); s1.show(); }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Two Windows"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Nwdev source share