treeview控件怎么使用修改发育树_JavaFX 树视图

20f305ca71464c13bd6362aad2531eab.png

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(); }}

上面的代码生成以下结果。

8237fdb731220638ae3e588f597dd899.png

创建树视图

当创建树视图时,我们通常从构建根的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(); }}

上面的代码生成以下结果。

96418605bb53fa3a4bff1a850407200d.png

用户域模型

以下代码显示了如何使用用户域模型数据与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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值