By using
'ToggleGroup' instance, we can group the radio buttons. If you add 'n' radio
buttons to a group 'group1', then at a time one radio button of that group will
be selected.
Ex
ToggleGroup
toggleGroup = new ToggleGroup();
RadioButton
radioButton1 = new RadioButton("Java");
radioButton1.setToggleGroup(toggleGroup);
radioButton1.setSelected(true);
RadioButton
radioButton2 = new RadioButton("Python");
radioButton2.setToggleGroup(toggleGroup);
RadioButton
radioButton3 = new RadioButton("Haskell");
radioButton3.setToggleGroup(toggleGroup);
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import java.io.FileNotFoundException; import java.net.MalformedURLException; 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 RadioButtonApp extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ToggleGroup toggleGroup = new ToggleGroup(); RadioButton radioButton1 = new RadioButton("Java"); radioButton1.setToggleGroup(toggleGroup); radioButton1.setSelected(true); RadioButton radioButton2 = new RadioButton("Python"); radioButton2.setToggleGroup(toggleGroup); RadioButton radioButton3 = new RadioButton("Haskell"); radioButton3.setToggleGroup(toggleGroup); VBox vBox = new VBox(10, radioButton1, radioButton2, radioButton3); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 400, 400); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Radio Button Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(RadioButtonApp.class, args); } }
No comments:
Post a Comment