javafx full screen on SECOND screen
Asked Answered
B

1

7

For the life of me, I can't seem to get help on this. I have a JavaFX screen and I am trying to get to show fullscreen on my 2nd monitor. I tried the following based on other recommendations but to no avail. I know the coordinates are right but it KEEPS going full screen on my MAIN monitor. Please help.

if (mainSet.getBoolean("fullScr", false)) {
  int count = mainSet.getInt("MonSel", 0);
  if (count > 0) {
    int i = 0;
    for (Screen screen: Screen.getScreens()) {
      if (count == i) {
        Rectangle2D bounds = screen.getBounds();
        primaryStage.setX(bounds.getMinX());
        System.out.println(bounds.getMinX());
        System.out.println(bounds.getMinY());
        primaryStage.setY(bounds.getMinY());
      }
      i++;
    }
  }
  primaryStage.setFullScreen(true);
}

The first if checks a preference to see if fullscreen is set. the 2nd if sees if another monitor besides the first one was selected. It's 1, so that should be the 2nd monitor. The program loops through all screens and tries to move the program and THEN will go full screen. I know the coordinates are the same but no dice, it still goes full screen on the main screen. Please help.

Burglarize answered 11/9, 2018 at 5:45 Comment(2)
Please provide a minimal reproducible example that demonstrates the problem.Persecute
I was able to resolve it by putting primarystage.show() after declaring the fullscreen code but I like that other guys answer much better, so neater :)Burglarize
P
5

I don't know if I truly understand your problem, but if you have two screens, why loop through the screens? Why not just use the info associated with the screen in position two/index one of the ObservableList? I am posting a sample app that demos how to display full screen on a second monitor.

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication257 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        ObservableList<Screen> screens = Screen.getScreens();//Get list of Screens
        Button btn = new Button();
        btn.setText("Full Screen - Screen 1");
        btn.setOnAction((ActionEvent event) -> {
            Rectangle2D bounds = screens.get(0).getVisualBounds();
            primaryStage.setX(bounds.getMinX());
            primaryStage.setY(bounds.getMinY());
            primaryStage.setFullScreen(true);
            //primaryStage.setWidth(bounds.getWidth());
            //primaryStage.setHeight(bounds.getHeight());
        });

        Button btn2 = new Button();
        btn2.setText("Full Screen - Screen 2");
        btn2.setOnAction((ActionEvent event) -> {
            if (screens.size() > 0) {
                Rectangle2D bounds = screens.get(1).getVisualBounds();
                primaryStage.setX(bounds.getMinX());
                primaryStage.setY(bounds.getMinY());
                primaryStage.setFullScreen(true);
                //primaryStage.setWidth(bounds.getWidth());
                //primaryStage.setHeight(bounds.getHeight());
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(new VBox(btn, btn2));

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
Pion answered 11/9, 2018 at 14:13 Comment(2)
I'm marking this as answered because It actually simplifies my code greatly. I had no idea I could do that. Thank you My friend :)Burglarize
Interestingly enough it worked when I did all that code BEFORE doing primarystage.show() BUT it also worked if i did the fullscreen command with a button. its odd.Burglarize

© 2022 - 2024 — McMap. All rights reserved.