JavaFX Tutorial - Java HBox.setMargin(Node child, Insets value)








Syntax

HBox.setMargin(Node child, Insets value) has the following syntax.

public static void setMargin(Node child,
  Insets value)

Example

In the following code shows how to use HBox.setMargin(Node child, Insets value) method.

/* w  w  w .  j av a  2s .  com*/
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    HBox hbox = new HBox();
    Button button1 = new Button("Add");
    Button button2 = new Button("Remove");
    HBox.setHgrow(button1, Priority.ALWAYS);
    HBox.setHgrow(button2, Priority.ALWAYS);
    button1.setMaxWidth(Double.MAX_VALUE);
    button2.setMaxWidth(Double.MAX_VALUE);
    hbox.getChildren().addAll(button1, button2);
    
    hbox.setPrefWidth(400);
    
    HBox.setMargin(button1,new Insets(1,1,1,1));
    
    root.getChildren().add(hbox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}