删除 Java JTree 结构的子节点

发布于 2024-10-31 08:36:10 字数 1516 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2024-11-07 08:36:10

您的模型是正确的,但 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 the model.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 below chosen.

Since you will be reloading the branch, you can also change model.insertNodeInto(dir, chosen,0) to chosen.insert(dir,0). That reduces the number of events posted.

[旋木] 2024-11-07 08:36:10

调用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.

风尘浪孓 2024-11-07 08:36:10

在我的应用程序中,我也遇到了同样的问题。为此,我只使用了以下代码。

    JTree.removeAll();
    JTree.setModel(null);

它从我的 Jtree 中删除所有子节点。

In my application also i just fact the same problem. For that i just used the following code.

    JTree.removeAll();
    JTree.setModel(null);

It remove all child nodes from my Jtree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文