aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Prevent JPopupMenu from hiding when clicked outside the menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Prevent JPopupMenu from hiding when clicked outside the menu" Watch "Prevent JPopupMenu from hiding when clicked outside the menu" New topic
Author

Prevent JPopupMenu from hiding when clicked outside the menu

Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

Wondering if there is any way that the JPopupMenu can be set so that if you click outside of the menu, it doesn't cancel (hide). I want it to stay until I tell it to disappear (or the frame is minimized).

Also, any way to remove the drop shadow that is visible on my Mac for the JPopupMenu? I've setOpaque(false) and setBorderPainted(false), but still see a drop shadow.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Second edit: This prevents the popup from closing if you click elsewhere in the JFrame (or more correctly, its content pane). Clicking a menu item hides the popup; if that too is undesired, see my blog post Keeping Menus Open.

I don't think there's any reasonable, LaF independent way to keep the popup open if the mouse is clicked outside the Java application or if the window is resized or minimized.
If you opt for this solution, you may need to iterate recursively over all components and set the client property.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Actually, on reading the requirement again, it looks more like a JOptionPane with a JList or JCombobox would be a better choice than a menu.

Could you describe the situation in which you thought this behavior would solve a problem? Not the why and the how, but the what.
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

I'm trying to overcome some heavyweight / lightweight issues with regard to OpenGL. I'm trying to display a JEditorPane on top of an OpenGL scene. I've been able to do this pretty well by placing the JScrollPane inside of a JPopupMenu, but the problem is if I click on the scene behind the JEditorPane (PopupMenu), I don't want it to disappear (cancel). I also don't want it to disappear if you click on it (this hasn't been a problem). I only want it to disappear if I call setVisible(false) or if the frame is minimized (automatic). It's a fullscreen app, so not worried about outside frame clicks. So I'm not really using it as a button or menu, but as a way to overcome other issues. Here is my code thus far, using Processing as the IDE.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Use a JWindow, not a JPopupMenu. Or an undecorated JFream or modeless JDialog.
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

A JFrame and JDialog drop behind the OpenGL scene (not sure about JWindow - haven't got that working yet) if the main frame is Always on Top. So you then have to do some AlwaysOnTop trick to make sure it doesn't fall behind. You loose keyboard control to the main scene and clicking on the main scene changes the focus. I don't have these problems with the PopupMenu. I just need to prevent it from closing itself if I click outside of the menu. I'll keep testing with the other options though..
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

I would have thought a JWindow parented to the JFrame would always stay on top of it, but I haven't experience with mixing heavy/light components.

I also thought the heavy/light mixing issue was resolved in Java 6u10 -- are you using an earlier version?
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

Using a current version of Java... doing some testing now and looks like JDialog is working correctly. I'm going to try JWindow as well and let you know. Perhaps some progress. :-)
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

Darryl, Thanks so much.. JDialog is working great for this. I must have tested it in a prior version of Java or something, but it's working good now. JavaRanch rocks :-)
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

Running into a problem... while this dialog screen is up, I make a call to a FileDialog. It uses a javax.swing.SwingUtilities.invokeAndWait(). With the JDialog, it no longer works. I've tried to hide the JDialog prior to running the FileDialog, but no go. Any tricks here? I've tried an invokeLater() but this won't stay on top of the screen.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

What happens if you specify the JDialog as the parent of the FileDialog?
Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

Specifying java.awt.Dialog.ModalityType.MODELESS helped it pop through, but it locks up afterward if I click to save more than once.
Specifying the JDialog as the parent had mixed results, sometimes it would show and sometimes not.. always locking up if I clicked the button more than once.
Update: MODELESS is also mixed, sometimes it locks up on the first click without the dialog. Even tried both.. MODELESS and JDialog.. same.


Jeff Gent
Ranch Hand

Joined: Mar 24, 2011
Posts: 44

I restarted my IDE and now it seems to be working... Uggg.. ok, it's late. Thank you so much for you help. I'll let you know what I find out tomorrow.
Update: now it's not working again.. this thing is so odd.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prevent JPopupMenu from hiding when clicked outside the menu