In this Java GUI article we are going to learn about Creating HBox Layout in JavaFX, so HBox lays
out its children in a single horizontal row. If the hbox has a border or padding set, then the
contents will be layed out within those insets.
Also you can check Python GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
5: wxPython GUI Development Tutorials
You can create HBox layout or container by creating the object of HBox class like this.
1 |
HBox hbox = new HBox(); |
So now this is the complete code for Java GUI – Creating HBox Layout in JavaFX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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 HBoxLayout extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("JAVAFX HBox Layout"); HBox hbox = new HBox(); Scene scene = new Scene(hbox, 500, 400); hbox.setPadding(new Insets(15,12,15,12)); hbox.setSpacing(10); Button btn1 = new Button("Click"); btn1.setPrefSize(100,20); Button btn2 = new Button("Close"); btn2.setPrefSize(100,20); Button btn3 = new Button("Update"); btn3.setPrefSize(100,20); hbox.getChildren().addAll(btn1, btn2, btn3); stage.setScene(scene); stage.show(); } } |
Every JavaFX application should have a container, a container is like layout. as we
have created an HBox container in the above code.
1 |
HBox hbox = new HBox(); |
Also for every JavaFX application we need to create a Scene object. in the scene we need
to add our container with the width and height of the window, if you want to colorize your
window, you can do it in here. you can see that we have given 500 width and 400 height for the
window.
1 |
Scene scene = new Scene(hbox, 500, 400); |
In here we have created the padding and spacing for our HBox Layout in JavaFX.
1 2 |
hbox.setPadding(new Insets(15,12,15,12)); hbox.setSpacing(10); |
There are the three buttons that we want to add in JavaFX HBox Layout.
1 2 3 4 5 6 7 8 |
Button btn1 = new Button("Click"); btn1.setPrefSize(100,20); Button btn2 = new Button("Close"); btn2.setPrefSize(100,20); Button btn3 = new Button("Update"); btn3.setPrefSize(100,20); |
When you create a widget in JavaFX, for example button,label,combobox or some other
widgets, you need to add that to your container, in here we need to add our three buttons
to the HBox container.
1 |
hbox.getChildren().addAll(btn1, btn2, btn3); |
At the end you need to set your scene object to the stage of the window, and show the window.
1 2 |
stage.setScene(scene); stage.show(); |
Run the complete code and this will be the result.
Subscribe and Get Free Video Courses & Articles in your Email