JavaFX Get Screen Size

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.

More From Author

JavaFX Scene Switch Animation

Fake (Photoshopped) Image Detection using Machine Learning

52 thoughts on “JavaFX Get Screen Size

  1. Pingback: Find Out More
  2. Pingback: aksara178 slot
  3. Pingback: altogel rtp
  4. Pingback: info
  5. Pingback: buy valganciclovir
  6. Pingback: pilsakmens
  7. Pingback: AMBKING
  8. Pingback: lights out gummies
  9. Pingback: https://vhnbio.com
  10. Pingback: nagaway
  11. Pingback: โคมไฟ
  12. Pingback: College
  13. Pingback: gambia senegal
  14. Pingback: sci diyalaa
  15. Pingback: cinemakick
  16. Pingback: bangkok tattoo
  17. Pingback: HArmonyCa
  18. Pingback: betflix wallet

Leave a Reply