不完全确定,但您可能正在寻找
popup.getInvoker();
这将返回调用组合框。
下面的实用方法(从 SwingXUtilities 中复制,它是 SwingX 框架附带的):如果您找到了事件的源组件(不幸的是,方法中的命名是 focusOwner ;-),它会检查该源是否在父级下方的某个位置,包括弹出窗口.
只是注意到你的父母是一个弹出窗口,所以你必须稍微调整逻辑,切换第一个和第二个 if 块(虽然没有尝试 - 有多个可见弹出窗口是不寻常的。:-)
/**
* Returns whether the component is part of the parent's
* container hierarchy. If a parent in the chain is of type
* JPopupMenu, the parent chain of its invoker is walked.
*
* @param focusOwner
* @param parent
* @return true if the component is contained under the parent's
* hierarchy, coping with JPopupMenus.
*/
public static boolean isDescendingFrom(Component focusOwner, Component parent) {
while (focusOwner != null) {
if (focusOwner instanceof JPopupMenu) {
focusOwner = ((JPopupMenu) focusOwner).getInvoker();
if (focusOwner == null) {
return false;
}
}
if (focusOwner == parent) {
return true;
}
focusOwner = focusOwner.getParent();
}
return false;
}