标题几乎说明了一切。当我以编程方式执行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);
有没有更好的方法?
答案 0 :(得分:0)
您可以尝试为听众创建一个开关。例如:
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的子类并覆盖某些方法。