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) {


No comments:

Post a Comment