I have a Frame filled with components (including panels filled with components) -- the nesting gets deep. How can I make the same pop-up menu show no matter where I click within the frame, preferably without registering with every nested component?

Sandip Chitale

You could do it very easily using the following method -

public void addAWTEventListener(AWTEventListener listener,
                                long eventMask)
Adds an AWTEventListener to receive all AWTEvents dispatched system-wide that conform to the given eventMask. 
First, if there is a security manager, its checkPermission method is called with an AWTPermission("listenToAllAWTEvents") permission. This may result in a SecurityException. 

eventMask is a bitmask of event types to receive. It is constructed by bitwise OR-ing together the event masks defined in AWTEvent. 

Note: event listener use is not recommended for normal application use, but are intended solely to support special purpose facilities including support for accessibility, event record/playback, and diagnostic tracing. If listener is null, no exception is thrown and no action is performed.

listener - the event listener.
eventMask - the bitmask of event types to receive
The basic idea being -

  • Use the AwtEvent.MOUSE_EVENT_MASK
  • For every mouse event check if it is a popup trigger event (i.e. isPopupTrigger() returns true). If it is then check if the source of the mouse event (returned by getSource()) is contained in your frame (i.e. the top level window in the parent chain is your frame). If it is then translate the co-ordinates of the mouse event to that of the frame. Then show the popup.