JavaFX: Creating checkboxes

This entry is part 29 of 55 in the series JavaFX

Checkboxes allow multiple selections.  Here in this post, we also look at how to put 2 Hboxes into a Vbox where each Hbox is for one Checkbox.

Creating a checkbox is relatively easy.

CheckBox cb1 = new CheckBox("Rock");

Consider the the example below.

package javafxapplication24;
 
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
 
public class JavaFXApplication24 extends Application {
 
    @Override
    public void start(Stage stage) {
 
        stage.setTitle("JavaFX Program!");
        
        VBox vbox_pane = new VBox();
        vbox_pane.setAlignment(Pos.CENTER);
        
        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();
        
        hbox1.setPadding(new Insets(15, 12, 15, 12));
        hbox1.setAlignment(Pos.CENTER_LEFT);
        
        hbox2.setPadding(new Insets(15, 12, 15, 12));
        hbox2.setAlignment(Pos.CENTER_LEFT);
 
        CheckBox cb1 = new CheckBox("Rock");
        CheckBox cb2 = new CheckBox("Country");
 
        hbox1.getChildren().add(cb1);   
        hbox2.getChildren().add(cb2);  
        
        vbox_pane.getChildren().addAll(hbox1,hbox2);
 
        Scene scene = new Scene(vbox_pane, 300, 250);
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

checkbox

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 {

    @Override
    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);
        button.setMaxWidth(Double.MAX_VALUE);

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

        Scene scene = new Scene(hboxPane, 300, 250);
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

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

palways

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

p_always1

JavaFX: Putting StackPane in HBox

This entry is part 12 of 55 in the series JavaFX

We know that HBox is a region in BorderPane and we could understand that BorderPane is flat and it is divided into a few regions as shown below.

borderpane

StackPane provides a z-order for us to put our elements and in a way, the elements are leveled with one above the other.

We can put a StackPane on a HBox by creating an addStackPane method and pass the Hbox object into the StackPane to have the StackPane added to the HBox.

In the example below, I have:

addStackPane(hbox);

The purpose of the above line is to add the StackPane into the HBox and not the other way round.

In our method implementation in line 41, we have:

private void addStackPane(HBox hb)

This is to bring in the hb, which is an instance of HBox in to the method.

The actual line that attaches the StackPane into the HBox is:

hb.getChildren().add(stack);
StackPane in HBox
package javafxapplication11;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class JavaFXApplication11 extends Application {

    @Override
    public void start(Stage stage) {

        BorderPane border = new BorderPane();

        border.setPrefSize(500, 400);

        HBox hbox = addHorizontalBox();

        addStackPane(hbox);

        border.setBottom(hbox);

        Scene scene = new Scene(border);
        stage.setScene(scene);
        stage.setTitle("Layout Example");
        stage.show();
    }

    private void addStackPane(HBox hb) {

        StackPane stack = new StackPane();
        Rectangle xIcon = new Rectangle(35.0, 35.0);
        xIcon.setFill(Color.GREY);

        Text xText = new Text("X");
        xText.setFont(Font.font("Verdana", FontWeight.BOLD, 18));

        stack.getChildren().addAll(xIcon, xText);
        stack.setAlignment(Pos.CENTER_RIGHT);

        StackPane.setMargin(xText, new Insets(0, 10, 0, 0));

        hb.getChildren().add(stack);
        HBox.setHgrow(stack, Priority.ALWAYS);

    }

    private HBox addHorizontalBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);   // Gap between nodes
        hbox.setStyle("-fx-background-color: blue;");

        Button buttonAccept = new Button("Accept");
        buttonAccept.setPrefSize(100, 20);

        Button buttonNext = new Button("Next");
        buttonNext.setPrefSize(100, 20);

        hbox.getChildren().addAll(buttonAccept, buttonNext);

        return hbox;
    }

    public static void main(String[] args) {
        launch(args);
    }

}

 

sp

JavaFX: BorderPane class

This entry is part 9 of 55 in the series JavaFX

BorderPane lays out children in the scene in top, left, right, bottom, and center positions similar to the illustration in the screenshot below.

borderpane

 

There are a few panes commonly used in JavaFX in laying out the boxes in the scene.  In the screenshot above, we have the Top pane and to place items in the Top pane, we can use the HBox pane class.

Consider the example below, the addHorizontalBox method basically set up the 2 buttons for display in the top panel.

package javafxapplication9;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
 
public class JavaFXApplication9 extends Application {
 
    @Override
    public void start(Stage stage) {
 
        BorderPane border = new BorderPane();
        
        border.setPrefSize(500,400);
        
        HBox hbox = addHorizontalBox();
        border.setTop(hbox);

        Scene scene = new Scene(border);
        stage.setScene(scene);
        stage.setTitle("Layout Sample");
        stage.show();
    }
    
    private HBox addHorizontalBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);   // Gap between nodes
        hbox.setStyle("-fx-background-color: blue;");

        Button buttonAccept = new Button("Accept");
        buttonAccept.setPrefSize(100, 20);

        Button buttonNext = new Button("Next");
        buttonNext.setPrefSize(100, 20);
        
        hbox.getChildren().addAll(buttonAccept, buttonNext);
        
        return hbox;
    }
 
    public static void main(String[] args) {
        launch(args);
    }
    
}

borderpane1