4

I want to switch the scenes of my JavaFX application in Fullscreen with a "Next"-Button. But if I click on that Button it switches from fullscreen to windowed and back to fullscreen within a second. How can I achieve to avoid that and stay in fullscreen mode?

Some relevant snippets:

Application.java:

public class Application extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLMain.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(true);
        stage.setTitle("AppName");

    }

    public static void main(String[] args) {
        launch(args);
    }

}

FXMLMainController.java:

@FXML
private void handleBtnNext(ActionEvent event) throws Exception{
    Stage stage; 
    Parent root;
    if(event.getSource()==btnNext){
        //get reference to the button's stage         
        stage=(Stage) btnNext.getScene().getWindow();
        //load up OTHER FXML document
        root = FXMLLoader.load(getClass().getResource("FXMLOptions.fxml"));
    }
    else{
        stage=(Stage) btnNext.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("FXMLMain.fxml"));

    }
    //create a new scene with root and set the stage
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    stage.setFullScreen(true);
}
Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
Nik
  • 179
  • 1
  • 3
  • 11

2 Answers2

1

That behavior, where the app pops out of full screen mode when you switch scenes, is weird (it happens for me too on Java 8u60, OS X 10.11.3). It may be a bug.

To work-around it, you can just reuse the same stage and scene and adjust the root of your scene, rather than changing the scene itself.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class FullScreenScenes extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Button next1 = new Button("Show Scene 2");
        StackPane layout1 = new StackPane(next1);
        layout1.setStyle("-fx-background-color: palegreen;");

        Button next2 = new Button("Show Scene 1");
        StackPane layout2 = new StackPane(next2);
        layout2.setStyle("-fx-background-color: paleturquoise;");

        Scene scene = new Scene(layout1);

        next1.setOnAction(event -> scene.setRoot(layout2));
        next2.setOnAction(event -> scene.setRoot(layout1));

        stage.setScene(scene);
        stage.setFullScreen(true);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
-3

first Adjust your scene Builder view (Responsive responsive screen sizes) this is your Scene Builder View problem Adjust the view (Layout or Fxid ) check again .

  • 1
    how to adjust the Scene Builder View? And how can this problem has sth to do with the Scene Builder if the problem appears when the application is running after it was compiled? – Nik Mar 16 '16 at 05:34
  • fxml does not necessarily mean SceneBuilder was used. Actually it's rather easy to write a valid fxml that SceneBuilder cannot open. – fabian Mar 16 '16 at 07:39