hiding JComponent when jframe resized,lostFocus,mousePressedAnywhere Just like JPOPUP

- Hi ;
- I have a JFrame, it contains a JComponent;
- I want the JComponent to become invisible when the JFrame is resized or lost focus or mouse pressed on any component or anything else that makes a JPopUpMenu or a JMenu become invisible;
- I don't wanna add a listener for each of the components in the jframe ;
- I would be thankful if anybody know how JPopUpMenu does this;

- I did try to look at JPopUpMenu 's source. but i didn't find out anything;
- I think it might be implemented somewhere else. perhaps in the Container or JFrame;
- I dunno (i'm still trying to find out);

I would assume it is behaviour is implemented in a object handled may the JVM.
If I remember it right, when a object (AWT/Swing) is created JVM identifies the class and ands it behaviour, but it is always worth a try to look in the source.

I will however post my solution candidate when I have looked up that some methods are called…

I think the process* methods are invoked are bottom component, and forwarded to next component if not consumed. (This is just a theory, I have never examined the behaviour of the for controls with subcontrols.)

Classes like Window, JFrame, JPopupMenu, &a have methods with names starting with process (and postProcess) and are protected. Overload them process parameters, invoke event.consume() (ugly enough called e.consume() in Java's source) if the data is want you expected otherwise pass on to super's implementation of the method (for example super.processWindowEvent(event)).

You may want to example the source's files, but some of the methods are called (with example class of source's implementation):
Window.processEvent(AWTEvent) (It may do the trick just to override this one)
Window.processWindowEvent(WindowEvent)
Window.processWindowFocusEvent(WindowEvent)
Window.processWindowStateEvent(WindowEvent)
JPopupMenu.processKeyEvent(KetEvent)
Component.processMouseEvent(MouseEvent)

- Thank you for your hearty help Hibernate ;
- but i'm afraid processEvent() is not exactly what i need;
- cause it doesn't handle
> mouseClicks on the components
> or mouseClicks on the contentPane
> or resizing the JFrame;
- and also we must override the method for every JFrame that we want to add the JComponent to it;
-
- I searched a lot about my problem. and i think i've found a clue;
- It seems javax.swing.plaf.basic.BasicPopupMenuUI is behind all of this;
- I tried to read the code but it's too confusing & i can't understand ;
- I hope some body may have an idea about this ;
- It looks i have to create a class that extends ComponentUI and set my JComponents UI to that ;
- Well, i,m not sure maybe somebody can help ;

- Thank you for your hearty help Hibernate ;
- but i'm afraid processEvent() is not exactly what i need;
- cause it doesn't handle
> mouseClicks on the components
> or mouseClicks on the contentPane
> or resizing the JFrame;
- and also we must override the method for every JFrame that we want to add the JComponent to it;
-
- I searched a lot about my problem. and i think i've found a clue;
- It seems javax.swing.plaf.basic.BasicPopupMenuUI is behind all of this;
- I tried to read the code but it's too confusing & i can't understand ;
- I hope some body may have an idea about this ;
- It looks i have to create a class that extends ComponentUI and set my JComponents UI to that ;
- Well, i,m not sure maybe somebody can help ;

I like how you end all lines [although missing on two of them, as well as the - line].
You do not need to override the method for every JFrame; you can create a class (MyFrame) and override it in MyFrame and then use MyFrame instead of JFrame.
Yes, BasicPopupMenuUI is behind it all, the UI classes are always behind the magic (actually the magic is passed on to them).

(Form Component's source code)

Override protected void processComponentEvent(ComponentEvent e) and process on e.getID() == ComponentEvent.COMPONENT_RESIZED.
There is also ComponentEvent.COMPONENT_MOVED, ComponentEvent.COMPONENT_SHOWN and ComponentEvent.COMPONENT_HIDDEN.

Override protected void processMouseEvent(MouseEvent e) and process on e.getID() == MouseEvent.MOUSE_PRESSED.
There is also MOUSE_RELEASED, MOUSE_CLICKED, MOUSE_EXITED and MOUSE_ENTERED.

- camickr & Hibernate , thankyou very much ;
- Although i didn't find out how the JPopUpMenu handles all of this , but my problem is solved by overriding the ProcessEvent and some other methods ;
- I also tried to use JPopUpMenu but i couldn't make it opaque cause its a heavyweight Component and it had some other problems like passing JFrame borders, so I decided not to use it;
- best wishes;