Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Hibernate

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

  1. #1
    8831085 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Question 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;

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The code is buried in the UI implementation. Don't reinvent the wheel. Just use a JPopupMenu.

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Ever considered looking at its source code?

    zip://....../jdk.../src.zip!/javax/swing/JPopUpMenu.java
    (the source code is reattached in Eclipse and is visible by opening the class (Ctrl+Mouse1 on class in your code))

    Edit:
    Be aware that must of Swing, AWT and undocumented classes are feed parameters from the JVM you can't reach.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    8831085 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    Ever considered looking at its source code?
    - 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);

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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…
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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)
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by camickr View Post
    The code is buried in the UI implementation. Don't reinvent the wheel. Just use a JPopupMenu.
    Do not hate on wheel reinvents!
    8831085 likes this.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    8831085 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    - 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 ;
    Last edited by 8831085; 08-10-2011 at 04:22 PM.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Again you still haven't explained why you are trying to reinvent the wheel. What is wrong with using a JPopupMenu???

  10. #10
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by 8831085 View Post
    - 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.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  11. #11
    8831085 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    - 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;

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    but my problem is solved
    What problem? You still haven't stated what your problem is?

    All you have to do use use a JPopupMenu and you don't have a problem.

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2011, 08:50 AM
  2. Replies: 2
    Last Post: 10-01-2010, 08:18 PM
  3. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 11:14 PM
  4. Random size of JComponent
    By Karl-von-bahnhof in forum AWT / Swing
    Replies: 0
    Last Post: 04-13-2010, 09:14 PM
  5. JComponent gradient background
    By snipered in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2008, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •