How to Retrieve Data from a ChoiceBox in JavaFX

Java


In this article, we show how to retrieve data from a ChoiceBox in JavaFX.

A ChoiceBox in JavaFX is really a dropdown menu.

More than likely you are very familiar with dropdown menus. You click on an option and a dropdown menu presents itself, where you can select an option.

It's very popular and is widely in many graphical user interfaces (GUIs), such as in menus.

It is actually very to easy to get the value from a choicebox.

All we have to do is use the getValue() function.

So if we have a ChoiceBox named choicebox, to get the value that the user selects from the ChoiceBox named choicebox, we have the following code.



The code above gets the value that the user has selected from the ChoiceBox.

We can then do whatever we want with this selection such as display it on the menu or perform some type of processing with it.

So to show the full Java code of how we can retrieve data from a ChoiceBox, we have the following code shown below.



So just like any JavaFX program, we import all the packages we need to run our code.

We create our public class that extends the Application class, so that we inherit all of the code (classes, methods, etc.) in the Application class.

We override the start() method, which is found in the Application class.

We set the title to "Semester Class Selection", create a label asking a user what science class s/he would like to take, and create another label that will be used to give the output of the science class that the user has selected.

We then create the button and name it button. We put the text "Submit" on the button.

We then create our ChoiceBox and name it dropdownmenu.

This is a ChoiceBox that holds String values.

Next, we add all the options we want in our ChoiceBox. To do this, we use the statement, dropdownmenu.getItems.().addAll(). Instead of the addAll() function, we all the options for the ChoiceBox.

The line, dropdownmenu.getSelectionModel().select(0);, chooses the first item in the ChoiceBox. If you don't add this line, when you create the drop down menu, it will show nothing unless you specify a default value. In order to show a value (normally you want to show the first item on the list), you can put this line in. This line will always select the first value from a list of items.

Next, we add event handling to the button. This is when a user selects a value and then clicks the button, to submit the data. When this happens, we want to output what value the user has chosen.

So we take our label named labelresponse that we've created and set its value equal to dropdownmenu.getValue(). This sets the label equal to the value of the selected value in the ChoiceBox. The statement, dropdownmenu.getValue(), gets the value that of the selected item from the ChoiceBox. Using the setText() function, we set the label equal to the value that the user has chosen.

And this concludes retrieving data from the ChoiceBox.

The rest of the code creates the alyout, adds the elements to layout, creates the scene, sets the scene on the stage, and shows the stage.

Running the code above gives us the image shown at the following link: JavaFX ChoiceBox

And this is how we can retrieve data from a ChoiceBox 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 Radio Button Group in JavaFX




HTML Comment Box is loading comments...