RadioButtons are a part of JavaFx package. RadioButtons are mainly used to create a series of items where only one can be selected. When a Radio button is pressed and released an Action event is sent, this Action Event can be handled using an Event Handler.
RadioButton can be added to Toggle Group so that the user cannot select more than one item . By default a radio button is not a part of any toggle group. The selected item of a toggle group can be found using getSelectedToggle() function.
Constructors of the RadioButton class:
- RadioButton():Creates a radio button with an empty string for its label.
- RadioButton(String t):Creates a radio button with the specified text as its label
Commonly used methods:
| method | explanation |
|---|---|
| getText() | returns the textLabel for radio button |
| isSelected() | returns whether the radiobutton is selected or not |
| setSelected(boolean b) | sets whether the radiobutton is selected or not |
| setToggleGroup(ToggleGroup tg) | sets the toggle group for the radio button |
| fire() | Toggles the state of the radio button if and only if the RadioButton has not already selected or is not part of a ToggleGroup. |
Below programs illustrate the RadioButton class:
- Program to create RadioButton and add it to the stage: This program creates a RadioButton indicated by the name r1, r2, r3. The radio button will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a tile-pane is created, on which addChildren() method is called to attach the radio button inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
// Java program to create RadioButton and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.text.*;publicclassradiobuttonextendsApplication {   Â// launch the application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating RadioButton");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a label       ÂLabel l =newLabel("This is a Radiobutton example ");       Â// create radiobuttons       ÂRadioButton r1 =newRadioButton("male");       ÂRadioButton r2 =newRadioButton("female");       ÂRadioButton r3 =newRadioButton("others");       Â// add label       Âr.getChildren().add(l);       Âr.getChildren().add(r1);       Âr.getChildren().add(r2);       Âr.getChildren().add(r3);       Â// create a scene       ÂScene sc =newScene(r,200,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Program to create RadioButton and add it to a ToggleGroup: This program creates a RadioButton indicated by the name r1, r2, r3. The radio button will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. A toggle group is created and the radio buttons are added to the toggle group using setToggleGroup() function. Then a tile-pane is created, on which addChildren() method is called to attach the radio button inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
// Java Program to create RadioButton and add it to a ToggleGroupimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.text.*;publicclassradiobutton_1extendsApplication {   Â// labels   ÂLabel l;   Â// launch the application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating RadioButton");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a label       Âl =newLabel("This is a Radiobutton example ");       Â// create a toggle group       ÂToggleGroup tg =newToggleGroup();       Â// create radiobuttons       ÂRadioButton r1 =newRadioButton("male");       ÂRadioButton r2 =newRadioButton("female");       ÂRadioButton r3 =newRadioButton("others");       Â// add radiobuttons to toggle group       Âr1.setToggleGroup(tg);       Âr2.setToggleGroup(tg);       Âr3.setToggleGroup(tg);       Â// add label       Âr.getChildren().add(l);       Âr.getChildren().add(r1);       Âr.getChildren().add(r2);       Âr.getChildren().add(r3);       Â// create a scene       ÂScene sc =newScene(r,200,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Program to create RadioButton, add it to a ToggleGroup and add a listener to it: This program creates a RadioButton indicated by the name r1, r2, r3. The radio button will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. A toggle group is created and the radio buttons are added to the toggle group using setToggleGroup() function. A label l2 is created to show which radio button is selected. A change listener is added to handle any change in the selection of the radio buttons (using the addListener() function). The change in selection is depicted by changing the text of label l2. Then a tile-pane is created, on which addChildren() method is called to attach the radio button inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
// Java Program to create RadioButton, add it to a ToggleGroup and add a listener to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.*;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.text.*;importjavafx.beans.value.*;publicclassradiobutton_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating RadioButton");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a label       ÂLabel l =newLabel("This is a Radiobutton example ");       ÂLabel l2 =newLabel("nothing selected");       Â// create a toggle group       ÂToggleGroup tg =newToggleGroup();       Â// create radiobuttons       ÂRadioButton r1 =newRadioButton("male");       ÂRadioButton r2 =newRadioButton("female");       ÂRadioButton r3 =newRadioButton("others");       Â// add radiobuttons to toggle group       Âr1.setToggleGroup(tg);       Âr2.setToggleGroup(tg);       Âr3.setToggleGroup(tg);       Â// add label       Âr.getChildren().add(l);       Âr.getChildren().add(r1);       Âr.getChildren().add(r2);       Âr.getChildren().add(r3);       Âr.getChildren().add(l2);       Â// create a scene       ÂScene sc =newScene(r,200,200);       Â// add a change listener       Âtg.selectedToggleProperty().addListener(newChangeListener<Toggle>()       Â{           Âpublicvoidchanged(ObservableValue<?extendsToggle> ob,                                                   ÂToggle o, Toggle n)           Â{               ÂRadioButton rb = (RadioButton)tg.getSelectedToggle();               Âif(rb !=null) {                   ÂString s = rb.getText();                   Â// change the label                   Âl2.setText(s +" selected");               Â}           Â}       Â});       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
Note: The above programs will not run in an online IDE please use an offline compiler
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/RadioButton.html

