Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose which monitor does a JavaFX window open in

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.

like image 240
Vivek V K Avatar asked Jan 21 '16 23:01

Vivek V K


People also ask

How do I assign a window to a monitor?

Windows+Shift+Left or Right Arrow: Move a window from one monitor to another.

What is window in Javafx?

A top level window within which a scene is hosted, and with which the user interacts. A Window might be a Stage , PopupWindow , or other such top level. A Window is used also for browser plug-in based deployments.


1 Answers

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)

like image 87
Sergey Grinev Avatar answered Oct 14 '22 19:10

Sergey Grinev