防止调用 ExpandItem() 时触发事件

发布于 2024-11-30 22:17:30 字数 1221 浏览 0 评论 0原文

标题几乎说明了一切。当我以编程方式执行expandItem()函数时,我不希望触发的事件导致nodeExpand()调用。

我已经实现了 ExpandListener:

@Override
public void nodeExpand(ExpandEvent event)
{
    System.out.println("This should only appear when the user clicks the node on the UI");
}

当我调用 Tree 类的 ExpandItem() 函数时,总会触发一个事件。这是原始 Tree 类的代码:

public boolean expandItem(Object itemId) {
    boolean success = expandItem(itemId, true);
    requestRepaint();
    return success;
}

private boolean expandItem(Object itemId, boolean sendChildTree) {

    // Succeeds if the node is already expanded
    if (isExpanded(itemId)) {
        return true;
    }

    // Nodes that can not have children are not expandable
    if (!areChildrenAllowed(itemId)) {
        return false;
    }

    // Expands
    expanded.add(itemId);

    expandedItemId = itemId;
    if (initialPaint) {
        requestRepaint();
    } else if (sendChildTree) {
        requestPartialRepaint();
    }
    fireExpandEvent(itemId);

    return true;
}

我现在为完成这项工作所做的是:

m_Tree.removeListener((ExpandListener)this);
m_Tree.expandItem(sItemId);
m_Tree.addListener((ExpandListener)this);

有更好的方法吗?

the title says almost everything. When I execute the expandItem() function programmatically I do not want the fired event causing a nodeExpand() call.

I have implemented the ExpandListener:

@Override
public void nodeExpand(ExpandEvent event)
{
    System.out.println("This should only appear when the user clicks the node on the UI");
}

When I call the expandItem() function of the Tree class, there is always an event fired. This is the code of the original Tree class:

public boolean expandItem(Object itemId) {
    boolean success = expandItem(itemId, true);
    requestRepaint();
    return success;
}

private boolean expandItem(Object itemId, boolean sendChildTree) {

    // Succeeds if the node is already expanded
    if (isExpanded(itemId)) {
        return true;
    }

    // Nodes that can not have children are not expandable
    if (!areChildrenAllowed(itemId)) {
        return false;
    }

    // Expands
    expanded.add(itemId);

    expandedItemId = itemId;
    if (initialPaint) {
        requestRepaint();
    } else if (sendChildTree) {
        requestPartialRepaint();
    }
    fireExpandEvent(itemId);

    return true;
}

What I did now to get this work is:

m_Tree.removeListener((ExpandListener)this);
m_Tree.expandItem(sItemId);
m_Tree.addListener((ExpandListener)this);

Is there any nicer approach?

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

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

发布评论

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

评论(1

何止钟意 2024-12-07 22:17:30

您可以尝试为您的侦听器创建一个开关。例如:

private boolean listenerDisabled;

void setListenerDisabled(boolean disabled) 
{
    listenerDisabled = disabled;
}

@Override
public void nodeExpand(ExpandEvent event)
{
    if(listenerDisabled) return;
    System.out.println("This should only appear when the user clicks the node on the UI");
}

并在需要时禁用监听器。

如果有多个侦听器,您可以尝试创建 Tree 的子类并重写一些方法。

You could try to create a switch to your listener. For example:

private boolean listenerDisabled;

void setListenerDisabled(boolean disabled) 
{
    listenerDisabled = disabled;
}

@Override
public void nodeExpand(ExpandEvent event)
{
    if(listenerDisabled) return;
    System.out.println("This should only appear when the user clicks the node on the UI");
}

and disable the listener when needed.

If there are more than one listener, you could try creating a subclass of Tree and overriding some methods.

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