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.

Vivek V K
  • 1,100
  • 2
  • 15
  • 33
  • 1
    You could probable work out something with `Screen`. See [here](https://community.oracle.com/thread/2593365?tstart=0) (No time to work it to a complete answer at the moment). – Itai Jan 22 '16 at 00:51
  • 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. – hotzst Jan 22 '16 at 07:24
  • 1
    Can you provide an example that I could test on my dual-monitor setup? – Oliver Jan Krylow Jan 22 '16 at 08:31
  • @Vivek which version of JavaFX do you use? It looks like an old bug in JavaFX on Mac. – Sergey Grinev Jan 25 '16 at 13:14
  • @Sergey I use 8.0.20-b26 at the moment since that is what the project already uses. – Vivek V K Jan 27 '16 at 00:04
  • 1
    not that old, but anyway can you try latest as an experiment? 8u72 – Sergey Grinev Jan 27 '16 at 11:59
  • 1
    fixed by updating to 8u71. Thanks @SergeyGrinev – Vivek V K Mar 02 '16 at 02:54

1 Answers1

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)

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141