JavaFX: Hbox with priority always

This entry is part 26 of 55 in the series JavaFX

As the name implies, if you layout the elements in a HBox, the elements will be put horizontally.

To cater to user resizing the window, Javafx provides us a setting for us to always expand an element to take up extra space.

The best way to see this is to compare the output with or without priority.Always setting.

For Hbox, the priority always setting is set by method setHgrow().  For Vbox, it will be setVgrow().

For this example, the Hbox is the only pane that we have and so we have hboxPane as the item in the scene in line 31.

package javafxapplication23;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Priority;

public class JavaFXApplication23 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        HBox hboxPane = new HBox(10); // spacing = 10       
        hboxPane.setPadding(new Insets(10, 10, 10, 10));

        Label lb = new Label("Click Submit To Send:");
        Button button = new Button("Submit");

        hboxPane.setHgrow(button, Priority.ALWAYS);

        hboxPane.getChildren().addAll(lb, button);

        Scene scene = new Scene(hboxPane, 300, 250);

    public static void main(String[] args) {

The screenshot below has the submit button set with priority always.


If we comment out line 26, this is what we have:


Series Navigation<< JavaFX: Using a Click Event Handler
JavaFX: Entering text using TextField >>

Leave a comment

Leave a Reply