Java Reference
In-Depth Information
Creating a
ListView
and Assigning Items to a
ListView
The following code snippet, from Listing 6-9, defines and populates the
ListView
in the StarterApp program:
ListView listView = new ListView(model.listViewItems);
In addition to the code in the preceding snippet, the following line from Listing 6-1 contains an instance variable
from our
StarterAppModel
class that contains the objects that will be displayed in the
ListView
:
public ObservableList listViewItems = FXCollections.observableArrayList();
Recall that this is the same instance variable that is populated when clicking a leaf
TreeItem
in the previous section.
The parameter we provide to the
ListView
constructor causes the
ObservableList
named
listViewItems
in
the model to be associated with the
ListView
. As demonstrated in Step 11 of the previous exercise, if contents of the
underlying
ObservableList
change the
ListView
is automatically updated to reflect the changes.
Creating a
SplitPane
As you experienced in Step 12 of the previous exercise, the
SplitPane
shown in Figure
6-6
contains a
TreeView
and
a
ListView
, and its divider can be dragged by the user. The following code snippet, from Listing 6-9, defines and
populates the
SplitPane
in the StarterApp program:
SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(treeView, listView);
In this case there are two nodes in the
SplitPane
, which means that there will be just one divider. Note that a
SplitPane
may have more than two nodes and therefore more than one divider.
In addition to the functionality shown in the previous snippet, a
SplitPane
may also have its
orientation
set to
VERTICAL
, and its
dividerPositions
set to given percentages of the pane.
Now that you know how to create
TreeView
,
ListView,
and
SplitPane
controls, we move on to the next tab,
ScrollPane/Miscellaneous.
Creating a
TreeTableView
Sometimes, it is useful to combine a tree and a table. This is especially important when the data that has to be
visualized is hierarchical and each entry contains the same type of properties. In this case, the
TreeTableView
class
provides a great solution. As you can see in Figures
6-7
and
6-8
, a
TreeTableView
provides the functionality offered by
a
TreeView
, but for every entry that is shown, we can also show additional columns in a
TreeTableView
.
The data in a
TreeTableView
is created using
TreeItem
instances. In our StarterApp program, we created data in
the
getFamily()
method of the model, as shown in Listing 6-10.
Listing 6-10.
Create Hierarchical Data in
StarterAppModel.java
public TreeItem<Person> getFamilyTree() {
Random random = new Random();
TreeItem<Person> root = new TreeItem();
for (int i = 0; i < 5; i++) {
Person parent = new Person("Parent " + i, "LastName" + i, "Phone" + i);
TreeItem<Person> parentItem = new TreeItem(parent);
for (int j = 0; j < random.nextInt(4); j++) {
Person child = new Person("Child " + i + "-" + j, "LastName" + i, "Phone" + j);
Search WWH ::
Custom Search