1. Technology

ListView Overview


Woman working on laptop
Joshua Hodge Photography/E+/Getty Images

The ListView Class


The ListView class creates a control presents a list of items to the user. This items can be displayed as a horizontal or vertical list. The items in the list can be selected by the user. 

The items in a ListView object are contained within an ObservableList object. The ObservableList object allows the storage of a list of objects and can track the changes to the objects in the list. When working with the ListView it's recommended to make use of the generic type set to set the object type of the data in the list.

Import Statement

import javafx.scene.control.ListView


The ListView class has two constructors depending on whether you want to create an empty ListView object or one populated with items. Both constructors default to showing the items in the list vertically:

    • To create an empty ListView which will contain String items:
      ListView<String> list = new ListView<String>();
    • To create a ListView object and populate it with String items from an ObservableList:
ObservableList<String> countries = FXCollections.observableArrayList( 
"England", "Germany", "France", "Israel", "South Africa", "U.S.A.", "Australia");
ListView<String> list = new ListView<String>(names);

Useful Methods

If you create an empty ListView object you can use the setItems method. Passing an ObservableList of objects will set the items in the ListView:

ObservableList<String> countries = FXCollections.observableArrayList( 
"England", "Germany", "France", "Israel", "South Africa", "U.S.A.", "Australia");

The size of the ListView can be set using the setPrefHeight and setPrefWidth methods (the values passed represent pixels):


By default the orientation of the items displayed in a ListView is vertical. This orientation can be changed by using the setOrientation method and passing it the Orientation.HORIZONTAL (or Orientation.VERTICAL) constants contained in the Orientation enumeration:


Usage Tips

The selection and focus of items in a ListView object is handled by SelectionModel and FocusModel classes. The ListView contains an instance of each class which can be retrieved by using the getSelectionModel and getFocusModel methods. 

By default the SelectionModel for a ListView object uses the MultipleSelectionModel abstract class to set the selection behavior of the list. It is set to only allow the user to select one item in the list at any one time. This can be changed by changing the SelectionMode of the SelectionModel using the Selection.MULTIPLE (or Selection.SINGLE) constants contained in the Selection enumeration:


To retrieve the currently selected item use the getSelectedItem method of the SelectionModel (in this example the ListView contains String items):

String currentSelection = list.getSelectionModel().getSelectedItem();

To retrieve the index of the currently selected item use the getSelectedIndex method of the SelectionModel:

int currentSelectionIndex = list.getSelectionModel().getSelectedIndex();

To get the index of the item with the focus use the getFocusedIndex method of the FocusModel:

int focusIndex = list.getFocusModel().getFocusedIndex();

To get the item with the focus use the getFocusedItem method of the FocusModel:

String focusItem = list.getFocusModel().getFocusedItem();

Handling Events

To track the selection of items on a ListView object you can use the addListener method of the selectedItemProperty method of the SelectionModel to create a ChangeListener. It will pick up the change events for the list:

final Label selectionLabel = new Label();
            new ChangeListener() {
                public void changed(ObservableValue<? extends String> ov, 
                    String old_val, String new_val) {

To find out about other JavaFX controls have a look at JavaFX User Interface Controls.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. JavaFX
  6. JavaFX Controls
  7. JavaFX Controls: ListView Overview

©2014 About.com. All rights reserved.