JAVAFX, how to update maximized-Undecorated window's width and height on screen when taskbar's location gets changed?

Viewed 66

What I am trying to do is this...enter image description here1

I am trying to add the ability for my Undecorated-window to automatically resize when its in maximize and the taskbar's location gets changed.

I have gotten close by using a onMouseMove to detect the screen size change through getVisualBounds() and checking for that. A problem with onMouseMove() is it only resizes when the mouse is moved over the window and not after the taskbar has been changed.

Here is the Demo Code I came up with.

/////// controller Class////////
private Stage stage;
boolean lock = false;
ObservableList<Screen> screenSizes;

public void setUp(Stage stage){
        this.stage = stage;
        screenSizes  = Screen.getScreensForRectangle(new Rectangle2D(this.stage.getX(), this.stage.getY(), this.stage.getWidth(), this.stage.getHeight()));
}


@FXML
protected void onMaximizedButton() {
        if(lock == true){
            stage.setMaximized(false);
            lock = false;
        }else{
            stage.setMaximized(true);
            lock = true;
            stage.setX(screenSizes.get(0).getVisualBounds().getMinX());
            stage.setY(screenSizes.get(0).getVisualBounds().getMinY());
            stage.setWidth(screenSizes.get(0).getVisualBounds().getWidth());
            stage.setHeight(screenSizes.get(0).getVisualBounds().getHeight());
        }


}

@FXML
private void onMouseMove(){
        ObservableList<Screen> newScreenSizes = Screen.getScreensForRectangle(new Rectangle2D(stage.getX(), stage.getY(), stage.getWidth(),stage.getHeight()));
        if(!(newScreenSizes.get(0).getVisualBounds().equals(screenSizes.get(0).getVisualBounds()))){
            if(lock == true){
                //stage.setMaximized(true);
                screenSizes = newScreenSizes;
                stage.setX(screenSizes.get(0).getVisualBounds().getMinX());
                stage.setY(screenSizes.get(0).getVisualBounds().getMinY());
                stage.setWidth(screenSizes.get(0).getVisualBounds().getWidth());
                stage.setHeight(screenSizes.get(0).getVisualBounds().getHeight());

            }

        }

}
///// main class////////
@Override
public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Parent root = fxmlLoader.load();
        HelloController helloController = fxmlLoader.getController();

        Scene scene = new Scene(root, 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.show();
        helloController.setUp(stage);


}

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

I am asking if there is a way to get the taskbars' location and update it when it gets changed?

Other things I have thought about but have not done, maybe use Observable list of Screens and add a listener to it and somehow bind it to something to have it auto change when taskbar gets changed?

1 Answers

May be this may not be a right solution, but if you are ready to take the risk of using com.sun.* classes, you can go with the below approach.

import com.sun.glass.ui.Screen;

@Override
public void start(final Stage stage) throws Exception {
    // Your code....

    Screen.setEventHandler(new Screen.EventHandler() {

        @Override
        public void handleSettingsChanged() {
            System.out.println("Resize the window here......");
        }
    });
}
Related