0 Replies Latest reply on Dec 4, 2012 1:41 PM by csh

    Layout problems with bound height

    csh
      Hi,

      I have a problem concerning layout. I want to create a custom control ("Number Spinner Control").

      Therefore I need a text field with two buttons next to it. The height of the buttons is half the height of the text field - 1px (due to padding).

      Therefore I bind the button height to this:
      button1.maxHeightProperty().bind(textField.heightProperty().divide(2.0).subtract(1));
      button1.minHeightProperty().bind(button1.maxHeightProperty());

      It kind of works, but the layout bounds are calculated wrong the first time: If I start the scene, it takes too much space (the buttons height is calculated as if it wasn't bound).
      Therefore I have a gap in my HBox. When I resize the stage, the layout it recalculated correctly.

      Another strange thing I have noticed: If I remove the subtract(1) from the binding I don't get that behavior (but the buttons don't sit correctly in place).

      Any explanations for that behavior?
      Any workarounds?
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBoxBuilder;
      import javafx.stage.Stage;
      
      
      public class TestApp4 extends Application {
      
          public static void main(String[] args) {
              launch(args);
          }
      
          @Override
          public void start(final Stage stage) throws Exception {
              HBox root = new HBox();
      
              TextField textField = new TextField();
              Button button1 = new Button();
      
              Button button2 = new Button();
      
              button1.maxHeightProperty().bind(textField.heightProperty().divide(2.0).subtract(1));
              button1.minHeightProperty().bind(button1.maxHeightProperty());
      
              button2.maxHeightProperty().bind(textField.heightProperty().divide(2.0).subtract(1));
              button2.minHeightProperty().bind(button1.maxHeightProperty());
      
      
              root.getChildren().addAll(textField, VBoxBuilder.create().children(button1, button2).build());
              root.setStyle("-fx-border-color:red");
      
              Scene scene = new Scene(VBoxBuilder.create().children(root).build());
              stage.setScene(scene);
              stage.show();
          }
      }