Java · JavaFX · Topic 3
JavaFX Controls
Create and use common JavaFX controls for text, choices, dates, ranges, lists, and progress feedback.
Step 1 of 13
3.1 Label
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.
Label heading = new Label("Student Registration");
heading.setText("Registration Form");
heading.setWrapText(true);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); }
}A window titled Label Demo displays:
Welcome to JavaFXCheck your understanding: Which method changes the text already displayed by a Label?
setText().
Step 2 of 13
3.2 Button
Create a command control and connect it to an action.
Button submit = new Button("Submit");
submit.setOnAction(event -> result.setText("Submitted"));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); }
}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
Accept a single line of editable text.
TextField nameField = new TextField();
nameField.setPromptText("Enter student name");
nameField.setPrefColumnCount(20);
String name = nameField.getText().trim();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); }
}For input Vatsal, clicking Display shows:
Hello VatsalCheck your understanding: How do you read the current value of a TextField?
Call getText().
Step 4 of 13
3.4 PasswordField
Accept a single line while masking its visible characters.
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
Accept and display multiple lines of text.
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
Represent an independent true-or-false choice.
CheckBox java = new CheckBox("Java");
CheckBox python = new CheckBox("Python");
if (java.isSelected()) {
System.out.println("Java selected");
}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); }
}When both boxes are selected and Show is clicked:
Selected: Java PythonCheck your understanding: Which method checks whether a CheckBox is selected?
isSelected().
Step 7 of 13
3.7 RadioButton
Represent one option in a mutually exclusive choice.
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
Group RadioButtons and find the selected option.
ToggleGroup genderGroup = new ToggleGroup();
male.setToggleGroup(genderGroup);
female.setToggleGroup(genderGroup);
male.setSelected(true);
RadioButton selected =
(RadioButton) genderGroup.getSelectedToggle();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); }
}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
Present a compact drop-down list and read its selected value.
ComboBox<String> branch = new ComboBox<>();
branch.getItems().addAll("CSE", "IT", "EC", "Mechanical");
branch.setPromptText("Select branch");
branch.setOnAction(event ->
result.setText("Branch: " + branch.getValue()));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); }
}After choosing IT:
Selected: ITCheck your understanding: How do you read the selected ComboBox item?
comboBox.getValue() or comboBox.getSelectionModel().getSelectedItem().
Step 10 of 13
3.10 ListView
Display a scrollable list with a selection model.
ListView<String> languages = new ListView<>();
languages.getItems().addAll("Java", "Python", "C++", "JavaScript");
languages.setPrefHeight(120);
String selected = languages.getSelectionModel().getSelectedItem();languages.getSelectionModel().setSelectionMode(
SelectionMode.MULTIPLE);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); }
}After selecting Java and clicking the button:
Selected: JavaCheck your understanding: Which object controls ListView selection?
Its SelectionModel, obtained with getSelectionModel().
Step 11 of 13
3.11 DatePicker
Let the user choose a calendar date as a LocalDate.
DatePicker birthDate = new DatePicker();
birthDate.setPromptText("Date of birth");
LocalDate selectedDate = birthDate.getValue();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); }
}For a selected date of 2026-10-07:
Selected Date: 2026-10-07Check your understanding: What type does DatePicker.getValue() return?
LocalDate.
Step 12 of 13
3.12 Slider
Select a numeric value from a bounded continuous range.
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
Display progress from 0.0 to 1.0 and combine controls in a complete form.
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);
}
}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.| Value | Visual meaning |
|---|---|
| 0.0 | 0% |
| 0.5 | 50% |
| 1.0 | 100% |
| -1.0 | Indeterminate progress |
Check your understanding: What progress value represents 75 percent?
0.75.