JavaFX provides an easy option to get screen dimensions (screen size) of all the monitors connected. This can be done using the javafx.stage.Screen class.

Get Screen Size of Primary Monitor

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    //Get primary screen bounds
    Rectangle2D screenBounds = Screen.getPrimary().getBounds();
    System.out.println(screenBounds);
    System.exit(0);
  }
}

Example Output

Rectangle2D [minX = 0.0, minY=0.0, maxX=1920.0, maxY=1080.0, width=1920.0, height=1080.0]

Get Number Of Monitors / Visual Devices

Let’s see how we can find number of monitors currently attached to the system using the JavaFX Screen API.

import javafx.application.Application;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    System.out.println(Screen.getScreens().size());
    System.exit(0);
  }
}

Example Output

2

Get Screen Size Of All Monitors

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    ObservableList<Screen> screenSizes = Screen.getScreens();
    screenSizes.forEach(screen -> {
      System.out.println(screen.getBounds());
    });
    System.exit(0);
  }
}

Example Output

Rectangle2D [minX = 0.0, minY=0.0, maxX=1920.0, maxY=1080.0, width=1920.0, height=1080.0]
Rectangle2D [minX = -1920.0, minY=0.0, maxX=-384.0, maxY=864.0, width=1536.0, height=864.0]
Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

26 COMMENTS