Java:当 Jtree 展开节点或折叠节点时,getRowForLocation() 返回不同的 row int
JTree jtree
附加了一个鼠标侦听器。当用户单击 Jtree 组件节点时,我通过
jtree.getRowForLocation(evt.getX(), evt.getY());
问题获取它的行整数,问题是当存在以下 jtree 节点时,返回行整数不同展开以及折叠时。
我需要一种方法来以某种方式获取单击的每个 jtree 节点的唯一且绝对的 ID,这样就不会造成混淆。
有没有办法向Jtree组件添加数据?
否则,我需要一种方法来监听节点何时展开并重新计算从上述函数返回的行......
JTree jtree
has a mouse listener attached to it.when the user clicks on a Jtree component node, I get it's row integer via
jtree.getRowForLocation(evt.getX(), evt.getY());
the problem is that the return row integer is different when there are jtree nodes which are expanded and when it's collapsed.
I need a way to somehow get a unique and absolute ID of each jtree nodes clicked so that there is no confusions.
Is there a way to add data to the Jtree component?
Otherwise, I need a way to listen to when nodes are expanded and recalculate the row returned from that above function....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在当前的方法中,您依靠树视图的状态来识别节点。然而,正如您所发现的,视图的状态可以改变。不变的是视图底层的树模型。
如果您需要为每个节点提供唯一的 ID,那么您应该将其直接添加到树模型中的每个树节点。然后,您可以在鼠标侦听器中检测您单击的节点并访问其唯一 ID。
In your current approach you are relying on the state of the tree view to identify the node. However, as you have found out the state of the view can change. What does not change is the tree model that is underlying the view.
If you need a unique ID for each node then you should add this directly to each tree node in your tree model. In the mouse listener you can then detect the node that you clicked on and access its unique ID.
路径唯一标识节点,因此 getPathForLocation 将为您提供路径,但这是一个 TreePath 对象,因此如果您必须有一个整数表示您需要编写的 ID将 TreePath 转换为整数的方法。
Paths uniquely identify nodes, so
getPathForLocation
will give you the path, but that is aTreePath
object, so if you have to have an integer represent the ID you would need to write a method that translatesTreePath
s to integers.