ListView(列表视图)

JavaFX ListView 控件使用户能够从预定义的选项列表中选择一个或多个选项。JavaFX ListView 控件由 javafx.scene.control.ListView 类表示。这个 JavaFX ListView 教程将解释如何使用 ListView 类。

The JavaFX ListView control enables users to choose one or more options from a predefined list of choices. The JavaFX ListView control is represented by the class javafx.scene.control.ListView . This JavaFX ListView tutorial will explain how to use the ListView class.

创建 ListView

您只需通过创建 ListView 类的新实例来创建 ListView。这是一个 JavaFX ListView 实例化的示例:

You create a ListView simply by creating a new instance of the ListView class. Here is a JavaFX ListView instantiation example:

  1. ListView listView = new ListView();

将项目添加到 ListView

您可以通过获取其项目集合并向其添加项目来将项目(选项)添加到 ListView。这是一个将项目添加到 JavaFX ListView 的示例:

You can add items (options) to a ListView by obtaining its item collection and add items to it. Here is an example that adds items to a JavaFX ListView:

  1. listView.getItems().add("Item 1");
  2. listView.getItems().add("Item 2");
  3. listView.getItems().add("Item 3");

ListView 添加到场景图中

要使 ListView 可见,您必须将其添加到场景图中。这意味着您必须将 ListView 添加到 Scene 对象或某些布局组件,然后将其附加到 Scene 对象。

To make a ListView visible you must add it to the scene graph. This means that you must add the ListView to a Scene object or to some layout component which is then attached to the Scene object.

下面是一个示例,展示了如何将 JavaFX ListView 添加到场景图中:

Here is an example showing how to add a JavaFX ListView to the scene graph:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.ListView;
  5. import javafx.scene.layout.HBox;
  6. import javafx.stage.Stage;
  7. public class ListViewExperiments extends Application {
  8. @Override
  9. public void start(Stage primaryStage) throws Exception {
  10. primaryStage.setTitle("ListView Experiment 1");
  11. ListView listView = new ListView();
  12. listView.getItems().add("Item 1");
  13. listView.getItems().add("Item 2");
  14. listView.getItems().add("Item 3");
  15. HBox hbox = new HBox(listView);
  16. Scene scene = new Scene(hbox, 300, 120);
  17. primaryStage.setScene(scene);
  18. primaryStage.show();
  19. }
  20. public static void main(String[] args) {
  21. Application.launch(args);
  22. }
  23. }

运行此示例生成的应用程序将类似于此屏幕截图:

The application resulting from running this example would look similar to this screenshot:

A JavaFX ListView added.

注意 ListView 默认是如何显示多个选项的。您可以为 ListView 设置高度和宽度,但不能明确设置应该可见的项目数。高度根据显示的每个项目的高度确定。

Notice how the ListView shows multiple options by default. You can set a height and width for a ListView, but you cannot set explicitly how many items should be visible. The height determines that based on the height of each item displayed.

如果 ListView 中的项目超出其可见区域,则 ListView 将添加滚动条,以便用户可以在项目上上下滚动。

If there are more items in the ListView than can fit into its visiible area, the ListView will add scroll bars so the user can scroll up and down over the items.

读取选定的值

您可以通过其 SelectionModel 读取 ListView 的选定索引。下面是一个示例,展示了如何读取 JavaFX ListView 的选定索引:

You can read the selected indexes of a ListView via its SelectionModel. Here is an example showing how to read the selected indexes of a JavaFX ListView:

  1. ObservableList selectedIndices =
  2. listView.getSelectionModel().getSelectedIndices();

OberservableList 将包含 Integer 对象,表示所有 ListView 中所选项目的索引。

The OberservableList will contain Integer objects representing the indexes of the selected items in the ListView.

这是一个完整的 JavaFX 示例。它添加了一个按钮,单击该按钮会读取 ListView 的选定项:

Here is a full JavaFX example with a button added which reads the selected items of the ListView when clicked:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.collections.ObservableList;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.ListView;
  7. import javafx.scene.control.SelectionMode;
  8. import javafx.scene.layout.VBox;
  9. import javafx.stage.Stage;
  10. public class ListViewExperiments extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. primaryStage.setTitle("ListView Experiment 1");
  14. ListView listView = new ListView();
  15. listView.getItems().add("Item 1");
  16. listView.getItems().add("Item 2");
  17. listView.getItems().add("Item 3");
  18. Button button = new Button("Read Selected Value");
  19. button.setOnAction(event -> {
  20. ObservableList selectedIndices = listView.getSelectionModel().getSelectedIndices();
  21. for(Object o : selectedIndices){
  22. System.out.println("o = " + o + " (" + o.getClass() + ")");
  23. }
  24. });
  25. VBox vBox = new VBox(listView, button);
  26. Scene scene = new Scene(vBox, 300, 120);
  27. primaryStage.setScene(scene);
  28. primaryStage.show();
  29. }
  30. public static void main(String[] args) {
  31. Application.launch(args);
  32. }
  33. }

允许选择多个项目

要允许选择 ListView 中的多个项目,您需要在 ListView 选择模型上设置相应的选择模式。下面是在 JavaFX ListView 上设置选择模式的示例:

To allow multiple items in the ListView to be selected you need to set the corresponding selection mode on the ListView selection model. Here is an example of setting the selection mode on the JavaFX ListView:

  1. listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

ListView 选择模型上设置 SelectionMode.MULTIPLE 后,用户可以在选择第一个选定项目之后通过按住 SHIFTCTRLListView 中选择多个其它项目。

Once you have set the SelectionMode.MULTIPLE on the ListView selection model, the user can select multiple items in the ListView by holding down SHIFT or CTRL when selecting additional items after the first selected item.

这是一个完整的 JavaFX 示例,展示了如何将 ListView 设置为多选模式。此外还添加了一个按钮,单击该按钮将向控制台输出 ListView 中所选项目的索引:

Here is a full JavaFX example that shows how to set a ListView into multiple selection mode, including a button which when clicked will write out the indices of the selected items in the ListView:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.collections.ObservableList;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.ListView;
  7. import javafx.scene.control.SelectionMode;
  8. import javafx.scene.layout.VBox;
  9. import javafx.stage.Stage;
  10. public class ListViewExperiments extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. primaryStage.setTitle("ListView Experiment 1");
  14. ListView listView = new ListView();
  15. listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  16. listView.getItems().add("Item 1");
  17. listView.getItems().add("Item 2");
  18. listView.getItems().add("Item 3");
  19. Button button = new Button("Read Selected Value");
  20. button.setOnAction(event -> {
  21. ObservableList selectedIndices = listView.getSelectionModel().getSelectedIndices();
  22. for(Object o : selectedIndices){
  23. System.out.println("o = " + o + " (" + o.getClass() + ")");
  24. }
  25. });
  26. VBox vBox = new VBox(listView, button);
  27. Scene scene = new Scene(vBox, 300, 120);
  28. primaryStage.setScene(scene);
  29. primaryStage.show();
  30. }
  31. public static void main(String[] args) {
  32. Application.launch(args);
  33. }
  34. }