TreeView UI控件为我们提供了一种渲染树结构的方法。 树具有包含所有分层值的根节点。
import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.TreeItem;import javafx.scene.control.TreeView;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { TreeItem rootItem = new TreeItem<>("Root"); rootItem.setExpanded(true); TreeItem item = new TreeItem<>("A"); rootItem.getChildren().add(item); item = new TreeItem<>("B"); rootItem.getChildren().add(item); TreeView tree = new TreeView<>(rootItem); StackPane root = new StackPane(); root.getChildren().add(tree); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); }}
上面的代码生成以下结果。
创建树视图
当创建树视图时,我们通常从构建根的TreeItem对象开始。 然后我们将根添加到树视图,并创建其他树项目对象并将它们添加到根。
我们可以通过setGraphic方法或使用它的构造函数来创建一个图形图标到树项目。 图标的建议大小为16x16。
对根项目调用的setExpanded方法定义树视图项目的初始外观。
以下代码从本地文件系统创建动态树。
import java.io.File;import javafx.application.Application;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.TreeItem;import javafx.scene.control.TreeView;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { launch(args); } private TreeItem createNode(final File f) { return new TreeItem(f) { private boolean isLeaf; private boolean isFirstTimeChildren = true; private boolean isFirstTimeLeaf = true; @Override public ObservableList> getChildren() { if (isFirstTimeChildren) { isFirstTimeChildren = false; super.getChildren().setAll(buildChildren(this)); } return super.getChildren(); } @Override public boolean isLeaf() { if (isFirstTimeLeaf) { isFirstTimeLeaf = false; File f = (File) getValue(); isLeaf = f.isFile(); } return isLeaf; } private ObservableList> buildChildren( TreeItem TreeItem) { File f = TreeItem.getValue(); if (f == null) { return FXCollections.emptyObservableList(); } if (f.isFile()) { return FXCollections.emptyObservableList(); } File[] files = f.listFiles(); if (files != null) { ObservableList> children = FXCollections .observableArrayList(); for (File childFile : files) { children.add(createNode(childFile)); } return children; } return FXCollections.emptyObservableList(); } }; } @Override public void start(Stage stage) { Scene scene = new Scene(new Group(), 300, 300); VBox vbox = new VBox(); TreeItem root = createNode(new File("c:/")); TreeView treeView = new TreeView(root); vbox.getChildren().add(treeView); ((Group) scene.getRoot()).getChildren().add(vbox); stage.setScene(scene); stage.show(); }}
上面的代码生成以下结果。
用户域模型
以下代码显示了如何使用用户域模型数据与TreeView。
创建Employee类以捕获用户域模型,并且代码使用循环将所有员工添加到树视图。
/* * Copyright (c) 2008, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */import java.util.Arrays;import java.util.List;import javafx.application.Application;import javafx.beans.property.SimpleStringProperty;import javafx.scene.Node;import javafx.scene.Scene;import javafx.scene.control.TreeItem;import javafx.scene.control.TreeView;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.stage.Stage;public class Main extends Application { List employees = Arrays.asList( new Employee("a1