有没有办法检测 JTree 上是否即将发生删除?
我有一个 JTree,用户可以在其中删除其他组件中的元素。当用户将鼠标悬停在树中的节点上(在“放置模式”期间)时,最近的节点会突出显示。这是在TransferHandler的实现中实现的。
@Override
public boolean canImport(TransferSupport support) {
//Highlight the most near lying node in the tree as the user drags the
//mouse over nodes in the tree.
support.setShowDropLocation(true);
每次选择新节点时(也在“放置模式”期间),都会触发 TreeSelectionEvent。这反过来将调用我创建的侦听器,该侦听器将查询数据库以获取与该节点相关的详细信息。
现在,我正在寻找一种方法来以某种方式过滤掉“放置模式”期间节点选择生成的事件。这是一种限制数据库调用的尝试。有人对我如何实现这一目标有任何想法吗?
所有的意见都将受到高度赞赏!
I have a JTree where users can drop elements from other components. When the users hovers over nodes in the tree (during "drop mode") the most near lying node is highlighted. This is achieved in the implementation of TransferHandler.
@Override
public boolean canImport(TransferSupport support) {
//Highlight the most near lying node in the tree as the user drags the
//mouse over nodes in the tree.
support.setShowDropLocation(true);
Each time a new node is selected (also during "drop mode"), this will kick of a TreeSelectionEvent. This in turn will invoke a listener i have created which will query a database for details related to that node.
Now, I am looking for a way to somehow filter out events that is generated from node selections during "drop mode". It is an attempt to limit database calls. Does anyone have any ideas about how I can achieve this?
All input will be highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一种非常间接的方法可以检测这种情况。您可以在树组件的属性
“dropLocation”
上注册一个PropertyChangeListener
。每当放置位置发生变化时都会调用此函数,因此您可以在那里设置一个字段dropOn
,然后您可以在TreeSelectionListener
中读取该字段。请注意,这确实会在第一次进入树时触发错误的
false
值,但所有后续事件都会显示dropOn = true
。There is a very indirect method to detect this case. You may register a
PropertyChangeListener
on the property"dropLocation"
with the tree component. This will be called whenever the drop location changes and thus you can set a fielddropOn
there which you then can read in theTreeSelectionListener
.Note that this does fire a wrong
false
value for the first time it enters the tree, but all subsequent events then showdropOn = true
.