Reputation: 889
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!
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 :
Okay for this one, the divider is at 1, so only one pane is showing, now I will maximize the window :
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!
Upvotes: 1
Views: 1133