0

我正在尝试制作一个 JTree 来复制放置在其上的文件的结构。我遇到的问题是我的代码只是我添加的第一个文件。当我尝试将另一个文件 d&d 到树中时,它会被忽略。这是拖放功能:

root = new DefaultMutableTreeNode("Home");
JTree jtree = new JTree(root);


/**
* JScrollPane and other swing code here.
* Try-catch blocks in the code below were
* removed for readability
**/


jtree.setDropTarget(new DropTarget() {
        public void drop(DropTargetDropEvent evt) {
            evt.acceptDrop(DnDConstants.ACTION_COPY);
            ArrayList<File> inputFiles = new ArrayList<>();
            inputFiles.addAll((List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                updateTree(inputFiles);
        }
    });

这是我用来添加节点的方法:

private void updateTree(ArrayList<File> inputFiles){

    for(File f: inputFiles){
        String[] filePath = f.getPath().split("\\\\");
        DefaultMutableTreeNode node = root;
        DefaultMutableTreeNode child;

            for(String g: filePath){
                child = new DefaultMutableTreeNode(g);
                node.add(child);
                node = child;
            }
     }
}
4

0 回答 0