Javafx 简明教程
JavaFX - TreeView
TreeView 是一个图形用户界面组件,用于显示一个分层的条目结构。它包含一个根节点和任意数量的子节点。树形视图主要用于组织层次结构中的数据。它提供了对数据与其关联的其他组件关系的更好理解。在下面的图中,我们可以看到一个组件树形视图。
The TreeView is a graphical user interface component that displays a hierarchical structure of items. It consists of a root node and any number of child nodes. Primarily, the tree view is used for organizing data with hierarchy. It provides a better understanding of data and its relation with other components. In the below figure, we can see a tree view of components.
TreeView in JavaFX
在 JavaFX 中,树形视图由一个名为 TreeView 的类表示。该类属于包 javafx.scene.control 。通过实例化这个类,我们可以在 JavaFX 中创建一个树形视图。TreeView 类的构造函数列在下面 −
In JavaFX, the treeview is represented by a class named TreeView. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a treeview in JavaFX. Constructors of the TreeView class are listed below −
-
TreeView() − It is the default constructor that constructs an empty treeview.
-
TreeView(TreeItem rootNode) − It creates a new treeview with the specified root node.
How to create a TreeView in JavaFX?
按照下面的步骤在 JavaFX 中创建一个树形视图。
Follow the steps given below to create a tree view in JavaFX.
Step 1: Create the root node of TreeView
首先,为所有条目的列表创建容器。在这种情况下,根节点是容器,因为它保存所有子节点。要创建根节点,我们使用 TreeItem 类,它是 javafx.scene.control 的一部分包。将根节点的名称作为参数值传递给该类的构造函数。 −
First, create containers for the lists of all items. In this case, root node is the container as it holds all the child nodes. To create the root node, we use the TreeItem class which is the part of javafx.scene.control package. Pass the name of root node to the constructor of this class as a parameter value. −
// Creating the root node
TreeItem<String> root = new TreeItem<>("Smartphones");
Step 2: Create the child nodes of TreeView
再次使用 TreeItem 类创建子节点。创建子节点后,使用 getChildren() 方法以及 addAll() 添加必需的条目。
Again, use the TreeItem class to create child nodes. Once the child nodes are created, use the getChildren() method along with addAll() to add the required items to it.
TreeItem<String> ios = new TreeItem<>("iOS");
ios.getChildren().addAll(
new TreeItem<>("iPhone 15 Plus"),
new TreeItem<>("iPhone 14")
);
Step 3: Add the child nodes to root nodes
要将子节点添加到根节点,请将子节点的名称作为参数值传递给 addAll() 方法,如下面的代码块所示 −
To add the child nodes to the root node, pass the name of child nodes as a parameter value to the addAll() method as shown in the following code blocks −
// Add the child nodes to the root node
root.getChildren().addAll(android, ios);
Step 4: Instantiate the TreeView class
要创建树形视图,请实例化 TreeView 类,并将根节点作为参数值传递给它的构造函数,如下面的代码所示 −
To create tree view, instantiate the TreeView class and pass the root node to its constructor as a parameter value as shown in the below code −
// Create the TreeView and add the root node
TreeView<String> treesV = new TreeView<>(root);
Step 5: Launching Application
创建树形视图后,请按照下面给出的步骤正确启动应用程序 −
Once the tree view is created, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the TreeView object as a parameter value to its constructor. We can also pass the dimensions of the application screen as optional parameters to this constructor.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
以下程序将使用 JavaFX 创建一个 TreeView。将此代码保存在名为 JavafxTreeview.java 的文件中。
Following is the program that will create a TreeView using JavaFX. Save this code in a file with the name JavafxTreeview.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
public class JavafxTreeview extends Application {
@Override
public void start(Stage stage) throws Exception {
// Creating the root node
TreeItem<String> root = new TreeItem<>("Smartphones");
root.setExpanded(true);
// Creating its child nodes
TreeItem<String> android = new TreeItem<>("Android");
android.getChildren().addAll(
new TreeItem<>("Samsung Galaxy S23 Ultra"),
new TreeItem<>("Xiaomi Redmi Note 13 Pro"),
new TreeItem<>("OnePlus 11R")
);
TreeItem<String> ios = new TreeItem<>("iOS");
ios.getChildren().addAll(
new TreeItem<>("iPhone 15 Plus"),
new TreeItem<>("iPhone 14")
);
// Add the child nodes to the root node
root.getChildren().addAll(android, ios);
// Create the TreeView and add the root node
TreeView<String> treesV = new TreeView<>(root);
// Create a scene and add the TreeView
Scene scene = new Scene(treesV, 400, 300);
stage.setTitle("TreeView in JavaFX");
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 JavafxTreeview.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTreeview
在执行之后,上述程序会生成一个 JavaFX 窗口,其中显示了一个 TreeView,其中包含一个智能手机列表(如下所示)。
On executing, the above program generates a JavaFX window displaying a TreeView with a list of smartphones as shown below.
Setting the mouse events for TreeView
如果我们想要展示哪些项目被用户点击,那么,我们可以通过传递一个 lambda 表达式来使用 setOnMouseClicked() 方法,如下所示的 JavaFX 代码中所示。将此代码保存在一个文件内,文件名是 JavafxTreeview.java 。
If we want to show which items are getting clicked by users, then, we can use setOnMouseClicked() method by passing a lambda expression as shown in the below JavaFX code. Save this code in a file with the name JavafxTreeview.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
public class JavafxTreeview extends Application {
@Override
public void start(Stage stage) throws Exception {
// Creating the root node
TreeItem<String> root = new TreeItem<>("Smartphones");
root.setExpanded(true);
// Creating its child nodes
TreeItem<String> android = new TreeItem<>("Android");
android.getChildren().addAll(
new TreeItem<>("Samsung Galaxy S23 Ultra"),
new TreeItem<>("Xiaomi Redmi Note 13 Pro"),
new TreeItem<>("OnePlus 11R")
);
TreeItem<String> ios = new TreeItem<>("iOS");
ios.getChildren().addAll(
new TreeItem<>("iPhone 15 Plus"),
new TreeItem<>("iPhone 14")
);
// Add the child nodes to the root node
root.getChildren().addAll(android, ios);
// Create the TreeView and add the root node
TreeView<String> treesV = new TreeView<>(root);
// Handle mouse clicks on the nodes
treesV.setOnMouseClicked(event -> {
// Get the selected item
TreeItem<String> item = treesV.getSelectionModel().getSelectedItem();
if (item != null) {
// Display the item text
System.out.println(item.getValue());
}
});
// Create a scene and add the TreeView
Scene scene = new Scene(treesV, 400, 300);
stage.setTitle("TreeView in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTreeview.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTreeview