Home > other >  JavaFX Java17 - exit Fullscreen Event
JavaFX Java17 - exit Fullscreen Event

Time:12-24

I have added a custom toolbar (where my window actions are placed) to my application. So far everything works well. Related to the window handling I'm searching for a possibility to handle the "fullscreen got closed" event. Scenario: App starts in windowed mode -> user clicks on (custom) toolbar button to get into fullscreen. The Toolbar will now be set its visibility to false. The users now exits fullscreen mode via button (native macOS Button to exit fullscreen) --> I need now to react for this (to set the toolbar to visible again) but cannot find a way how to do it.

main.java

MainController mc = new MainController();
Parent root = FXMLLoader.load(getClass().getResource("welcome-view.fxml"));
stage.initStyle(StageStyle.TRANSPARENT);
mc.doSwitchScenes(stage, root);
stage.show();

MainController.java

...
private String title = "Project Apollo";
private Color fillColor = TRANSPARENT;
private int minWidth = 800;
private int minHeight = 600;
...

public void btnMinimize(MouseEvent mouseEvent) {
    Stage stage = (Stage)((Circle)mouseEvent.getSource()).getScene().getWindow();
    // is stage minimizable into task bar. (true | false)
    stage.setIconified(true);
};

public void btnCloseApp(MouseEvent mouseEvent) {
    Platform.exit();
    System.exit(0);
}

public void btnFullscreen(MouseEvent mouseEvent) {
    Stage stage = (Stage)((Circle)mouseEvent.getSource()).getScene().getWindow();
    stage.setFullScreen(true);
    Scene actualScene = ((Node)mouseEvent.getSource()).getScene();
    Parent hbc = (Parent) actualScene.lookup("#headerBarContainer");

    if(hbc != null){
        hbc.setVisible(false);
    }
    System.out.println("clicked FS");

}
...

The point is that at least on MacOS the window has its native os control to exit fullscreen - is it possible to target this event or at least the change of the stage size maybe?

CodePudding user response:

Listen to stage.fullScreenProperty() and respond to changes:

stage.fullScreenProperty().addListener((ChangeListener) (obs,oldValue,newValue) -> 
                                          {/*TODO respond to changes in full screen */;});
  • Related