删除 Java JTree 结构的子节点
我有一个 ftp 程序,每次展开时都会检索文件夹数据。它通过使用如下模型来做到这一点:
private void FilesTreeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {
String path = new String("");
DefaultMutableTreeNode chosen = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
String[] pathArray = evt.getPath().toString().replaceAll("]", "").split(",");
for (int i = 1 ; i < pathArray.length ; i++) path += "/"+ pathArray[i].trim();
// 我被添加了 selected.removeAllChildren();没有成功
ftp.GoTo(路径);
ArrayList listDir = null;
listDir = ftp.ListDir();
ArrayList listFiles = null;
listFiles = ftp.ListFiles();
DefaultMutableTreeNode child = null , dir = null , X = null;
//this will add files to tree
for (int i = 0; i < listFiles.size(); i++) {
child = new DefaultMutableTreeNode(listFiles.get(i));
if(listFiles.size() > 0)
model.insertNodeInto(child, chosen, 0);
}
//this will add dirs to list
for (int i = 0; i < listDir.size(); i++) {
X = new DirBranch("در حال دریافت اطلاعات ...").node();
dir = new DirBranch( (String) listDir.get(i)).node();
dir.add(X);
if(listDir.size() > 0)
model.insertNodeInto(dir, chosen, 0);
}
FilesTree.setModel(model); //this is my Swing JTree
}
问题是每次我展开 JTree 时,它都会重复文件和文件夹列表。所以我尝试使用 chosen.removeAllChildren(); @ 代码顶部,但它没有删除任何内容。我应该怎么办?
I have a ftp program that retrieve folder data each time expanded. It does this by using a model like this:
private void FilesTreeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {
String path = new String("");
DefaultMutableTreeNode chosen = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
String[] pathArray = evt.getPath().toString().replaceAll("]", "").split(",");
for (int i = 1 ; i < pathArray.length ; i++) path += "/"+ pathArray[i].trim();
// i were aded chosen.removeAllChildren(); without success
ftp.GoTo(path);
ArrayList listDir = null;
listDir = ftp.ListDir();
ArrayList listFiles = null;
listFiles = ftp.ListFiles();
DefaultMutableTreeNode child = null , dir = null , X = null;
//this will add files to tree
for (int i = 0; i < listFiles.size(); i++) {
child = new DefaultMutableTreeNode(listFiles.get(i));
if(listFiles.size() > 0)
model.insertNodeInto(child, chosen, 0);
}
//this will add dirs to list
for (int i = 0; i < listDir.size(); i++) {
X = new DirBranch("در حال دریافت اطلاعات ...").node();
dir = new DirBranch( (String) listDir.get(i)).node();
dir.add(X);
if(listDir.size() > 0)
model.insertNodeInto(dir, chosen, 0);
}
FilesTree.setModel(model); //this is my Swing JTree
}
the problem is every time i expand the JTree it duplicate list of files and folders. so i tried to use chosen.removeAllChildren(); @ the top of the code but it didnt remove anything. what should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的模型是正确的,但 JTree 正在运行旧信息。
removeAllChildren()
方法删除子项,但它不会触发任何事件,而model.insertNodeInto()
会触发插入事件。因此,JTree 可以看到节点被添加,但永远不会看到节点被删除。添加新的子项后,尝试调用
model.reload(chosen)
以使chosen
下面的树无效。由于您将重新加载分支,因此您还可以将
model.insertNodeInto(dir, selected,0)
更改为chosen.insert(dir,0)
。这减少了发布的事件数量。Your model is correct, but JTree is operating on old information.
The
removeAllChildren()
method removes the children, but it does not fire any events and themodel.insertNodeInto()
does fire insert events. So, JTree sees the nodes being added, but never sees the nodes being removed.After adding the new children, try calling
model.reload(chosen)
to invalidate the tree belowchosen
.Since you will be reloading the branch, you can also change
model.insertNodeInto(dir, chosen,0)
tochosen.insert(dir,0)
. That reduces the number of events posted.调用
removeAllChildren()
将从节点中删除子节点。这里一定发生了其他事情正在创建重复项。确保您没有两次调用任何内容并且正在刷新树的显示。Calling
removeAllChildren()
will remove the children from the node. There must be something else happening here that is creating duplicates. Make sure you are not calling anything twice and that you are refreshing the display of the tree.在我的应用程序中,我也遇到了同样的问题。为此,我只使用了以下代码。
它从我的 Jtree 中删除所有子节点。
In my application also i just fact the same problem. For that i just used the following code.
It remove all child nodes from my Jtree.