Dr. Vatsal Shah
Subject Material

Java · JavaFX · Topic 3

JavaFX Controls

Create and use common JavaFX controls for text, choices, dates, ranges, lists, and progress feedback.

Topic progress · 1 of 13 sections

Step 1 of 13

3.1 Label

Learning objective

Display non-editable text and update it at runtime.

A Label displays read-only text, a graphic, or both. It commonly identifies another control or shows a result.

Create and update a Label
Label heading = new Label("Student Registration");
heading.setText("Registration Form");
heading.setWrapText(true);
LabelDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LabelDemo extends Application {
    @Override
    public void start(Stage stage) {
        Label label = new Label("Welcome to JavaFX");
        stage.setScene(new Scene(new VBox(10, label), 300, 150));
        stage.setTitle("Label Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
A window titled Label Demo displays:
Welcome to JavaFX
Check your understanding: Which method changes the text already displayed by a Label?

setText().

Step 2 of 13

3.2 Button

Learning objective

Create a command control and connect it to an action.

Button click action
Button submit = new Button("Submit");
submit.setOnAction(event -> result.setText("Submitted"));
ButtonDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonDemo extends Application {
    @Override
    public void start(Stage stage) {
        Label message = new Label("Click the button");
        Button button = new Button("Click Me");
        button.setOnAction(e -> message.setText("Button clicked!"));
        stage.setScene(new Scene(new VBox(15, message, button), 300, 200));
        stage.setTitle("Button Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
Before clicking: Click the button
After clicking: Button clicked!
Check your understanding: Which method registers a Button action handler?

setOnAction(...).

Step 3 of 13

3.3 TextField

Learning objective

Accept a single line of editable text.

Single-line input
TextField nameField = new TextField();
nameField.setPromptText("Enter student name");
nameField.setPrefColumnCount(20);

String name = nameField.getText().trim();
TextFieldDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldDemo extends Application {
    @Override
    public void start(Stage stage) {
        TextField name = new TextField();
        name.setPromptText("Enter your name");
        Button display = new Button("Display");
        Label result = new Label();
        display.setOnAction(e -> result.setText("Hello " + name.getText()));
        stage.setScene(new Scene(new VBox(10, name, display, result), 350, 200));
        stage.setTitle("TextField Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
For input Vatsal, clicking Display shows:
Hello Vatsal
Check your understanding: How do you read the current value of a TextField?

Call getText().

Step 4 of 13

3.4 PasswordField

Learning objective

Accept a single line while masking its visible characters.

Masked input
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("Enter password");
String password = passwordField.getText();
Check your understanding: Does PasswordField encrypt the entered password?

No. It only masks the visual display; secure storage still requires proper password handling.

Step 5 of 13

3.5 TextArea

Learning objective

Accept and display multiple lines of text.

Multiline input
TextArea address = new TextArea();
address.setPromptText("Enter address");
address.setPrefRowCount(4);
address.setWrapText(true);
Check your understanding: Which control is suitable for a postal address or long comment?

TextArea.

Step 6 of 13

3.6 CheckBox

Learning objective

Represent an independent true-or-false choice.

Independent selections
CheckBox java = new CheckBox("Java");
CheckBox python = new CheckBox("Python");

if (java.isSelected()) {
    System.out.println("Java selected");
}
CheckBoxDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxDemo extends Application {
    @Override
    public void start(Stage stage) {
        CheckBox java = new CheckBox("Java");
        CheckBox python = new CheckBox("Python");
        Button show = new Button("Show");
        Label result = new Label();
        show.setOnAction(e -> {
            String subjects = "";
            if (java.isSelected()) subjects += "Java ";
            if (python.isSelected()) subjects += "Python";
            result.setText("Selected: " + subjects.trim());
        });
        stage.setScene(new Scene(new VBox(10, java, python, show, result), 350, 220));
        stage.setTitle("CheckBox Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
When both boxes are selected and Show is clicked:
Selected: Java Python
Check your understanding: Which method checks whether a CheckBox is selected?

isSelected().

Step 7 of 13

3.7 RadioButton

Learning objective

Represent one option in a mutually exclusive choice.

Radio-button choices
RadioButton male = new RadioButton("Male");
RadioButton female = new RadioButton("Female");
Check your understanding: Can two ungrouped RadioButtons be selected together?

Yes. Mutual exclusion requires a ToggleGroup.

Step 8 of 13

3.8 ToggleGroup

Learning objective

Group RadioButtons and find the selected option.

Mutually exclusive selection
ToggleGroup genderGroup = new ToggleGroup();
male.setToggleGroup(genderGroup);
female.setToggleGroup(genderGroup);
male.setSelected(true);

RadioButton selected =
        (RadioButton) genderGroup.getSelectedToggle();
RadioButtonDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RadioButtonDemo extends Application {
    @Override
    public void start(Stage stage) {
        RadioButton male = new RadioButton("Male");
        RadioButton female = new RadioButton("Female");
        ToggleGroup gender = new ToggleGroup();
        male.setToggleGroup(gender);
        female.setToggleGroup(gender);
        Label result = new Label();
        male.setOnAction(e -> result.setText("Male selected"));
        female.setOnAction(e -> result.setText("Female selected"));
        stage.setScene(new Scene(new VBox(10, male, female, result), 300, 200));
        stage.setTitle("RadioButton Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
Selecting Female displays:
Female selected

Selecting Male automatically deselects Female.
Check your understanding: What value does getSelectedToggle() return when no option is selected?

null.

Step 9 of 13

3.9 ComboBox

Learning objective

Present a compact drop-down list and read its selected value.

Branch selection
ComboBox<String> branch = new ComboBox<>();
branch.getItems().addAll("CSE", "IT", "EC", "Mechanical");
branch.setPromptText("Select branch");
branch.setOnAction(event ->
        result.setText("Branch: " + branch.getValue()));
ComboBoxDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxDemo extends Application {
    @Override
    public void start(Stage stage) {
        ComboBox<String> branch = new ComboBox<>();
        branch.getItems().addAll("IT", "Computer", "Civil", "Mechanical");
        branch.setPromptText("Select Branch");
        Label result = new Label();
        branch.setOnAction(e -> result.setText("Selected: " + branch.getValue()));
        stage.setScene(new Scene(new VBox(15, branch, result), 350, 200));
        stage.setTitle("ComboBox Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
After choosing IT:
Selected: IT
Check your understanding: How do you read the selected ComboBox item?

comboBox.getValue() or comboBox.getSelectionModel().getSelectedItem().

Step 10 of 13

3.10 ListView

Learning objective

Display a scrollable list with a selection model.

Programming-language list
ListView<String> languages = new ListView<>();
languages.getItems().addAll("Java", "Python", "C++", "JavaScript");
languages.setPrefHeight(120);

String selected = languages.getSelectionModel().getSelectedItem();
Allow multiple selection
languages.getSelectionModel().setSelectionMode(
        SelectionMode.MULTIPLE);
ListViewDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListViewDemo extends Application {
    @Override
    public void start(Stage stage) {
        ListView<String> list = new ListView<>();
        list.getItems().addAll("Java", "Python", "AI", "Data Science");
        list.setPrefHeight(120);
        Button show = new Button("Show Selection");
        Label result = new Label();
        show.setOnAction(e -> result.setText(
                "Selected: " + list.getSelectionModel().getSelectedItem()));
        stage.setScene(new Scene(new VBox(10, list, show, result), 350, 260));
        stage.setTitle("ListView Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
After selecting Java and clicking the button:
Selected: Java
Check your understanding: Which object controls ListView selection?

Its SelectionModel, obtained with getSelectionModel().

Step 11 of 13

3.11 DatePicker

Learning objective

Let the user choose a calendar date as a LocalDate.

Choose a date of birth
DatePicker birthDate = new DatePicker();
birthDate.setPromptText("Date of birth");

LocalDate selectedDate = birthDate.getValue();
DatePickerDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DatePickerDemo extends Application {
    @Override
    public void start(Stage stage) {
        DatePicker date = new DatePicker();
        Button show = new Button("Show Date");
        Label result = new Label();
        show.setOnAction(e -> result.setText(date.getValue() == null
                ? "Select a date"
                : "Selected Date: " + date.getValue()));
        stage.setScene(new Scene(new VBox(10, date, show, result), 320, 180));
        stage.setTitle("DatePicker Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
For a selected date of 2026-10-07:
Selected Date: 2026-10-07
Check your understanding: What type does DatePicker.getValue() return?

LocalDate.

Step 12 of 13

3.12 Slider

Learning objective

Select a numeric value from a bounded continuous range.

Rating slider
Slider rating = new Slider(0, 10, 5);
rating.setShowTickLabels(true);
rating.setShowTickMarks(true);
rating.setMajorTickUnit(1);
rating.setSnapToTicks(true);

int score = (int) Math.round(rating.getValue());
Check your understanding: If a Slider has a current value of 7.6, what should be done when the application requires an integer rating?

Round or explicitly convert the double according to the application rule.

Step 13 of 13

3.13 ProgressBar

Learning objective

Display progress from 0.0 to 1.0 and combine controls in a complete form.

ControlsDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ControlsDemo extends Application {
    @Override
    public void start(Stage stage) {
        Label result = new Label("Complete the form");
        TextField name = new TextField();
        name.setPromptText("Student name");

        ComboBox<String> branch = new ComboBox<>();
        branch.getItems().addAll("CSE", "IT", "EC");
        branch.setPromptText("Select branch");

        Slider progressValue = new Slider(0, 100, 50);
        progressValue.setShowTickLabels(true);
        ProgressBar progress = new ProgressBar(0.5);

        Button submit = new Button("Submit");
        submit.setOnAction(event -> {
            progress.setProgress(progressValue.getValue() / 100.0);
            result.setText("Student: " + name.getText()
                    + ", Branch: " + branch.getValue());
        });

        GridPane root = new GridPane();
        root.setHgap(10);
        root.setVgap(10);
        root.addRow(0, new Label("Name"), name);
        root.addRow(1, new Label("Branch"), branch);
        root.addRow(2, new Label("Completion"), progressValue);
        root.addRow(3, progress, submit);
        root.add(result, 0, 4, 2, 1);

        stage.setScene(new Scene(root, 460, 280));
        stage.setTitle("JavaFX Controls");
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
Expected result
The window displays a name field, branch list, slider,
progress bar, Submit button, and result label.

Example after clicking Submit:
Student: Vatsal, Branch: IT

The progress bar matches the selected slider percentage.
Progress values
ValueVisual meaning
0.00%
0.550%
1.0100%
-1.0Indeterminate progress
Check your understanding: What progress value represents 75 percent?

0.75.