×

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select one choice for specific fields. The user has a choice to either select or deselects the Radio Button. The Radio Buttons are often created in one group where users can select only one option among them. In JavaFX application, javafx.scene.control.RadioButton class is used to include a radio button in an application. Whenever a user checks or unchecks a Radio Button event is generate on it and is further handled by the Event Handler. Using the isSelected() method, we can check whether the radio button is clicked or not. This method is used to retrieve the choice entered by the user. We can provide the user choice to either select or deselect the option using Radio Button.

JavaFX Radio Button Constructors:

  1. RadioButton(): This constructor creates JavaFX Radio Button without any label.
  2. RadioButton(String text): This constructor creates Radio Button with a label passed as an argument to it.

JavaFX Radio Button Methods:

  1. isSelected(): This method is used to check whether Radio Button is selected or not.
  2. getText(): This method is used to get label associated with Radio Button.
  3. setSelected(Boolean value): This method is used to set the status of the Radio Button either selected or not selected based on the Boolean value passed to it.
  4. fire(): This method is used to change the state of the Radio Button. This method will only work if Radio Button is not previously selected.

JavaFX Radio Button:

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;  


  
public class RadioButtonUI extends Application 
 {  
static  String items = " ";


    @Override  
    public void start( Stage primaryStage ) throws Exception 
   {  
    Label l = new Label ( " Choose your country : ");  
    RadioButton  newc1 = new RadioButton (" India ");  
    RadioButton  newc2 = new RadioButton (" UK ");  
    RadioButton  newc3 = new RadioButton (" USA ");  
    RadioButton  newc4 = new RadioButton (" Canada ");  
        Button button= new Button( "Submit");
        button.setOnAction(e -> 
        {
          
        if ( newc1.isSelected() )
        {
        items += " India \n ";
        }
           
        if ( newc2.isSelected() )
        {
        items += " UK \n ";
        }
            
        if ( newc3.isSelected() )
        {
        items += " USA \n ";
        }
             
        if ( newc4.isSelected() )
        {
        items += " Canada \n ";
        }  
        System.out.println(" Country of Residence : "+items);


        }
        );
        
        HBox root = new HBox();  
        root.getChildren().addAll( l, newc1, newc2, newc3, newc4, button );  
       
        root.setSpacing(5);  
        Scene scene = new Scene( root, 600, 400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX RadioButton Example ");  
        primaryStage.show();     
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the radio button and action on it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.RadioButton, javafx.scene.control.Label, javafx.scene.layout.HBox.

Then we have created one class named RadioButtonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button with Checkbox and Labels, an HBox object is created which is then passed to the Scene class object.

Label for entering the country of residence is created using the constructor with label text. Also, radio buttons are created for various countries which are then added to HBox. Radio buttons are created for each country like India, UK, USA, and Canada. User have to choose only one country out of choices available, so radio button are used. User can select the country of residence say India and then after clicking on submit button, it will display output as Country of Residence: India. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX RadioButton Example ”. Also, it displays radio buttons for various countries like India, UK, USA, and Canada. We can select any one of the country and after clicking on the submit button it will display the name of the country selected as the country of residence.

JavaFX RadioButton

Related Topics

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.