How to display only the filename in a JavaFX TreeView?

后端 未结 1 754
清酒与你
清酒与你 2021-01-16 18:58

So i have figured out how to get all the files and directories and add them to the treeview but it shows me the complete file path: C/user/file.txt i just want the file or f

相关标签:
1条回答
  • 2021-01-16 19:38

    Use a custom cellFactory to determine, how the items are shown in the TreeView:

    treeView.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
    
        public TreeCell<File> call(TreeView<File> tv) {
            return new TreeCell<File>() {
    
                @Override
                protected void updateItem(File item, boolean empty) {
                    super.updateItem(item, empty);
    
                    setText((empty || item == null) ? "" : item.getName());
                }
    
            };
        }
    });
    
    0 讨论(0)
提交回复
热议问题