JTree只允许我添加1个文件

时间:2013-06-21 21:11:44

标签: java swing jtree

我正在尝试创建一个JTree,它复制了放在其上的文件结构。我遇到的问题是我的代码只添加了第一个文件。当我尝试将另一个文件放入树中时,它会被忽略。这是拖累和删除功能:

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

0 个答案:

没有答案