1

I have a popup with settings displayed to the user. If you click outside it, its hidden but if you click inside it remains visible.

The event handler handling this behavior gets the Component (that was clicked) and by using component.getParent() recursively I can check if its a child of my settings panel. This has worked so far.

But I just added a JComboBox to that panel and it turns out that the "selectable items popup" (does it have a name?) the combobox shows when clicked isnt a child of the combobox. Attempting to select something in a combobox would hide my settings panel.

Using the NetBeans debugger I can see its of the type BasicComboPopup$1 (is that an anonymous class?), but it isn't an instance of neither ComboPopup, JPopupMenu nor BasicComboPopup.

I need a way to identify the owner/parent combobox of the "combobox popup" that was clicked.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
  • Yes, that is an anonymous class. Also, a wild guess, the popup parent will be the combobox popup so it can be drawn inside it. – SJuan76 Aug 24 '12 at 11:46
  • 2
    could be good question, can you please post an SSCCE demonstrated your Components hierarchy. – mKorbel Aug 24 '12 at 11:47
  • @SJuan76 the parent is a `JViewport`. – Mizipzor Aug 24 '12 at 11:52
  • @mizipzor parent is JViewport, not (event I use that) don't use JViewport, this isn't Component and Container too, use JLayer, basic stuff is GlassPane, but carrefully with lightweight status for JViewport and GlassPane – mKorbel Aug 24 '12 at 12:33
  • curious: how do you keep open two popupMenus (the settings and the combo popup)? – kleopatra Aug 24 '12 at 12:35

2 Answers2

5

not entirely sure, but you might be looking for

 popup.getInvoker();

which would return the invoking comboBox.

Below's utility method (copied from SwingXUtilities, which comes with the SwingX framework): given you found the source component (unfortunate naming in the method is focusOwner ;-) of an event, it checks whether that source is somehwhere below the parent, including popups.

Just noticed that your parent is-a popup, so you have to adjust the logic a bit, switching the first and second if block (didn't try, though - it's unusual to have more than one visible popups. :-)

/**
 * 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;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Although the clicked component wasn't a `JPopupMenu`, walking its parents recursively (like your code does) I found one. Then its `getInvoker()`'s parents had the combobox. – Mizipzor Aug 24 '12 at 12:49
1
  1. not sure if you talking about

    • mouse event

    • keyboard event

    • mouse and keyboard event

  2. have look at SwingUtilities there are methods for child v.s. parent and vice versa

  3. post an SSCCE, with detailed descriptions about desired events, becaue there a few ways how to extraxt and modify the Popup from JComboBox

EDIT

in the case that you use AWT Popup or mixing Swing lightweight with AWT heavyweight components, then you have to look at Swing Utils by Darryl

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Its a custom event. We do a lot of custom drawing so we have created a `MousePressListener` which has the method `void mousePressed(Component c)` which is the one I'm implementing to create the behavior. – Mizipzor Aug 24 '12 at 11:54
  • did you `1)` replace popup with your own, maybe then there are couple of side effecta, all custom popup (my view) are based on J/Window or undecorated Swing JDialog), `2)` or extract, modify and add returns back to JComboBox, `3)` maybe someone created that similair way as your, have got the same issue, have to wait – mKorbel Aug 24 '12 at 12:05
  • have to check [BugParade](http://stackoverflow.com/questions/11245982/open-jpopupmenu-from-opened-jcombobox/11246209#11246209) – mKorbel Aug 24 '12 at 12:08
  • I'm trying to extract a SSCCE, but its hard when we've added so much custom event handling. Would have been easier if we stuck to the built in stuff. – Mizipzor Aug 24 '12 at 12:16
  • 3
    *"custom event handling"* (Shudder) What is the odds that if you were able to remove that code, the problem would vanish? – Andrew Thompson Aug 24 '12 at 12:18
  • 2
    @AndrewThompson thats my current approach, get rid of the mouse event handler class, use the builtin stuff, if it still fails at least I will be able to provide a SSCCE. – Mizipzor Aug 24 '12 at 12:20