By associating Checkbox objects with a CheckboxGroup.
The Checkbox
class in Java is used to create checkboxes, not radio buttons. In Java, if you want to create a radio button, you would typically use the RadioButton
class, which is part of the JavaFX library.
Here’s a simple example of creating a radio button in JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RadioButtonExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Radio Button Example");
ToggleGroup group = new ToggleGroup();
RadioButton radioButton1 = new RadioButton("Option 1");
radioButton1.setToggleGroup(group);
RadioButton radioButton2 = new RadioButton("Option 2");
radioButton2.setToggleGroup(group);
VBox vbox = new VBox(radioButton1, radioButton2);
Scene scene = new Scene(vbox, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
In this example, RadioButton
objects are created and added to a ToggleGroup
. A ToggleGroup
ensures that only one radio button within the group can be selected at a time, providing the functionality of a radio button group.