How to Retrieve Data from a Group of Check Boxes in JavaFX

Java


In this article, we show how to retrieve data from a group of check boxes in JavaFX.

Check boxes are items where a user can click from various selections. The user can check 1 or multiple check boxes. Unlike a radio button, where only 1 choice can be selected, with check boxes, there is no limit, meaning the user can select multiple options.

So in our code we create a group of 4 check boxes. And we tie an event handler to the button that accompanies the group of check boxes, so that we can obtain the value from the check boxes when the user clicks the submit button.

We obtain the value from the check boxes group through the Java isSelected() function.

We can know which check box(es) are selected through the isSelected() function.

Thus, if one of the check boxes is one, we use the statement, if (one.isSelected()) { //code to execute }

And we can do this for each check box to check whether it is selected or not.

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 create a static String variable named selecteditems and set it equal to "" (blank). We use this variable later in our code with the check boxes. If a check box is selected, its value is added to this selecteditems variable. This way, once we have gone through each check box to see whether or not it is selected, this variable will hold all the values of all the check boxes that are selected.

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 "Dinner Menu Fish Selection".

We then create a label, asking the question, "Which items would you like?"

We then create our check boxes group.

To create a check boxes group, we create instances of the CheckBox class. Since in this case, we are creating 4 check boxes for the group, we create 4 instances of the CheckBox class, one, two, three, and four and initialize the values to Salmon, Lobster, Shrimp, Tuna, as their values.

So now we've created the check boxes group, we now must add event handling so that we can find out which check box(es) are selected.

We attach event handling not to the check boxes but to the submit button. When the submit button is clicked, we then want to find out which check boxes the user selected.

So we add event handling to the button that is triggered when the user clicks on the submit button.

We go through each check box and check whether each is selected through the isSelected() function.

So for check box 1, which is named one, we use the statement, if(one.isSelected()) { selecteditems += "Salmon\n"; } If the check box is selected, we add the value, "Salmon" to the selecteditems variable. And we do the same for the rest of the check boxes.

Once all the for loops are done, we create a label that states the items that the user has selected.

And this concludes the code for the check boxes.

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 Check Box.

And this is all that is required to retrieve data from a group of check boxes in JavaFX.


Related Resources

How to Retrieve Data from a Text Field in JavaFX

How to Retrieve Data from a Radio Button Group in JavaFX

How to Retrieve Data from a ChoiceBox in JavaFX




HTML Comment Box is loading comments...