JavaFX Tutorial 08 RadioButton GUI
Code Source :
@Override
public void start(Stage primaryStage) {
// cretae Group
Group gr = new Group();
VBox v = new VBox();
ToggleGroup tg = new ToggleGroup();
RadioButton rb1 = new RadioButton("Cocacola");
RadioButton rb2 = new RadioButton("Fanta");
RadioButton rb3 = new RadioButton("Pepsi");
tg.getToggles().addAll(rb1,rb2,rb3);
v.getChildren().addAll(rb1,rb2,rb3);
Scene sc = new Scene(v,600,400);
primaryStage.setScene(sc);
primaryStage.setTitle("Tutorial JavaFX");
primaryStage.show();
}
Libraries Importing :
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
JavaFX RadioButton
Creating a RadioButton
Adding a RadioButton to the Scene Graph
Reading Selected State
ToggleGroup
A JavaFX RadioButton is a button that can be selected or not selected. The RadioButton is very similar to the JavaFX ToggleButton, but with the difference that a RadioButton cannot be "unselected" once selected. If RadioButtons are part of a ToggleGroup then once a RadioButton has been selected for the first time, there must be one RadioButton selected in the ToggleGroup .
The JavaFX RadioButton is represented by the class javafx.scene.control.RadioButton. The RadioButton class is a subclass of the ToggleButton class.