DEV Community

Cover image for CheckBox
Paul Ngugi
Paul Ngugi

Posted on

CheckBox

A CheckBox is used for the user to make a selection. Like Button, CheckBox inherits all the properties such as onAction, text, graphic, alignment, graphicTextGap, textFill, contentDisplay from ButtonBase and Labeled, as shown in Figure below. Additionally, it provides the selection property to indicate whether a check box is selected.

Image description

Here is an example of a check box with text US, a graphic image, green text color, and black border, and initially selected.

CheckBox chkUS = new CheckBox("US");
chkUS.setGraphic(new ImageView("image/usIcon.gif"));
chkUS.setTextFill(Color.GREEN);
chkUS.setContentDisplay(ContentDisplay.LEFT);
chkUS.setStyle("-fx-border-color: black");
chkUS.setSelected(true);
chkUS.setPadding(new Insets(5, 5, 5, 5));

Image description

When a check box is clicked (checked or unchecked), it fires an ActionEvent. To see if a check box is selected, use the isSelected() method.

We now write a program that adds two check boxes named Bold and Italic to the preceding example to let the user specify whether the message is in bold or italic, as shown in Figure below.

Image description

There are at least two approaches to writing this program. The first is to revise the preceding ButtonDemo class to insert the code for adding the check boxes and processing their events. The second is to define a subclass that extends ButtonDemo. Please implement the first approach as an exercise. The program below gives the code to implement the second approach.

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;

public class CheckBoxDemo extends ButtonDemo {
    @Override // Override the getPane() method in the super class
    protected BorderPane getPane() {
        BorderPane pane = super.getPane();

        Font fontBoldItalic = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 20);
        Font fontBold = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
        Font fontItalic = Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.ITALIC, 20);
        Font fontNormal = Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.REGULAR, 20);

        text.setFont(fontNormal);

        VBox paneForCheckBoxes = new VBox(20);
        paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5));
        paneForCheckBoxes.setStyle("-fx-border-color: green");
        CheckBox chkBold = new CheckBox("Bold");
        CheckBox chkItalic = new CheckBox("Italic");
        paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
        pane.setRight(paneForCheckBoxes);

        EventHandler<ActionEvent> handler = e -> {
            if(chkBold.isSelected() && chkItalic.isSelected()) {
                text.setFont(fontBoldItalic); // Both check boxes checked
            }
            else if(chkBold.isSelected()) {
                text.setFont(fontBold); // The Bold check box checked
            }
            else if(chkItalic.isSelected()) {
                text.setFont(fontItalic); // The Italic check box checked
            }
            else {
                text.setFont(fontNormal); // The check boxes unchecked
            }
        };

        chkBold.setOnAction(handler);
        chkItalic.setOnAction(handler);

        return pane; // Return a new pane
    }

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

Enter fullscreen mode Exit fullscreen mode

CheckBoxDemo extends ButtonDemo and overrides the getPane() method (line 15). The new getPane() method invokes the super.getPane() method from the ButtonDemo class to obtain a border pane that contains the buttons and a text (line 16). The check boxes are created and added to paneForCheckBoxes (lines 28–30). paneForCheckBoxes is added to the border pane (lines 31).

The handler for processing the action event on check boxes is created in lines 33–46. It sets the appropriate font based on the status of the check boxes.

The start method for this JavaFX program is defined in ButtonDemo and inherited in CheckBoxDemo. So when you run CheckBoxDemo, the start method in ButtonDemo is invoked. Since the getPane() method is overridden in CheckBoxDemo, the method in CheckBoxDemo is invoked from line 40 in the post, ButtonDemo.java.

Top comments (0)