DefaultTreeSelectionModel.addSelectionPaths() 的正确格式

发布于 2024-11-06 04:28:03 字数 1767 浏览 5 评论 0原文

我正在使用一个扩展 DefaultTreeSelectionModel 的类来创建 CheckBoxTree。当单击树上的复选框节点时,它会触发一个 MouseEvent,该事件直接从 JTree(“树”)获取 TreePath。

TreePath path = tree.getPathForLocation(me.getX(), me.getY());

该“路径”被传递到 DefaultTreeSelectionModel.addSelectionPath() 函数中,并选择适当的复选框。到目前为止,一切都很好!

我想做的是将我自己的 TreePath 传递到 DefaultTreeSelectionModel.addSelectionPath() 中,但到目前为止,我遇到了 TreePath 格式的问题,这似乎与我从上面的代码中获得的 TreePath 不同,因为它不具有相同的效果;这就是我的问题。

当我单击复选框(工作方式)时,TreePath 打印输出是“[/,文档和设置,管理员]”,但为了模拟,我必须创建一个类似以下的 TreePath:

File[] finalPath = new File[3];
finalPath[0] = (File)parentNode; // parentNode is the root from the Model
finalPath[1] = new File("Documents and Settings");
finalPath[2] = new File("Administrator");
TreePath path = new TreePath(finalPath);

... 这不起作用,因为索引 1 和 2 不引用根目录,但它会打印输出为“[/,文档和设置,管理员]”,就像工作单击方法一样。

然后我尝试:

File[] finalPath = new File[3];
finalPath[0] = (File)parentNode; // parentNode is the root from the Model
finalPath[1] = new File(finalPath[0],"Documents and Settings");
finalPath[2] = new File(finalPath[1],"Administrator");
TreePath path = new TreePath(finalPath);

...这使得选择“parentNode”并选择“Administrator”,但不选择“Documents and Settings”。打印输出是“[/,/Documents and Settings,/Documents and Settings/Administrator]”

所以我想,它必须将所有三个部分作为单独的选择进行处理,以便出现奇数次的任何内容都将保持选中状态,这就是为什么“文档和设置”没有坚持下去。所以,我简单地尝试了一下。

File[] finalPath = new File[1];
finalPath[0] = new File(parentNode+"Documents and Settings"+File.separator+"Administrator");
TreePath path = new TreePath(finalPath);

...它没有选择任何东西。打印输出是“[\Documents and Settings\Administrator]”

所以在大约 5 个小时的尝试各种排列之后,我不知道上面的排列是我最接近得到它的排列,现在我在这里发布我的问题,希望能得到解决回复。 谢谢!!!!

I'm working with a class that extends the DefaultTreeSelectionModel to create a CheckBoxTree. When clicking a checkbox node on the tree it fires a MouseEvent which gets the TreePath directly from the JTree ("tree").

TreePath path = tree.getPathForLocation(me.getX(), me.getY());

This "path" is passed into the DefaultTreeSelectionModel.addSelectionPath() function and the appropriate CheckBoxes are selected. So far so good!

What I'd like to do is pass my own TreePath into DefaultTreeSelectionModel.addSelectionPath() but so far I'm having issues with the format of the TreePath which seems to differ from the TreePath I get from the code above because it doesn't have the same effect; and that is my problem.

When I click the CheckBox (the way that works) the TreePath printout is "[/, Documents and Settings, Administrator]", but to simulate that I'd have to create a TreePath like:

File[] finalPath = new File[3];
finalPath[0] = (File)parentNode; // parentNode is the root from the Model
finalPath[1] = new File("Documents and Settings");
finalPath[2] = new File("Administrator");
TreePath path = new TreePath(finalPath);

... which doesn't work because index 1 and 2 don't reference the root, but it does printout as "[/, Documents and Settings, Administrator]" like the working click method.

So then I tried:

File[] finalPath = new File[3];
finalPath[0] = (File)parentNode; // parentNode is the root from the Model
finalPath[1] = new File(finalPath[0],"Documents and Settings");
finalPath[2] = new File(finalPath[1],"Administrator");
TreePath path = new TreePath(finalPath);

... which leaves the "parentNode" selected and "Administrator" selected, but not "Documents and Settings". The printout is "[/, /Documents and Settings, /Documents and Settings/Administrator]"

So I figured, it must be processing all three parts as separate selections such that anything that appears an odd number of times will stay selected and that's why "Documents and Settings" didn't stick. So, I tried simply.

File[] finalPath = new File[1];
finalPath[0] = new File(parentNode+"Documents and Settings"+File.separator+"Administrator");
TreePath path = new TreePath(finalPath);

... which doesn't select anything. The printout is "[\Documents and Settings\Administrator]"

So after about, I donno, 5 hours of trying various permutations the ones above are the closest I came to getting it and now I'm posting my issue here in hopes of a response.
Thanks!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文