Display two windows at the same time “on fullscree

2019-02-28 08:37发布

I'm working on a mini application where I need to display to users 2 windows at the same time but on Fullscreen (the application will be made for users on dual screen).

I'm working with JavaFx Scene Builder 2.0 on NetBeans 8.0.1

I tried this but only the second window gets displayed on fullscreen.

public void showTwoScreens() {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("ClientsOperationsWindow.fxml"));
        Scene scene = new Scene(root);

        globalStage.setScene(scene);
        globalStage.setFullScreen(true);
        globalStage.setResizable(true);
        globalStage.show();

        Stage anotherStage = new Stage();
        Parent secondRoot = FXMLLoader.load(getClass().getResource("ClientsSearchWindow.fxml"));

        Scene secondStage = new Scene(secondRoot);
        secondStage.setScene(anotherScene);
        secondStage.setFullScreen(true);
        secondStage.show();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

is it possible to display two windows on fullscreen ?

Thank you!

1条回答
聊天终结者
2楼-- · 2019-02-28 09:05

I think you can't set two stages in fullscreen in two monitors at the same time, but you can get the same result, by forcing the stage dimensions.

For that, we'll use javafx.stage.Screen, to get the characteristics for each of the different monitors connected. Then we load the fxml files to each scene, and display each scene on its stage. With Screen.getBounds() we now the origin and dimensions of the rectangle, refered to the primary screen. So we set each stage bounds with the bounds of these rectangles. Finally we set the style to undecorated. The only feature missing now is an exit "fullscreen" mode key combination.

private Screen secondaryScreen;

@Override
public void start(Stage primaryStage) throws IOException {

    Screen primaryScreen = Screen.getPrimary();

    Parent root = FXMLLoader.load(getClass().getResource("Screen1.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    Rectangle2D bounds = primaryScreen.getBounds();
    primaryStage.setX(bounds.getMinX());
    primaryStage.setY(bounds.getMinY());
    primaryStage.setWidth(bounds.getWidth());
    primaryStage.setHeight(bounds.getHeight());
    primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.show();

    // look for a second screen
    Screen.getScreens().stream()
            .filter(s->!s.equals(primaryScreen))
            .findFirst().ifPresent(s->secondaryScreen = s);

    if(secondaryScreen!=null){
        Stage secondaryStage = new Stage();
        Parent root2 = FXMLLoader.load(getClass().getResource("Screen2.fxml"));
        Scene scene2 = new Scene(root2);
        secondaryStage.setScene(scene2);
        Rectangle2D bounds2 = secondaryScreen.getBounds();
        secondaryStage.setX(bounds2.getMinX());
        secondaryStage.setY(bounds2.getMinY());
        secondaryStage.setWidth(bounds2.getWidth());
        secondaryStage.setHeight(bounds2.getHeight());
        secondaryStage.initStyle(StageStyle.UNDECORATED);
        secondaryStage.show();  
    } 
}
查看更多
登录 后发表回答