How to Retrieve Data from a Radio Button Group in JavaFX
In this article, we show how to retrieve data from a radio button group in JavaFX.
A radio button group is a group of radio buttons. It is very similar to check boxes, except that in a radio button group, only one button can be selected. With checkboxes, multiple boxes can be selected.
So in our code we create a radio button group with 4 radio buttons. And we tie an event handler to the button that accompanies the radio buttons, so that we can obtain the value from the radio button when the user clicks the submit button.
We obtain the value from the radio button group through the Java isSelected() function.
We can know which radio button is selected through the isSelected() function.
Thus, if one of the radio buttons is radio1, we use the statement, if (radio1.isSelected()) { //code to execute }
Since only one button can be selected with radio buttons, you can use the isSelected function to do whatever you want based on that.
The full Java code is shown below.
So just like any JavaFX application, you need to import several packages. You're more than likely familiar with this.
We then create a class that extends the Application. Therefore, we inherit all the functionality of the Application class, which has a lot of classes and methods that we need to use.
We then override the start() method, which is present in the Application class.
The start() method contains the main functionality of the program.
The stage is primaryStage.
All JavaFX applications must have at least one stage.
We set the title to "Quiz Question 1".
We then create a label, asking the question, "What is 10 + 20?"
We then create our radio button group.
To create a radio button group, we create instances of the RadioButton class. Since in this case, we are creating 4 radio buttons for the group, we create 4 instances of the RadioButton class, radio1, radio2, radio3, and radio4 and initialize the values to 10, 20, 30 and 40, as their values.
After creating instances of the radio buttons and initializing their values, radio buttons have to be put in a group so that our program knows that all these radio buttons belong to a single group.
We created our radio buttons but they are linked together. Therefore, we create an instance of the ToggleGroup class. Then we set all the radio buttons to this toggle group, so that they're all linked. Now only one radio button in this ToggleGroup can be truly selected.
So now we've created our full radio button group, we now must add event handling so that we can find out which radio button is selected.
We attach event handling not to the radio buttons but to the submit button. When the submit button is clicked, we then want to find out which radio button the user selected.
So we add event handling to the buton that is triggered when the user clicks on the submit button. If the radio button 3 is selected, which in this case is the correct answer of the quiz question, then we output that this answer is correct. Else, if any other options are selected, we output that the answer is wrong.
We check if the radio button 3, radio3, is selected through the isSelected() function.
We then create our layout for the window. We create a VBox layout, meaning the elements are vertically stacked, 5px apart.
We then add all of our components to the layout, create the scene, set the scene to the stage, and then show the stage.
When done, the window will look like the image at the following link: JavaFX Radio Button.
And this is all that is required to retrieve data from a radio button group in JavaFX.
Related Resources
How to Retrieve Data from a Text Field in JavaFX
How to Retrieve Data from a Group of Check Boxes in JavaFX
How to Retrieve Data from a ChoiceBox in JavaFX