Javafx 简明教程

JavaFX - ListView

ListView 是一个图形用户界面组件,用于显示用户可以从中选择所需项目的一个项目列表。通常, list 指代项目的一个组或集合。它有助于以更友好且可读的方式组织、构建和提供信息。下图展示了一个城市列表视图,用户可以从中选择一个选项 −

The ListView is a graphical user interface component used for displaying a list of items from which a user can select desired items. Generally, a list refers to a group or collection of items. It helps in organizing, structuring and presenting information in more user-friendly and readable way. The figure below illustrates a list view of cities from which a user can choose one option −

sample listview

ListView in JavaFX

在 JavaFX 中,列表视图由名为 ListView 的一个类表示,它属于 javafx.scene.control 包。我们可以通过实例化该类来创建一个列表视图组件。此外,我们可以选择其方向,使其既能垂直显示又能水平显示。ListView 类的构造函数列表如下 −

In JavaFX, the list view is represented by a class named ListView which is a part of javafx.scene.control package. We can create a list view component by instantiating this class. Additionally, we have the option to select its orientation, allowing it to be displayed either vertically or horizontally. List of constructors of the ListView class is as follows −

  1. ListView() − It is the default constructor that constructs a vertical list view.

  2. ListView(ObservableList<type> listItems) − It constructs a new vertical list view with the specified list items.

Creating ListView in JavaFX

要创建任何 JavaFX 应用程序中的 ListView,我们可以使用默认构造函数或 ListView 类的参数化构造函数。如果使用默认构造函数,则应明确传递列表项。参数化构造函数接受 ArrayList 对象作为参数值,如下面的代码块所示 −

To create a ListView in any JavaFX application, we can use either the default contructor or the parameterized constructor of the ListView class. If the default constructor is used, we should pass the list items explicitly. The parameterized constructor accepts an ArrayList object as a parameter value as shown in the below code block −

//list View for educational qualification
ObservableList<String> names = FXCollections.observableArrayList("Engineering", "MCA", "MBA", "Graduation", "MTECH", "Mphil", "Phd");
ListView<String> listView = new ListView<String>(names);

一旦实例化 ListView 类并添加其项目,即定义 VBox 或 HBox 之类的任何布局窗格来容纳列表视图。接下来,创建一个 Scene 对象,通过将布局窗格对象和 Scene 的维度传递到其构造函数。然后,设置舞台并启动应用程序来显示结果。

Once the ListView class is instantiated and its items are added, define any layout pane such VBox or HBox to hold the list view. Next, create a Scene object by passing the layout pane obejct and the dimensions of the Scene to its constructor. Then, set the stage and launch the application to display the result.

Example

在以下示例中,我们将展示如何在 JavaFX 中创建 ListView。将以下代码保存在名为 JavafxListview.java 的文件中。

In the following example, we are going to demonstrates how to create ListView in JavaFX. Save this code in a file with the name JavafxListview.java.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class JavafxListview extends Application {
   public void start(Stage stage) {
      //Label for education
      Label label = new Label("Educational qualification:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //list View for educational qualification
      ObservableList<String> names = FXCollections.observableArrayList("Engineering", "MCA", "MBA", "Graduation", "MTECH", "Mphil", "Phd");
      ListView<String> listView = new ListView<String>(names);
      listView.setMaxSize(200, 160);
      //Creating the layout
      VBox layout = new VBox(10);
      layout.setPadding(new Insets(5, 5, 5, 50));
      layout.getChildren().addAll(label, listView);
      layout.setStyle("-fx-background-color: BEIGE");
      //Setting the stage
      Scene scene = new Scene(layout, 400, 300);
      stage.setTitle("List View Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令从命令提示符编译并执行已保存的 Java 文件 −

Compile and execute the saved Java file from the command prompt using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxListview.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxListview

在执行上述代码时,它将生成一个显示 ListView 的窗口,如下面的输出中所示 −

On executing the above code, it will generate a window displaying a ListView as shown in the below output −

listview output