Sunday, November 10, 2013

Get screen size using javafx.stage.Screen

javafx.stage.Screen class describes the characteristics of a graphics destination such as monitor. In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of the Screen objects are relative to the Screen.primary. We can obtain the width and height of screen by calling Screen.getPrimary().getVisualBounds().getWidth() and Screen.getPrimary().getVisualBounds().getHeight().

This example set the application run in full screen.
application run in full screen
JavaFX application run in full screen

package javafx_screen;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

 * @web
public class JavaFX_Screen extends Application {

    public void start(Stage primaryStage) {
        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        Label label = new Label();
                visualBounds.getWidth() + " x " + visualBounds.getHeight());

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 
                visualBounds.getWidth(), visualBounds.getHeight());

    public static void main(String[] args) {



  1. Thanks for posting this! It makes my life so much easier. Was thinking about Java AWT Toolkit, but when working with JavaFx, this is just so dead easy. Much obliged. :)

  2. It worked differently on the past, now it ignore fixed task-bars or panes from system, if you need a hardware scope information, the true resolution of your monitor, you can use GraphicsEnvironment to get your GraphicDevice list and so on get its configurations.