Reputation: 9818
I have a basic JavaFX application, that opens as so
public class MyApplication extends Application {
private Stage stage;
public static void main(String[] args) {
Console.setDebug();
launch(args);
}
@Override
public void start(Stage primaryStage) {
// set stage as primary
stage = primaryStage;
stage.setFullscreen(true);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Closing application...");
}
});
}
}
Now to change screens i have this function, which i call
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
Scene scene = new Scene(screen);
stage.setScene(scene);
stage.setFullscreen(true);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
Example call would be
public void goToHome() {
replaceScene("/fxml/HomeView.fxml", new HomeController());
}
Now when i run the application, the first screen is in fullscreen mode, then when i change screens, the screen resizes to window size, then changes again to full screen??? I have tried adding
stage.setFullscreen(false);
before i call
stage.setFullscreen(true);
but this does work either. How can i change screens/scenes without it resizing?
Also is is possible to toggle full screen mode using code, say if i want the user to be able to select full screen mode, can this be done?
Upvotes: 0
Views: 1529
Reputation: 9818
Thanks to Mailkov for his code, nearly worked, but had to change it slightly, to cope with the first screen loading. This now works using
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
if (stage.getScene() == null) {
Scene scene = new Scene(screen);
stage.setScene(scene);
} else {
stage.getScene().setRoot(screen);
}
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
Upvotes: 0
Reputation: 1231
This is the solution:
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
stage.getScene().setRoot(screen);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
Upvotes: 2