Java Reference
In-Depth Information
System.out.println( "Selected items: "
+ lv.getSelectionModel().getSelectedItems());
}
});
This anonymous inner class can be simplified using a lambda expression as follows:
lv.getSelectionModel().selectedItemProperty().addListener(ov -> {
System.out.println( "Selected indices: "
+ lv.getSelectionModel().getSelectedIndices());
System.out.println( "Selected items: "
+ lv.getSelectionModel().getSelectedItems());
});
Listing 16.9 gives a program that lets users select the countries in a list view and displays
the flags of the selected countries in the image views. Figure 16.20 shows a sample run of the
program.
FlowPane
ListView
inside a
scroll pane
An ImageView
is displayed
F IGURE 16.20
When the countries in the list view are selected, corresponding images
of their flags are displayed in the image views.
Here are the major steps in the program:
1. Create the user interface.
Create a list view with nine country names as selection values, and place the list view
inside a scroll pane. Place the scroll pane on the left of a border pane. Create nine image
views to be used to display the countries' flag images. Create a flow pane to hold the
image views and place the pane in the center of the border pane.
2. Process the event.
Create a listener to implement the invalidated method in the
InvalidationListener interface to place the selected countries' flag image views
in the pane.
L ISTING 16.9
ListViewDemo.java
1 import javafx.application.Application;
2 import javafx.stage.Stage;
3 import javafx.collections.FXCollections;
4 import javafx.scene.Scene;
5 import javafx.scene.control.ListView;
6 import javafx.scene.control.ScrollPane;
7 import javafx.scene.control.SelectionMode;
8 import javafx.scene.image.ImageView;
9 import javafx.scene.layout.BorderPane;
10 import javafx.scene.layout.FlowPane;
11
12 public class ListViewDemo extends Application {
 
 
Search WWH ::




Custom Search