我有一个JTree在C盘上显示和传输目录和文件。在这个JTree中,用户可以通过拖放来改变文件的位置。我已经实现了TransferHandler类来处理DefaultMutableTreeNode的传输,同时将文件从一个位置传输到另一个位置。从我第一次拖放文件就可以了(该文件的defaultMutableTreeNode被删除,并创建了新的defaultMutableTreeNode,此文件也在磁盘上传输)。但问题是我第二次这样做。 JTree在更改后不会更新,我无法拖放defaultMutableTreeNode。
我呼叫所有可能的更新功能,例如updateUI,他们没有工作。我花了三天时间,我不知道该怎么做。 我从类扩展TransferHandler中粘贴了两个函数。我认为jtree应该在这个函数中更新。任何人都可以看看它并告诉我何时更新这棵树以及如何?
//bundle up selected files into one list and export it
protected Transferable createTransferable(JComponent c) {
DefaultMutableTreeNode tmpNode;
JTree node = (JTree)c;
TreePath[] treePaths = node.getSelectionPaths();
if(treePaths != null) {
ArrayList<DefaultMutableTreeNode> nodesToCopy = new ArrayList<DefaultMutableTreeNode>();
fileToCopy = new ArrayList<File>();
ArrayList<DefaultMutableTreeNode> nodesToRemove = new ArrayList<DefaultMutableTreeNode>();
fileToRemove = new ArrayList<File>();
DefaultMutableTreeNode topNode = (DefaultMutableTreeNode)treePaths[0].getLastPathComponent();
// File dir = (File)topNode.getUserObject();
for(int i=0; i<treePaths.length; i++) {
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)treePaths[i].getLastPathComponent();
File file = (File)currentNode.getUserObject();
if(currentNode.getLevel() < topNode.getLevel())
break;
if (file.isDirectory()) {
tmpNode = FileNodeTree.createNodes(null,file);
nodesToCopy.add(tmpNode);
fileToCopy.add(file);
fileToRemove.add(file);
nodesToRemove.add(currentNode);
} else {
nodesToCopy.add(copyToNode(file));
fileToCopy.add(file);
nodesToRemove.add(currentNode);
fileToRemove.add(file);
}
}
DefaultMutableTreeNode[] nodes = nodesToCopy.toArray(
new DefaultMutableTreeNode[nodesToCopy.size()]);
removeNodes = nodesToRemove.toArray(
new DefaultMutableTreeNode[nodesToRemove.size()]);
return new FilesTransferable(nodes);
}
return null;
}
private DefaultMutableTreeNode copyToNode(File node) {
return new DefaultMutableTreeNode(node);
}
public boolean importData(TransferHandler.TransferSupport support) {
DefaultMutableTreeNode[] nodes = null;
if(!canImport(support))
return false;
try {
nodes = (DefaultMutableTreeNode[]) support.getTransferable().getTransferData(fileFlavor);
for(DefaultMutableTreeNode nodess : nodes)
System.out.println("nodess: "+nodess);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (UnsupportedFlavorException e1) {
System.err.println("UnsupportedFlavorException: "+e1.getMessage());
}
//fetch the drop location, path & childIndex
JTree.DropLocation dropLoc = (JTree.DropLocation)support.getDropLocation();
TreePath path = dropLoc.getPath();
int childIndex = dropLoc.getChildIndex();
//create new nodes and insert it into JTree
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path.getLastPathComponent();
File top = (File)parent.getUserObject();
JTree tree = (JTree)support.getComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
//configure for dropMode
int index = childIndex; //for dropMode.INSERT
if(childIndex == -1) //for dropMode.ON
index = parent.getChildCount();
//insert data into JTree
for(int i=0; i<nodes.length; i++)
model.insertNodeInto(nodes[i], parent, index++);
try {
for(File file : fileToCopy) {
if(file.isDirectory()) {
FileUtils.copyDirectory(file, new File(top+"/"+file.getName()));
} else {
Files.copy(file.toPath(),
(new File(top+"/"+file.getName()).toPath()), StandardCopyOption.REPLACE_EXISTING);
}
}
} catch (IOException e) {
System.err.println("Cannot copy to other directory " + e.getMessage());
}
return true;