Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8117223

Issue with BorderPane / HBox / making buttons same size.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • fx2.1
    • fx2.1
    • javafx
    • Windows 7, 2.1 b08

      Hi,

      I want to make some buttons in a HBox the same width. Each button should always be as wide as the widest button.

      I overrode layoutChildren to do that. Usually it works the way I want to.

      But under certan circumstances it doesn't.

      In this example I have the HBox in a BorderPane at the bottom. If the Button at the center is wider then the three buttons at the bottom my logic applies only if I mouseover one of the three buttons.
      Although layoutChildren is called with the correct width.

      If the center button is very small, it works as expected.

      This is some strange inconsistent behavior. A bug?
      (using 2.1 b08)


      import javafx.application.Application;
      import javafx.geometry.Insets;
      import javafx.geometry.Pos;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Control;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;
       
       
      public class TestApp3 extends Application {
          public static void main(String[] args) throws Exception {
              launch(args);
          }
       
          public void start(final Stage stage) throws Exception {
       
              BorderPane root = new BorderPane();
              root.setPadding(new Insets(20, 20, 20, 20));
              HBox hBox = new HBox() {
       
                  @Override
                  public void layoutChildren() {
                      super.layoutChildren();
       
                      double maxWidth = 0;
                      double maxHeight = 0;
                      for (Node node : getChildren()) {
                          maxWidth = Math.max(maxWidth, node.getLayoutBounds().getWidth());
                          maxHeight = Math.max(maxHeight, node.getLayoutBounds().getHeight());
                      }
       
                      for (Node node : getChildren()) {
                          ((Button) node).setPrefWidth(maxWidth);
       
                      }
                  }
              };
       
              Control control = new Button("This is very long text.... .Lorem ipsum............mmmmmmmmmmmmmmmmmmmmmmmmmm................");
       
              root.setCenter(control);
       
              hBox.getChildren().addAll(new Button("Ok"), new Button("Cancel"), new Button("Very long Button"));
       
              hBox.setSpacing(10);
              hBox.setAlignment(Pos.BOTTOM_CENTER);
              root.setBottom(hBox);
       
       
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();
          }
      }


            kwwong Kinsley Wong (Inactive)
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: