JavaFX - Layout Panes HBox

If we use HBox in the layout in our application, all the nodes are set in a single horizontal row.
The class named HBox of the package javafx.scene.layout represents the HBox pane. This class contains five properties namely −
alignment − This property represents the alignment of the nodes in the bounds of the HBox. You can set value to this property using the setter method setAlignment().
alignment − This property represents the alignment of the nodes in the bounds of the HBox. You can set value to this property using the setter method setAlignment().
fillHeight − This property is of Boolean type and on setting this to true, the resizable nodes in the HBox are resized to the height of the HBox. You can set value to this property using the setter method setFillHeight().
fillHeight − This property is of Boolean type and on setting this to true, the resizable nodes in the HBox are resized to the height of the HBox. You can set value to this property using the setter method setFillHeight().
spacing − This property is of double type and it represents the space between the children of the HBox. You can set value to this property using the setter method setSpacing().
spacing − This property is of double type and it represents the space between the children of the HBox. You can set value to this property using the setter method setSpacing().
In addition to these, this class also provides a couple of methods, which are −
setHgrow() − Sets the horizontal grow priority for the child when contained by an HBox. This method accepts a node and a priority value.
setHgrow() − Sets the horizontal grow priority for the child when contained by an HBox. This method accepts a node and a priority value.
setMargin() − Using this method, you can set margins to the HBox. This method accepts a node and an object of the Insets class (A set of inside offsets for the 4 side of a rectangular area).
setMargin() − Using this method, you can set margins to the HBox. This method accepts a node and an object of the Insets class (A set of inside offsets for the 4 side of a rectangular area).
The following program is an example of the HBox layout. Here, we are inserting a text field and two buttons, play and stop. This is done with a spacing of 10 and each having margins with dimensions – (10, 10, 10, 10).
Save this code in a file with the name HBoxExample.java.
import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Scene;
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 
import javafx.scene.layout.HBox;

public class HBoxExample extends Application {   
   @Override 
   public void start(Stage stage) {       
      //creating a text field   
      TextField textField = new TextField();       
      
      //Creating the play button 
      Button playButton = new Button("Play");       
      
      //Creating the stop button 
      Button stopButton = new Button("stop"); 
       
      //Instantiating the HBox class  
      HBox hbox = new HBox();    
      
      //Setting the space between the nodes of a HBox pane 
      hbox.setSpacing(10);    
      
      //Setting the margin to the nodes 
      hbox.setMargin(textField, new Insets(20, 20, 20, 20)); 
      hbox.setMargin(playButton, new Insets(20, 20, 20, 20)); 
      hbox.setMargin(stopButton, new Insets(20, 20, 20, 20));  
      
      //retrieving the observable list of the HBox 
      ObservableList list = hbox.getChildren();  
      
      //Adding all the nodes to the observable list (HBox) 
      list.addAll(textField, playButton, stopButton);       
      
      //Creating a scene object
      Scene scene = new Scene(hbox);  
      
      //Setting title to the Stage 
      stage.setTitle("Hbox Example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}
Compile and execute the saved java file from the command prompt using the following commands.
javac HBoxExample.java 
java HBoxExample.java

On executing, the above program generates a JavaFX window as shown below.

    33 Lectures 
     7.5 hours 

 Syed Raza

    64 Lectures 
     12.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    20 Lectures 
     4 hours 

 Emenwa Global, Ejike IfeanyiChukwu
 Print
 Add Notes
 Bookmark this page