java - setGraphic() 在递归创建的 TreeItems 上无法正常工作

标签 java recursion javafx treeview assign

我正在编写一个文件夹同步应用程序。目前,我正在研究负责递归地遍历两个用户指定的目录结构,将其中的文件夹和文件与其他结构进行比较,并显示每个文件或文件夹是否未更改、更改或新增的部分,方法是:彩色点的意思。问题在于,程序在当前状态下,在正确评估关系时,仅在每个点颜色的一个 TreeItem 上显示点,而不是在所有树项上显示点。图片供引用: enter image description here

这是什么原因造成的?我怀疑这与 Java 中对象分配的工作方式有关,因此我以某种方式将同一个对象重新分配给所有正确的 TreeItems,仅在最后一个对象处停止,但这太宽泛了,无法处理。请参阅下面的有问题的函数。

private void compareAndFillInSourceTreeView(Path x, TreeItem root) throws IOException {
    String xSourceName = x.getName(x.getNameCount() - 1).toString();
    String xTargetName = (getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))
            .toString();

    System.out.println("-----------------------------------------------------------------------------------------");
    System.out.println("NEW CALL: " + x.toString() + " " + root);
    System.out.println("EQUIVALENT: " + getEquivalentFileInTarget(x) + " EXISTS: " +
            getEquivalentFileInTarget(x).toFile().exists());
    System.out.println("IS NEW: " + xTargetName + ", " + (xTargetName == null));
    System.out.println("UNCHANGED: " + x + " " + getEquivalentFileInTarget(x) + " NAMES: " + xSourceName + ", "
            + xTargetName);
    System.out.println("CHANGED: " + ((x.getName(x.getNameCount() - 1)) ==
            getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1)));

    if (x.toFile().isFile()) {
        System.out.println("THIS IS A FILE: " + x.toString());

        //if new, i.e. doesn't exist in the target
        if (!getEquivalentFileInTarget(x).toFile().exists()) {
            System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS FILE IN TARGET");
            TreeItem newBranch = makeBranch(xSourceName, root);
            newBranch.setGraphic(blueDotIcon);
        }

        //if unchanged
        else if (sameContents(x, getEquivalentFileInTarget(x)) && (xSourceName.equals(xTargetName))) {
            System.out.println("THIS FILE AND ITS EQUIVALENT ARE EQUAL");
            TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            newBranch.setGraphic(greenDotIcon);
        }

        //if same name, but different contents, i.e. changed
        else if ((x.getName(x.getNameCount() - 1)).equals(
                getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))) {
            TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            newBranch.setGraphic(yellowDotIcon);

        } else {
            System.out.println("BAD, putInTreeView() Error, it should never reach this line");
            System.out.println("Error log: " + x + ", " + getEquivalentFileInTarget(x));
        }

    } else if (x.toFile().isDirectory()){ //if it's a folder, checked explicitly because it's behaving weird
        System.out.println("THIS IS A DIRECTORY: " + x.toString());

        if (getEquivalentFileInTarget(x).toFile().exists()) {
            System.out.println("EQUIVALENT EXISTS FOR THIS DIRECTORY IN TARGET.");
            //make new branches and mark them as existing folders
            TreeItem currentSourceTreeViewRoot = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
            currentSourceTreeViewRoot.setExpanded(true);
            currentSourceTreeViewRoot.setGraphic(greenDotIcon);
            for (File i : x.toFile().listFiles()) {
                System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
                compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
            }

        } else {
            System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS DIRECTORY IN TARGET.");
            //if they don't exist, make the branches anyway and mark them as representing nonexistent folders
            TreeItem currentSourceTreeViewRoot = makeBranch((x.getName(x.getNameCount() - 1)).toString(), root);
            currentSourceTreeViewRoot.setExpanded(true);

            for (File i : x.toFile().listFiles()) {
                System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
                compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
            }
        }
    }
}

最佳答案

你的假设是正确的。当使用setGraphic分配图形时,您是在告诉JavaFX在场景图中的哪个位置找到该节点。当您使用相同的对象作为参数再次调用 setGraphic 时,您实际上将其移动到场景图中的不同位置。

为每个项目创建一个新的点/圆圈,您的问题应该得到解决。

关于java - setGraphic() 在递归创建的 TreeItems 上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671716/

相关文章:

java - IntelliJ IDEA 15 Scene Builder 未显示所有控件

JavaFx TextField 货币格式过滤器

java - 使用类名 Java 获取对象的引用

java - 什么是NullPointerException,我该如何解决?

c++ - 对静态变量的赋值是函数的结果,该函数在内部更改了该静态变量

java - 如何在java中使用递归创建一系列子字符串

recursion - puppet 递归目录创建

java - vm 大小(任务管理器)与 java 应用程序堆大小

java - 向 Windows 控制台发送命令确认

JavaFx:组合框表格单元格双击