×

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 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 Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

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 ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Light Distant

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

4 minutes read.

JavaFX Drop Shadow

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

4 minutes read.

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 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 Inner Shadow

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

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 Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 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 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.