Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...