Java Reference
In-Depth Information
Notice that a vertical scroll bar has been included so that the list can be scrolled to see
all of its entries. As mentioned, when the contents of a
ListView
exceed its size, a scroll
bar is automatically added. This makes
ListView
a very convenient control.
In the program, pay special attention to how the
ListView
is constructed. First, an
Ob-
servableList
is created by this line:
It uses the
observableArrayList( )
method to create a list of strings. Then, the
Observ-
ableList
is used to initialize a
ListView
, as shown here:
The program then sets the preferred width and height of the control.
Now, notice how the selection model is obtained for
lvComputers
:
As explained,
ListView
uses
MultipleSelectionModel
, even when only a single selection
is allowed. The
selectedItemProperty( )
method is then called on the model and a change
listener is registered, as shown here:
As a point of interest, the same basic mechanism used to listen for and handle change
events can be applied to any control that generates change events.
Ask the Expert