javafx radio button
www.igifoc.aeditm
In JavaFX, a RadioButton
is a user interface control that allows the user to select one option from a group of options. Here's an example of how to create and use RadioButton
controls:
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 MyJavaFXApp extends Application { @Override public void start(Stage primaryStage) { // Create a group of radio buttons ToggleGroup group = new ToggleGroup(); RadioButton radioBtn1 = new RadioButton("Option 1"); radioBtn1.setToggleGroup(group); RadioButton radioBtn2 = new RadioButton("Option 2"); radioBtn2.setToggleGroup(group); RadioButton radioBtn3 = new RadioButton("Option 3"); radioBtn3.setToggleGroup(group); // Put the radio buttons in a layout container VBox vbox = new VBox(10, radioBtn1, radioBtn2, radioBtn3); // Create a scene and set it on the stage Scene scene = new Scene(vbox, 300, 250); primaryStage.setScene(scene); // Show the stage primaryStage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create a group of RadioButton
controls by creating a ToggleGroup
object and setting it as the toggle group for each RadioButton
. We then put the RadioButton
controls in a VBox
layout container, create a scene with the VBox
, and set the scene on the stage. When the user clicks on a RadioButton
, it will become selected and the previously selected RadioButton
in the group will become deselected.