2 Replies Latest reply: Feb 26, 2013 4:11 AM by 947038 RSS

    JavaFX SplitPane Divider Position Inconsistent Behaviour

    947038
      I have two StackPanes (pane1 & pane2) being displayed in a SplitPane (splitPane). The SplitPane's divider position is set to .3 in the builder. The seond StackPane (pane2) contains a Button which also sets the SplitPane's divider position to .3.

      Ceteris paribus, the expected behaviour would be that both actions (setting the divider position in the builder & setting the divider position through an action) either work or not.

      Yet, only the latter actually works.

      What changes between the construction of the SplitPane and the onAction of the Button? What hinders the placement of the divider in the builder?
          StackPane pane1 = new StackPane();
          StackPane pane2 = new StackPane();
      
          final SplitPane splitPane = SplitPaneBuilder.create()
                  .items(pane1, pane2)
                  .dividerPositions(new double[] {.3})
                  .orientation(Orientation.HORIZONTAL)
                  .build();
      
          pane2.getChildren().add(ButtonBuilder.create()
                  .text("Divider Position")
                  .onAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent event) {
                          splitPane.setDividerPositions(.3);
                      }
                  })
                  .build());
      
          Scene primaryScene = SceneBuilder.create()
                  .root(splitPane)
                  .build();
      
          primaryStage.setScene(primaryScene);
          primaryStage.setTitle("Name");
          primaryStage.setWidth(500);
          primaryStage.setHeight(500);
          primaryStage.show();
      
          splitPane.setDividerPositions(.3);
      (see also http://stackoverflow.com/questions/15041332/javafx-divider-position-inconsistent-behaviour)
        • 1. Re: JavaFX SplitPane Divider Position Inconsistent Behaviour
          James_D
          There was a [url http://javafx-jira.kenai.com/browse/RT-17229]bug filed for this, though it was closed with a "cannot reproduce". The issue appears to be that the scene's size is smaller than that of its window. This causes an additional layout pass when the window is first displayed; the second layout pass counts as a "window resize" and consequently the divider position of the split pane is not respected on this pass.

          I have a couple of workarounds. One (probably the cleanest) is to set the size of the scene, instead of the size of the window:
          Scene primaryScene = SceneBuilder.create()
                      .root(splitPane)
                      .width(500)
                      .height(500)
                      .build();
           
              primaryStage.setScene(primaryScene);
              primaryStage.setTitle("Name");
              primaryStage.show();
          The other is to wrap a call to splitPane.setDividerPositions(...) in a Platform.runLater(...). This causes the divider positions to be reset after the layout passes have been completed. This feels like a bit of a hack.
              Scene primaryScene = SceneBuilder.create()
                      .root(splitPane)
                      .build();
           
              primaryStage.setScene(primaryScene);
              primaryStage.setTitle("Name");
              primaryStage.setWidth(500);
              primaryStage.setHeight(500);
              primaryStage.show();
              Platform.runLater(new Runnable() {
                @Override
                public void run() {
                  splitPane.setDividerPositions(.3);
                }
              });
          You might want to file a bug, with your original code written as a complete, executable example, and reference RT-17229.
          • 2. Re: JavaFX SplitPane Divider Position Inconsistent Behaviour
            947038
            Thank you very much, perfect answer.

            I have filed a bug at jira and referenced RT-17229:
            http://javafx-jira.kenai.com/browse/RT-28607

            Also, if you want to answer the question on stackoverflow: http://stackoverflow.com/questions/15041332/javafx-splitpane-divider-position-inconsistent-behaviour. Otherwise, I will do that tomorrow by referencing your post.