1

I have a SplitPane in my main GUI that is behaving strangely. I am trying to save the position between executions of the program and do so by writing/reading a simple text file.

However, I am noticing that the getPosition() method returns an incorrect value when saving it to the file.

When exiting the program, I am saving the position with this code:

Settings.setSplitPaneLocation(Global.commentMasterController.getSplitPane().getDividers().get(0).getPosition());
  • Global is a Singleton that holds references to my main GUI controllers.
  • Settings is another Singleton that contains the values loaded from the settings file.

Upon loading the program, I set the position of the SplitPane by loading the last saved value from the Settings class:

splitPane.setDividerPosition(0, Settings.getSplitPaneLocation());

The problem is, when I load the program, even if I don't adjust the SplitPane position at all (load the program and immediately exit), the position that is saved is different from the position it was initially set to.

Here is the output, showing 3 consecutive runs of the program. Again, this is just running the program and immediately exiting; I don't adjust the position of any controls at all:

setDividerPosition(): 0.9247730220492867
getDividerPosition(): 0.9105058365758755

setDividerPosition(): 0.9105058365758755
getDividerPosition(): 0.893644617380026

setDividerPosition(): 0.893644617380026
getDividerPosition(): 0.874189364461738

As you can see, getDividerPosition() returns a different value than the actual position of the divider.

I have checked the max/min sizes of the child nodes within the SplitPane and the min size is set to 0.

Zephyr
  • 9,885
  • 4
  • 28
  • 63

2 Answers2

1

From the Javadocs for SplitPane:

A divider's position ranges from 0 to 1.0(inclusive). A position of 0 will place the divider at the left/top most edge of the SplitPane plus the minimum size of the node. A position of 1.0 will place the divider at the right/bottom most edge of the SplitPane minus the minimum size of the node. A divider position of 0.5 will place the the divider in the middle of the SplitPane. Setting the divider position greater than the node's maximum size position will result in the divider being set at the node's maximum size position. Setting the divider position less than the node's minimum size position will result in the divider being set at the node's minimum size position. Therefore the value set in setDividerPosition(int, double) and setDividerPositions(double...) may not be the same as the value returned by getDividerPositions().

(My emphasis.) Without knowing more about what you are doing, it is hard to be precise about the behavior you are observing, but I suspect that the value you are setting is being adjusted to account for the min/max sizes of the content nodes.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • I updated my question with more detail/code. I've checked the minimum size of the children and the problem persists. – Zephyr Sep 21 '16 at 15:00
0

I've had a similar problem and found this post without a real solution, so maybe it helps someone in the future:

Try to use Platform.runLater(). Apparently there's something clashing when you start an application and want to change GUI-elements at the same time.

For your specific question it would be: Platform.runLater(() -> splitPane.setDividerPosition(0, Settings.getSplitPaneLocation()));

I've found this hint in the following blogpost: http://broadlyapplicable.blogspot.de/2015/03/javafx-capture-restore-splitpane.html

How to use the javafx.application.Platform.runLater method to set the divider position when your application launches. (This technique is necessary due to how the application starts up and initializes)

yoshegg
  • 69
  • 9