1

I have a split pane with a initial divider position set to 0.2, the problem is that after running the application the position is changed to 0.5, I read that it is due to stage.setMaximized(true);
I checked this question, but the answers imply that the current split pane and the main stage are in the same class, but in my case, I have to work in a controller class.

I tried to add a listener to the stage showing property Main.getStage().showingProperty().addListener(...) in the initialize() method, but it doesn't work, it says stage is null, I guess it's because initialize() runs before Main.start()

So how can I make my split pane start with a fixed divider position?

Thanks!

EDIT :

I made a simple example to expose my problem :

Main

@Override
public void start(Stage primaryStage) throws Exception {
  FXMLLoader loader=new FXMLLoader(getClass().getResource("views/test.fxml"));
  mainStage=primaryStage;
  mainStage.setScene(new Scene(loader.load()));
  mainStage.show();
  Controller_Test ct=loader.getController();
  System.out.println("Main:"+ct.sp.getDividerPositions()[0]);
  ct.getDividerPos();
}

Controller_Test

@FXML
public SplitPane sp;

@FXML
public void initialize() {
    System.out.println("Initialize:"+sp.getDividerPositions()[0]);
}

public void getDividerPos(){
    System.out.println("Method:"+sp.getDividerPositions()[0]);
}

Now I set the split pane divider at 1, which means the second pane should not show, I run the application and here is the result :

window at launch

Okay for this one, the divider is at 1, so only one pane is showing, now I will maximize the window :

enter image description here

The divider position clearly changed. If you read the code I provide previously, you can see 3 print lines as checkpoints, here is their output after running the application :

Initialize:1.0
Main:0.9933110367892977
Method:0.9933110367892977

Thanks!

Community
  • 1
  • 1
adxl
  • 829
  • 1
  • 9
  • 19
  • [mcve] please .. – kleopatra Feb 24 '20 at 23:53
  • 2
    See [`SplitPane#setResizableWithParent(Node,Boolean)`](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/SplitPane.html#setResizableWithParent(javafx.scene.Node,java.lang.Boolean)). – Slaw Feb 25 '20 at 00:14
  • @kleopatra, I edited the post and added an example. – adxl Feb 25 '20 at 09:58
  • @Slaw, my question isn't related to split pane size. – adxl Feb 25 '20 at 09:58
  • 2
    When you resize the window the `SplitPane` changes size. This causes the `SplitPane` to resize its children by default, leading to the divider(s) changing position, but that behavior can be disabled with the method I linked to. – Slaw Feb 25 '20 at 11:23
  • @Slaw, thanks for your answer, I tried your method but the result is slightly the same, the red pane is thinner than before but still showing, I guess the divider at 1 should not let the second pane to appear. Here is what I tried : ```SplitPane.setResizableWithParent(sp,false);``` ```for(Node node : sp.getItems()){ SplitPane.setResizableWithParent(node,false); }``` `System.out.println(SplitPane.isResizableWithParent(sp));` After running, the divider position is : 0.9933110367892977. – adxl Feb 25 '20 at 14:17
  • 2
    Problem seems to be related to making every child not resizable-with-parent. If you only make the rightmost node not resizable-with-parent then the behavior would seem to fit your needs. – Slaw Feb 25 '20 at 15:57
  • @Slaw, problem solved, except that I had to make the LEFT node not resizable, (making the right one seems to only work with the divider at 1). Thanks for your help and efforts! – adxl Feb 25 '20 at 16:53

0 Answers0