Java Reference
In-Depth Information
One very useful feature of ListView is that scrollbars are automatically added when the
number of items in the list exceeds the number that can be displayed within the control's
dimensions. Because of its ability to make efficient use of limited screen space, ListView
is a popular alternative to other types of selection controls.
ListView is a generic class that is declared like this:
class ListView<T>
Here, T specifies the type of entries stored in the list view. Often, these are entries of type
String , but other types are also allowed.
Here is the ListView constructor that we will use:
ListView(ObservableList<T> list )
The list of items to be displayed is specified by list . It is an object of type ObservableList .
As explained earlier, ObservableList supports a list of objects. By default, a ListView al-
lows only one item in the list to be selected at any one time. You can allow multiple selec-
tions by changing the selection mode, but we will use the default, single-selection mode.
Probably the easiest way to create an ObservableList for use in a ListView is to use the
factory method observableArrayList( ) , which is a static method defined by the FXCol-
lections class (which is packaged in javafx.collections ). The version we will use is shown
here:
static <E> ObservableList<E> observableArrayList(E … elements )
In this case, E specifies the type of elements, which are passed via elements .
Although ListView provides a default size, sometimes you will want to set the preferred
height and/or width to best match your needs. One way to do this is to call the setPre-
fHeight( ) and setPrefWidth( ) methods, shown here:
final void setPrefHeight(double height )
final void setPrefWidth(double width )
Alternatively, you can use a single call to set both dimensions at the same time by use of
setPrefSize( ) , shown here:
void setPrefSize(double width , double height )
Search WWH ::




Custom Search