4 Replies Latest reply: Dec 10, 2012 8:34 AM by kapac RSS

    hbox

    kapac
      Hi;
      i want to put a button in a HBox at the right so i write a code
      HBox hb=new HBox();
              hb.setPadding(new Insets(1,1,1,180));
              Button btn = new Button();
              btn.setText("Say 'Hello World'");
               hb.getChildren().add(btn);
      i got a button at the right of the box
      now i want to put a button at the left of the box.how can i proceed ?
      if i write the code following the previous code
              hb.setPadding(new Insets(1,1,1,1));
              Button btn1 = new Button();
              btn.setText("Say 'Hello World1");
               hb.getChildren().add(btn1);
      it is not ok but the inverse helloworld-helloworld1 in the positions of buttons
        • 1. Re: hbox
          James_D
          The padding just puts space around the edge of the Region (HBox in your case) and its content. So
          HBox hb=new HBox();
          hb.setPadding(new Insets(1,1,1,180));
          Button btn = new Button();
          btn.setText("Say 'Hello World'");
          hb.getChildren().add(btn);
          doesn't really put the Button at the right of the HBox, it just puts 180 pixels of padding on the left, pushing the button further right.
          The positioning of Nodes within the HBox is controlled by the alignment, so hb.setAlignment(Pos.TOP_RIGHT); would align the nodes to the right of the HBox when there is additional space.
          i got a button at the right of the box
          now i want to put a button at the left of the box.how can i proceed ?
          HBox is not really designed for this. It will place nodes left to right in the order you add them. The overall alignment of the content is controlled by the HBox's alignment property. You can control the extent to which individual nodes receive extra space using HBox.setHgrow(), but not the positioning of individual nodes.

          To have one button against the left edge of the HBox and one against the right edge, BorderPane and AnchorPane are better choices (which depends on what other content you may want). You could also nest two HBox inside another HBox, one with left alignment and one with right.
          import javafx.application.Application;
          import javafx.geometry.Pos;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.layout.AnchorPane;
          import javafx.scene.layout.BorderPane;
          import javafx.scene.layout.HBox;
          import javafx.scene.layout.Priority;
          import javafx.scene.layout.VBox;
          import javafx.stage.Stage;
          
          public class HBoxPositioning extends Application {
          
               @Override
               public void start(Stage primaryStage) {
                    VBox root = new VBox(5);
                    
                    // Nested HBox
                    HBox outer = new HBox();
                    HBox leftInner = new HBox();
                    HBox rightInner = new HBox();
                    rightInner.setAlignment(Pos.TOP_RIGHT);
                    leftInner.getChildren().add(new Button("Left"));
                    rightInner.getChildren().add(new Button("Right"));
                    HBox.setHgrow(leftInner, Priority.ALWAYS);
                    HBox.setHgrow(rightInner, Priority.ALWAYS);
                    outer.getChildren().addAll(leftInner, rightInner);
                    root.getChildren().add(outer);
                    
                    // BorderPane
                    BorderPane border = new BorderPane();
                    border.setLeft(new Button("Left"));
                    border.setRight(new Button("Right"));
                    root.getChildren().add(border);
                    
                    // AnchorPane
                    AnchorPane anchorPane = new AnchorPane();
                    Button leftButton = new Button("Left");
                    Button rightButton = new Button("Right");
                    anchorPane.getChildren().addAll(leftButton, rightButton);
                    AnchorPane.setLeftAnchor(leftButton, 0.0);
                    AnchorPane.setRightAnchor(rightButton, 0.0);
                    root.getChildren().add(anchorPane);
                    
                    Scene scene = new Scene(root, 400, 200);
                    primaryStage.setScene(scene);
                    primaryStage.show();
               }
          
               public static void main(String[] args) {
                    launch(args);
               }
          }
          • 2. Re: hbox
            MiPa
            Is that what you want to achieve?
            package layouttests;
            
            import javafx.application.Application;
            import javafx.geometry.Insets;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.layout.HBox;
            import javafx.stage.Stage;
            
            public class ButtonTest3 extends Application {
            
                 @Override
                 public void start(Stage primaryStage) throws Exception {
            
                      HBox root = new HBox();
                      
                      Button btn1 = new Button();
                      btn1.setText("Say 'Hello World1'");
                      HBox.setMargin(btn1, new Insets(1, 1, 1, 1));
                      root.getChildren().add(btn1);
            
                      Button btn2 = new Button();
                      btn2.setText("Say 'Hello World2");
                      HBox.setMargin(btn2, new Insets(1, 1, 1, 180));
                      root.getChildren().add(btn2);
            
                      Scene scene = new Scene(root);
            
                      primaryStage.setTitle(getClass().getName());
                      primaryStage.setScene(scene);
                      primaryStage.show();
                 }
            
                 public static void main(String[] args) {
                      launch(args);
                 }
            
            }
            If yes, it would probably be much better to use for example an AchorPane like this.
            package layouttests;
            
            import javafx.application.Application;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.layout.AnchorPane;
            import javafx.stage.Stage;
            
            public class ButtonTest4 extends Application {
            
                 @Override
                 public void start(Stage primaryStage) throws Exception {
            
                      AnchorPane root = new AnchorPane();
                      
                      Button btn1 = new Button();
                      btn1.setText("Say 'Hello World1'");
                      AnchorPane.setTopAnchor(btn1, 1.0);
                      AnchorPane.setLeftAnchor(btn1, 1.0);
                      root.getChildren().add(btn1);
            
                      Button btn2 = new Button();
                      btn2.setText("Say 'Hello World2");
                      AnchorPane.setTopAnchor(btn2, 1.0);
                      AnchorPane.setRightAnchor(btn2, 1.0);
                      root.getChildren().add(btn2);
            
                      Scene scene = new Scene(root, 600, 400);
            
                      primaryStage.setTitle(getClass().getName());
                      primaryStage.setScene(scene);
                      primaryStage.show();
                 }
            
                 public static void main(String[] args) {
                      launch(args);
                 }
            
            }
            • 3. Re: hbox
              MiPa
              Why did James answer not show up in my browser before I posted mine. I checked it. Would have saved me some time.
              • 4. Re: hbox
                kapac
                thank you to all
                regards