简体   繁体   中英

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. This does not work in my code, as it complains I cannot install this on to a 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. 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). 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 s represent the data displayed in a tree, they are not UI components. You need to set the tooltip on the TreeCell s, which you can do in a factory.

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

// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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