有没有办法检测 JTree 上是否即将发生删除?

发布于 2024-11-09 12:39:03 字数 540 浏览 3 评论 0原文

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

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

发布评论

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

评论(1

妄司 2024-11-16 12:39:03

有一种非常间接的方法可以检测这种情况。您可以在树组件的属性“dropLocation”上注册一个PropertyChangeListener。每当放置位置发生变化时都会调用此函数,因此您可以在那里设置一个字段 dropOn ,然后您可以在 TreeSelectionListener 中读取该字段。

tree.addPropertyChangeListener("dropLocation", new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent pce) {
        dropOn = pce.getNewValue() != null;
    }
});

tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent tse) {
        System.out.println(tse + " dropOn=" + dropOn);
    }
});

请注意,这确实会在第一次进入树时触发错误的 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 field dropOn there which you then can read in the TreeSelectionListener.

tree.addPropertyChangeListener("dropLocation", new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent pce) {
        dropOn = pce.getNewValue() != null;
    }
});

tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent tse) {
        System.out.println(tse + " dropOn=" + dropOn);
    }
});

Note that this does fire a wrong false value for the first time it enters the tree, but all subsequent events then show dropOn = true.

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