How to Retrieve Data from a ListView in JavaFX


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

A ListView is a list of items that creates a vertial scrollable list.

A ListView can list as many items as is need for a list.

In this circuit, we create a ListView that has various items. We then retrieve the values that are selected in the ListView by creating an ObservableList and then reading the data by the following code.

Let's say we created a ListView that holds String data.

An ObservableList can be seen as a naturatrl list variable that can keep track of lists of data. Since we specify it to be of type String, it can read the list of String data from a ListView.

We then take this ObservableList and set it equal to the name of the ListView in this case listview and follow it with, .getSelectionModel().getSelectedItems();

This lets us read all of the selected items that the user has selected in the ListView.

If we want to output the data, then all we have to do is use an enhanced for loop to go through each of the items (which are those that are selected).

In the code above, just for demonstration purposes, we simply print out the selected items to the console.

In our code, however, we will do something more advanced. We'll show how to print out the selected items to the window using a label.

The full Java code to retrieve data from a ListView is 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, create a label asking a user to please choose a topic of desire, and create another label that will be used to output which items 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 ListView named listview. We create it to be of String type, since it will hold Strings.

We add items to the ListView using the getItems().addAll() function. We then pass in the items (Strings) we want to display on the ListView.

To enable the user to select multiple items on the ListView, we add the line, listview.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

This allows for multiple item selection.

We've now finished create our ListView.

Now we add event handling to the button, so that when the user clicks on it, we can see which items are selected.

So when then user clicks on the button the setOnAction method contains all of our code to handle what happens when the button is clicked.

Inside of this setOnAction() method, we create an ObservableList of type String named topics.

An ObservableList can be seen as a type of list that is totally compatible with a ListView. A ListView is actually a type of ObservableList. So they work seamlessly together.

The reason we create an ObservableList is because want to read which items are selected from the ListView. To read a list, you need a list such as an ObservableList. This is why we do it.

We set this ObservableList, topics, equal to listview.getSelectionModel().getSelectedItems().

This allows us to read all of the items that are selected on the ListView.

Because we want all the selected items in a single string that we can later output to the user, we create a string named list and initialize to an empty string initially.

We then use an enhanced for loop to loop through all of the selected items that are in list format in the ObservableList topics and put them in a string.

The string separates the items by a comma in between each item.

This is fine for all of the items except the last item.

We don't want the last item to have a comma after it, since it is the last item on the list and no other item follows after it.

We therefore use the Java lastIndexOf() function to find the last index of a comma. We then store this value in an integer variable named pos.

We then create a variable named selection. We create a substring of the String list that starts at the beginning and ends just before the last comma. This way, we have the same string as list, just without the last comma.

We then create our label that we made named labelresponse and set it equal to the string selection.

And this concludes our code for retrieving data from a ListView.

To see how this window will look, see the following link: JavaFX ListView.

The rest of the code finishes building the window. We create the layout, add all elements to the layout, create the scene, add the scene to the stage, and show the stage.

We then launch the program through the main method.

And this is all that is required to retrieve data from a ListView 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 Group of Check Boxes in JavaFX

How to Retrieve Data from a ChoiceBox in JavaFX

HTML Comment Box is loading comments...