Java Reference
In-Depth Information
The getter and setter methods for property
values and a getter for property itself are provided
in the class, but omitted in the UML diagram for brevity.
javafx.scene.control.ListView<T>
-items: ObjectProperty<ObservableList<T>>
-orientation: BooleanProperty
The items in the list view.
Indicates whether the items are displayed horizontally or vertically
in the list view.
Specifies how items are selected. The
SelectionModel
is also used
to obtain the selected items.
-selectionModel:
ObjectProperty<MultipleSelectionModel<T>>
+ListView()
+ListView(items: ObservableList<T>)
Creates an empty list view.
Creates a list view with the specified items.
F
IGURE
16.18
ListView
enables the user to select one or multiple items from a list of items.
The
getSelectionModel()
method returns an instance of
SelectionModel
, which
contains the methods for setting a selection mode and obtaining selected indices and items.
The selection mode is defined in one of the two constants
SelectionMode.MULTIPLE
and
SelectionMode.SINGLE
, which indicates whether a single item or multiple items can be
selected. The default value is
SelectionMode.SINGLE
. Figure 16.19a shows a single selec-
tion and Figure 16.19b-c show multiple selections.
(a) Single selection
(b) Multiple selection
(c) Multiple selection
F
IGURE
16.19
SelecitonMode
has two selection modes: single selection and multiple-
interval selection.
The following statements create a list view of six items with multiple selections allowed.
ObservableList<String> items =
FXCollections.observableArrayList(
"Item 1"
,
"Item 2"
,
"Item 3"
,
"Item 4"
,
"Item 5"
,
"Item 6"
);
ListView<String> lv =
new
ListView<>(items);
lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
The selection model in a list view has the
selectedItemProperty
property, which is an
instance of
Observable
. As discussed in Section 15.10, you can add a listener to this prop-
erty for handling the property change as follows:
lv.getSelectionModel().selectedItemProperty().addListener(
new
InvalidationListener() {
public void invalidated(Observable ov) {
System.out.println(
"Selected indices: "
+ lv.getSelectionModel().getSelectedIndices());
Search WWH ::
Custom Search