JavaFX using width/height listener to update controls

2.7k Views Asked by At

I am using JavaFX SplitPanes on my application and I need to change the divider positions when the height and width changes, because I want to keep the divider positions fixed. My code is as follows:

scene.widthProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
         GUIController.resetMainSplitPane();
    }
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
             GUIController.resetMusicSplitPane();
    }
});

and the resetSplitPane methods:

public static void resetMusicSplitPane() {
    musicSplitPane.setDividerPosition(0, 0.7);
    Util.err("height changed");
}

I do get the message 'height changed' however the divider positions have not been changed at all. I think this has something to do with JavaFX performing gui changes which override my changes. In other words; I change the divider position but JavaFX changes it back because its performing layout changes responding to the resizing of the window.

1

There are 1 best solutions below

1
On

Normally, divider automatically adjusts itself whenever the scene is resized (i.e. the size of both sides of the divider increases/decreases). The only reason I can think of, on why, you are trying to set divider position, is not to increase or decrease the height/width of one side of the Divider. This can be achieved by using

SplitPane.setResizableWithParent(paneToBeFixed, Boolean.FALSE);

A small example to show how it works, (example given by Sergey in here)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class TestObjectArray extends Application {

    @Override
    public void start(Stage stage) {
        final SplitPane root = new SplitPane();

        final Pane paneFixed = new StackPane();
        paneFixed.getChildren().add(new Text("Fixed"));

        SplitPane.setResizableWithParent(paneFixed, Boolean.FALSE);

        Pane paneFree = new StackPane();
        paneFree.getChildren().add(new Text("Free"));

        root.getItems().addAll(paneFixed, paneFree);

        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();

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