【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)

在使用Javafx插件开发作业项目时,我需要将房屋以树状表格的形式显示出来。
实现的效果:

【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)

1、简单介绍

在这里简单介绍一下我的程序中涉及到的类的属性。

在我的程序中,需要显示的类为House类。

House类的属性如下所示。我需要将楼盘、房屋编号、购房合同编号、移交日期、所属会员ID与备注显示在表格中。

public class House implements Serializable
{
    String code = "";//房屋编号
    String houseName = "";//楼盘
    String houseNumber = "";//楼号
    String houseFloor = "";//楼层
    String roomNumber = "";//房间号
    String purchaseNumber = "";//购房合同编号
    String purchaseDate = "";//移交日期
    String add = "";//备注
    String houseOwnerID = "";//所属会员ID
...下面的代码略

使用HouseList作为House的集合类。

public class HouseList implements Iterable<house>, Serializable
{
    public ArrayList<house> houselist = new ArrayList<>();

    /**
     * &#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x623F;&#x5C4B;&#x5230;&#x5217;&#x8868;
     * @param house &#x6DFB;&#x52A0;&#x7684;&#x623F;&#x5C4B;
     */
    public void add(House house)
    {
        this.houselist.add(house);
    }

    @Override
    public Iterator iterator()
    {
        return this.houselist.iterator();
    }
}
</house></house>

在Structure类中实例化一个HouseList对象。这个HouseList对象存储有要显示的房屋。

2、整体思路

我的整体的思路是:

在创建页面时,Javafx会调用页面的控制器(Controller)中的initialize()函数进行页面的初始化。

因此,我在该函数中遍历房屋列并完成树表的创建。

3、具体实现

首先使用Scene Builder创建一个页面,添加TreeTableView组件,并添加多个TreeTableColumn。

【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)
在右侧窗口设置TreeTableView与每个TreeTableColumn的fx:id。
【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)
在项目中创建一个控制类(我将其命名为AdminInterface_House_Controller),并将刚才创建的页面的fxml文件中的Controller设置为这个类。

在创建的控制类中添加属性。

注意:属性的命名与刚才设置的fx:id一致。

public class AdminInterface_House_Controller {
    @FXML
    private TreeTableView table;
    @FXML
    private TreeTableColumn houseTree_List;
    @FXML
    private TreeTableColumn purchaseNumber_List;
    @FXML
    private TreeTableColumn purchaseDate_List;
    @FXML
    private TreeTableColumn add_List;
    @FXML
    private TreeTableColumn houseOwnerID_List;
    @FXML
    private TreeTableColumn code_List;
...下面的代码省略

在这个类中创建初始化方法。我们将在这个函数中完成完成树表的创建。

@FXML
private void initialize() {

}

initialize()函数中,进行房屋遍历,每次循环取出一间房屋。每次循环的流程图如下所示:

【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)
为了减少initialize()中的代码,我先定义了一个repeatText()方法。这个方法输入一个ObservableList

这个函数判断是否存在一个占位房屋的roomNumber变量与输入的字符串相同。
如果存在,则说明输入的字符串重复,该函数输出重复位置。如果不存在,函数输出-1。

private int repeatText(ObservableList> list, String text){
    //输出-1代表不重复,输出其他自然数代表重复位置,如0代表与第一个节点重复
    int i = -1;
    House temp;
    for (TreeItem treeItem : list){
        i++;
        temp = treeItem.getValue();
        if (text.equals(temp.getRoomNumber())){
            //由于输入的是占位房屋列表,只有roomNumber变量有值。所以只需要判断text是否与roomNumber相同即可。
            //重复
            return i;
        }
    }
    //不重复
    return -1;
}

为了便于创建占位房屋,我还在House类中重载了构造函数。

public House(String roomNumber){
    this.roomNumber = roomNumber;
}

initialize()函数的具体代码较为复杂,具体如下所示。

@FXML
private void initialize() {
    //&#x6839;&#x8282;&#x70B9;root
    TreeItem<house> root = new TreeItem<>( new House("&#x623F;&#x5C4B;&#x5217;&#x8868;"));
    //&#x7ED1;&#x5B9A;root
    table.setRoot(root);
    root.setExpanded(true);
    //&#x904D;&#x5386;&#x623F;&#x5C4B;&#x5217;&#x8868;
    for (House house : Structure.getStructure().houseList){
        //&#x5BF9;&#x4E8E;&#x53D6;&#x51FA;&#x7684;house
        //&#x53D6;&#x697C;&#x76D8;
        //&#x68C0;&#x67E5;&#x91CD;&#x590D;&#x6027;
        if (repeatText(root.getChildren(), house.getHouseName()) == -1){
            //&#x697C;&#x76D8;&#x4E0D;&#x91CD;&#x590D;
            //&#x697C;&#x76D8;
            //&#x521B;&#x5EFA;&#x5360;&#x4F4D;&#x623F;&#x5C4B;&#xFF0C;&#x4F5C;&#x4E3A;&#x6839;&#x8282;&#x70B9;&#x7684;&#x5B50;&#x8282;&#x70B9;
            root.getChildren().add(new TreeItem<house>(new House(house.getHouseName())));
            //&#x697C;&#x53F7;
            int pos = root.getChildren().size()-1;
            //&#x521B;&#x5EFA;&#x5360;&#x4F4D;&#x623F;&#x5C4B;&#xFF0C;&#x4F5C;&#x4E3A;&#x5B50;&#x8282;&#x70B9;
            root.getChildren().get(pos).setExpanded(true);
            root.getChildren().get(pos).getChildren().add(
                    new TreeItem<house>(new House(house.getHouseNumber()))
            );
            //&#x697C;&#x5C42;
            //&#x521B;&#x5EFA;&#x5360;&#x4F4D;&#x623F;&#x5C4B;&#xFF0C;&#x4F5C;&#x4E3A;&#x5B50;&#x8282;&#x70B9;
            root.getChildren().get(pos).getChildren().get(0).setExpanded(true);
            root.getChildren().get(pos).getChildren().get(0).getChildren().add(
                    new TreeItem<house>(new House(house.getHouseFloor()))
            );
            //&#x623F;&#x5C4B;&#x5B58;&#x5165;
            root.getChildren().get(pos).getChildren().get(0).getChildren()
                    .get(0).setExpanded(true);
            root.getChildren().get(pos).getChildren().get(0).getChildren()
                    .get(0).getChildren().add(new TreeItem<house>(house));
        }else {
            //&#x697C;&#x76D8;&#x91CD;&#x590D;
            //&#x697C;&#x76D8;&#x5728;root&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;nameRepeatPos
            int nameRepeatPos = repeatText(root.getChildren(), house.getHouseName());
            //&#x68C0;&#x67E5;&#x697C;&#x53F7;&#x91CD;&#x590D;&#x6027;
            if (repeatText(root.getChildren().get(nameRepeatPos).getChildren(), house.getHouseNumber()) == -1){
                //&#x697C;&#x76D8;&#x91CD;&#x590D;&#xFF0C;&#x4F46;&#x697C;&#x53F7;&#x4E0D;&#x91CD;&#x590D;
                //&#x697C;&#x53F7;
                root.getChildren().get(nameRepeatPos).setExpanded(true);
                root.getChildren().get(nameRepeatPos).getChildren().add(
                        new TreeItem<house>(new House(house.getHouseNumber()))
                );
                //&#x697C;&#x5C42;
                int pos = root.getChildren().get(nameRepeatPos).getChildren().size()-1;
                root.getChildren().get(nameRepeatPos).getChildren().get(pos).setExpanded(true);
                root.getChildren().get(nameRepeatPos).getChildren().get(pos).getChildren().add(
                        new TreeItem<house>(new House(house.getHouseFloor()))
                );
                //&#x623F;&#x5C4B;&#x5B58;&#x5165;
                root.getChildren().get(nameRepeatPos).getChildren().get(pos).getChildren()
                        .get(0).setExpanded(true);
                root.getChildren().get(nameRepeatPos).getChildren().get(pos).getChildren()
                        .get(0).getChildren().add(new TreeItem<house>(house));
            }else {
                //&#x697C;&#x76D8;&#x91CD;&#x590D;&#xFF0C;&#x697C;&#x53F7;&#x91CD;&#x590D;
                //&#x697C;&#x53F7;&#x5728;root&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;numRepeatPos
                int numRepeatPos = repeatText(root.getChildren().get(nameRepeatPos).getChildren(), house.getHouseNumber());
                //&#x68C0;&#x67E5;&#x697C;&#x5C42;&#x91CD;&#x590D;&#x6027;
                if (repeatText(
                        root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                        , house.getHouseFloor()) == -1){
                    //&#x697C;&#x76D8;&#x91CD;&#x590D;&#xFF0C;&#x697C;&#x53F7;&#x91CD;&#x590D;&#xFF0C;&#x697C;&#x5C42;&#x4E0D;&#x91CD;&#x590D;
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).setExpanded(true);
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren().add(
                            new TreeItem<house>(new House(house.getHouseFloor()))
                    );
                    //&#x623F;&#x5C4B;&#x5B58;&#x5165;
                    int pos = root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren().size()-1;
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                            .get(pos).setExpanded(true);
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                            .get(pos).getChildren().add(new TreeItem<house>(house));
                }else {
                    //&#x697C;&#x76D8;&#x91CD;&#x590D;&#xFF0C;&#x697C;&#x53F7;&#x91CD;&#x590D;&#xFF0C;&#x697C;&#x5C42;&#x91CD;&#x590D;
                    //&#x697C;&#x5C42;root&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;floorRepeatPos
                    int floorRepeatPos = repeatText(
                            root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                            , house.getHouseFloor());
                    //&#x623F;&#x5C4B;&#x5B58;&#x5165;
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                            .get(floorRepeatPos).setExpanded(true);
                    root.getChildren().get(nameRepeatPos).getChildren().get(numRepeatPos).getChildren()
                            .get(floorRepeatPos).getChildren().add(new TreeItem<house>(house));
                }
            }
        }
    }
    //&#x5411;&#x5217;&#x8868;&#x4E2D;&#x6DFB;&#x52A0;&#x6761;&#x76EE;
    houseTree_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new eadOnlyStringWrapper(houseTemp.getValue().getValue().getRoomNumber()));
    purchaseNumber_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new ReadOnlyStringWrapper(houseTemp.getValue().getValue().getPurchaseNumber()));
    code_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new ReadOnlyStringWrapper(houseTemp.getValue().getValue().getCode()));
    houseOwnerID_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new ReadOnlyStringWrapper(houseTemp.getValue().getValue().getHouseOwnerID()));
    add_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new ReadOnlyStringWrapper(houseTemp.getValue().getValue().getAdd()));
    purchaseDate_List.setCellValueFactory((TreeTableColumn.CellDataFeatures<house, string> houseTemp) ->
            new ReadOnlyStringWrapper(houseTemp.getValue().getValue().getPurchaseDate()));

}
</house,></house,></house,></house,></house,></house,></house></house></house></house></house></house></house></house></house></house></house>

最后运行就可以达到树表的效果。

【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)

4、未来优化

由于时间比较紧张,这个部分还有很大的优化空间。由于树表计算部分设置在控制器的初始化函数中,因此每次打开房屋页面都要重新计算树表,在房屋数量较多时,这会造成性能浪费。因此,可以在创建每个新房屋时,将该房屋加入树表中,并将这个树表储存起来。每次打开页面时便免去了复杂的计算。

Original: https://www.cnblogs.com/Wu-765279087/p/16375601.html
Author: IDEA_W
Title: 【Javafx】以树状列表的形式显示类(TreeTableView控件的使用)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/588389/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • java将excel转成pdf

    分享一个简单的excel转pdf 1、引用aspose-cells工具 2、由于转换后会产生水印,去除PDF水印,需要进行权限认证,权限认证文件是license.xml,已经放在网…

    Java 2023年6月7日
    073
  • Nginx 源码分析– 浅谈对模块module 的基本认知

    分析nginx源码,谈到模块module是必然的。纵观nginx源码,可以说模块module机制是整个nginx的骨架。因此,对nginx的源码进行分析,那么对模块module就需…

    Java 2023年6月15日
    073
  • 《回炉重造》——泛型

    泛型 前言 以前学习到「泛型」的时候,只是浅浅的知道可以限制类型,并没有更深入理解,可以说基础的也没理解到位,只是浮于表面,所以,现在回炉重造,重学泛型!打好基础! 什么是泛型? …

    Java 2023年6月10日
    091
  • vue前后端分离项目,使用宝塔面板解决跨域问题,设置Nginx反向代理

    开发环境解决跨域问题: 使用教程 跨域解决本地跨域问题 非唯一方法 生成环境解决跨域问题: 1.创建站点 2.把打包的vue项目dist发送解压到站点里面 ,选择静态模式 3.然后…

    Java 2023年6月5日
    0119
  • Docker在官网下载Tomcat镜像里面没有ip addr等命令解决思路

    我们在看狂神说Docker时,在官网pull了Tomcat的镜像,运行命令发现以下报错: [root@centos7 ~]# docker run -d -P –name tom…

    Java 2023年6月5日
    092
  • 使用Visual Studio Code开发和调试Java Burp扩展

    几天前,我发布了Bug Diaries Burp扩展。这是一个Burp扩展程序,使社区(免费)版 Burp 拥有相似的issue功能 。由于某些原因,现在决定用Java重写。这是我…

    Java 2023年5月29日
    094
  • Vmware 虚拟机无法启动

    问题背景: 自己的电脑坏了,用的事小伙伴的电脑,安装VMware 软件,然后创建虚拟机(放在移动硬盘上)。在操作虚拟主机的时候,中间不小心碰到了移动硬盘, 然后移动硬盘就掉线了。这…

    Java 2023年5月30日
    073
  • Java如何对HashMap按值进行排序–非String int 类型时

    比如:Float 可以通过相减 取整返回,如下方: Java如何对HashMap按值进行排序_上善若水,水善利万物而不争。-CSDN博客https://blog.csdn.net/…

    Java 2023年5月29日
    075
  • vue路由守卫用于登录验证权限拦截

    vue路由守卫用于登录验证权限拦截 to:进入到哪个路由去 from:从哪个路由离开 next:路由的控制参数,常用的有next(true)和next(false) home需要判…

    Java 2023年6月15日
    071
  • MySQL的主从复制和分库分表初探

    主从复制 + 分库分表 要讲主从复制,首先来看看MySQL自带的日志文件。 日志 错误日志 错误日志是 MySQL 中最重要的日志之一,它记录了当 mysqld 启动和停止时,以及…

    Java 2023年6月15日
    074
  • 重构聚合支付案例教你如何写出高扩展性易读的代码

    人间清醒 以下代码逻辑为:按照不同的支付方式调用不同支付方式的逻辑流程。痛点: /** * 旧的支付 * * @param mode 模式 * @param payVO 支付签证官…

    Java 2023年6月5日
    0101
  • .net 基础服务开源战略规划备忘录

    公司现状 技术反馈渠道限制: 公司业务线暂不多,基础服务的应用面尚属狭窄;基础服务和镜像各种环境的适应性和性能不足以及时凸显暴露出来,框架bug和问题使用反馈周期太长,不足以快速跟…

    Java 2023年6月8日
    082
  • 登陆认证框架:SpringSecurity

    最近想给自己的小系统搭建一个登录认证服务,最初是想着一套oauth2权鉴就可以,但是发现这个oauth2只是权鉴,具体的登录认证需要由 SpringSecurity来进行实现。也就…

    Java 2023年6月7日
    074
  • 随机生成数组+冒泡排序

    示例如下: public class MaoPaoPaiXu { public static int[] mp(int[] nums) { for (int i = 0; i &l…

    Java 2023年6月6日
    0114
  • Spring框架的XML扩展特性

    Spring框架从2.0版本开始,提供了基于Schema风格的XML扩展机制,允许开发者扩展spring配置文件。现在我们来看下怎么实现这个功能,可以参考spring帮助文档中的《…

    Java 2023年5月30日
    074
  • 如何使用html制作简历

    今天跟大家分享一下,如何制作简历页面 从图片可以观察到,头部”个人简历”使用那几个常用的文本标签或者 , 即可;简历里有很多的栏,这就需要用到table标签…

    Java 2023年6月8日
    082
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球