Sunday, February 12, 2012

JavaFX 2.0: Full Screen scene

To make the scene occupy full screen, use the code:
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());


JavaFX 2.0: Full Screen scene
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_fullscreen;

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

/**
*
* @author erix7
*/
public class JavaFX_FullScreen extends Application {

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

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

StackPane root = new StackPane();

//Make scene occupy full screen
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());

root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
}
}

5 comments:

  1. If you want real fullscreen, do:
    primaryStage.setFullScreen(true);

    ReplyDelete
  2. Your solution doesn't cover task panel in Windows.

    Thanks, davidwaf! This do the trick!)))

    ReplyDelete
  3. the problem with setFullScreen is the message press esc.

    ReplyDelete
  4. disable the button escape by setonpressed and all other buttons

    ReplyDelete