Choose which monitor does a JavaFX window open in
Asked Answered
W

1

11

I have two monitors. I have Eclipse open on the second monitor but when I run my JavaFX code, the JavaFX window always opens up in the first monitor and every time I have to drag it to the second monitor to use it.

I have to do this because when it opens on the first monitor, none of the components inside the scene are loaded. It gets loaded only if I drag it to the second monitor. But when I disconnect the second monitor, it loads properly.

Can someone please help me out? How do I, by default, make the window to open on the second monitor?

NB: My first monitor is a Macbook Pro and the second is an iMac used as an external monitor.

Responding to comments made:

The problem of components not loading properly on screen1 happens with any simple javaFX code. For example and for convenience, I am taking the code that @Sergey has given as the answer.

code:

public class FXScreens extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 200, 250);

        int index = 1;
        for (Screen screen : Screen.getScreens()) {
            Rectangle2D bounds = screen.getVisualBounds();

            Button btn = new Button("Move me to Screen " + index++);
            btn.setOnAction((e) -> {
                stage.setX(bounds.getMinX() + 100);
                stage.setY(bounds.getMinY() + 100);
            });
            root.getChildren().add(btn);
        }

        stage.setTitle("Screen Jumper");
        stage.setScene(scene);
        stage.show();
    }

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

When I launch this from screen2 either with eclipse or with a terminal, this is what appears on screen 1: Faulty load Resizing the window does not show me the components but just zooms them. I am not able to click on the buttons as well.

When I drag this to screen 2, it becomes like this: loaded

With two other monitors still connected, if I drag eclipse to screen1 and run the code from there, it loads properly:runs properly

I should also add that once I drag it to the other screen and components are loaded, they work well on any screen.

Warsle answered 21/1, 2016 at 23:50 Comment(7)
You could probable work out something with Screen. See here (No time to work it to a complete answer at the moment).Pappas
I agree with @sillyfly on the use of Screen. However from your description, content loading only on the second screen I guess there is something seriously wrong or is this intended behavior? If not, you should investigate that and fix it instead of implementing a workaround.Photobathic
Can you provide an example that I could test on my dual-monitor setup?Eustis
@Vivek which version of JavaFX do you use? It looks like an old bug in JavaFX on Mac.Oft
@Sergey I use 8.0.20-b26 at the moment since that is what the project already uses.Warsle
not that old, but anyway can you try latest as an experiment? 8u72Oft
fixed by updating to 8u71. Thanks @SergeyGrinevWarsle
O
14

You can iterate Screen.getScreens() and move your stage to required one. See example below.

public class FXScreens extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 200, 250);

        int index = 1;
        for (Screen screen : Screen.getScreens()) {
            Rectangle2D bounds = screen.getVisualBounds();

            Button btn = new Button("Move me to Screen " + index++);
            btn.setOnAction((e) -> {
                stage.setX(bounds.getMinX() + 100);
                stage.setY(bounds.getMinY() + 100);
            });
            root.getChildren().add(btn);
        }

        stage.setTitle("Screen Jumper");
        stage.setScene(scene);
        stage.show();
    }

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

Unfortunately I can't help with components not loading on primary screen. Please provide more details to address that (which components, how are they created, code sample, etc)

Oft answered 22/1, 2016 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.