Choose which monitor opens the JavaFX window 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 on the first monitor, and every time I have to drag it to the second monitor to use it.

I need to do this because when it opens on the first monitor, none of the components inside the scene load. It only loads if I drag it onto a second monitor. But when I disconnect the second monitor, it loads correctly.

Can anyone help me out? How do I open a window on a second monitor by default?

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

Responding to comments made:

The problem of correctly loading components on screen1 occurs with any simple javaFX code. For example, for convenience, I take the code that @Sergey gave as an 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 run this from screen2, either with an eclipse or with a terminal, this is what appears on screen 1: Malfunctioning load Resizing the window does not show me the components, but simply increases them. I also can’t push buttons.

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

If the other two monitors are still connected, if I drag the eclipse to screen1 and run the code from there, it loads correctly: works correctly

I should also add that as soon as I dragged it onto another screen and the components loaded, they work well on any screen.

+6
source share
1 answer

You can iterate Screen.getScreens() and move the stage to the desired one. See the 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 cannot help with loading components on the main screen. Please provide more detailed information to solve this problem (which components, how they are created, sample code, etc.)

+6
source

All Articles