简体   繁体   English

javafx:如何创建一个TreeItem来显示包含文件路径的工具提示,并双击加载文件路径?

[英]javafx: How do I create a TreeItem that displays a tooltip that contains the file path and load the file path on double click?

I am trying to attach a tooltip, which contains the file path, to the TreeItem<String> , such that when I hover around this TreeItem, it will display a text of the file path when I hover my mouse around it. 我试图将包含文件路径的工具提示附加到TreeItem<String> ,这样当我将鼠标悬停在此TreeItem上时,当我将鼠标悬停在它上面时,它将显示文件路径的文本。 This does not work in my code, as it complains I cannot install this on to a String. 这在我的代码中不起作用,因为它抱怨我无法将其安装到String上。 How can I solve this problem? 我怎么解决这个问题?

Second, I want to be able to double click on the TreeItem then it can automatically load the file. 其次,我希望能够双击TreeItem,然后它可以自动加载文件。 How can I achieve that ? 我该如何实现?

    @FXML
    TreeView<String> fxFileTree;
    public void defineFileTree(){
        TreeItem<String> root = new TreeItem<String>("Portfolio");
        fxFileTree.setShowRoot(true);
        root.setExpanded(true);
        fxFileTree.setRoot(root);
    }

    public void populateTree(String fileName, String filePath){
        addLeaf(fileName, (TreeItem<String>) fxFileTree.getRoot(), filePath);
    }

    public void addLeaf(String leaf, TreeItem<String> parent, String filePath{
        TreeItem<String> item = new TreeItem<>(leaf);
        Tooltip.install(item,filepath)       // <- This is wrong
        parent.getChildren().add(item);
    }

UPDATE: the goal of this exercise is to build a tree which only contains the root and one level of branches, ie root -> leaf1 (stop here,no grandchildren for root, children only). 更新:此练习的目标是建立一棵仅包含根和一个分支级别的树,即root -> leaf1 (在此处停止,没有用于子代的孙子,仅子代)。 The root will just be a title String. 根将只是一个标题字符串。 And I want to add leaves to the root. 我想在根部添加叶子。 The leaf is a file object. 叶是文件对象。 The display text of the leaf will be the file name and install the tooltip for this leaf. 叶子的显示文本将是文件名,并为此叶子安装工具提示。 The tooltip will show the file path. 工具提示将显示文件路径。

You can't set a tooltip on a TreeItem . 您不能在TreeItem上设置工具提示。 TreeItem s represent the data displayed in a tree, they are not UI components. TreeItem表示树中显示的数据 ,它们不是UI组件。 You need to set the tooltip on the TreeCell s, which you can do in a factory. 您需要在TreeCell上设置工具提示,您可以在工厂中进行设置。

Since you are going to need access to the data about the file, you should not be using TreeView<String> and TreeItem<String> : you should either use TreeView<File> or TreeView<Path> (in other words, make the data type of the tree either File or Path ). 由于您将需要访问有关文件的数据,因此不应该使用TreeView<String>TreeItem<String> :您应该使用TreeView<File>TreeView<Path> (换句话说,使数据树的类型( FilePath )。 So you would do something like: 因此,您将执行以下操作:

@FXML
private TreeView<Path> fxFileTree ;

private TreeItem<Path> root ;

// ...

public void initialize() {
    fxFileTree.setCellFactory(tv ->  {
        final Tooltip tooltip = new Tooltip();
        TreeCell<Path> cell = new TreeCell<Path>() {
            @Override
            public void updateItem(Path item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                    setTooltip(null);
                } else if (getTreeItem() == root) {
                    setText("Portfolio");
                    setTooltip(null);
                } else {
                    setText(item.getFileName().toString());
                    tooltip.setText(item.toRealPath().toString());
                    setTooltip(tooltip);
                }
            }
        };
        cell.setOnMouseClicked(e -> {
            if (e.getClickCount() == 2 && ! cell.isEmpty()) {
                Path file = cell.getItem();
                // do whatever you need with path...
            }
        });
        return cell ;
    });
}


public void defineFileTree(){
    root = new TreeItem<>(null);
    fxFileTree.setShowRoot(true);
    root.setExpanded(true);
    fxFileTree.setRoot(root);
}

// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM