aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Mouse click getting lost after popping up a non-modal JPopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mouse click getting lost after popping up a non-modal JPopupMenu" Watch "Mouse click getting lost after popping up a non-modal JPopupMenu" New topic
Author

Mouse click getting lost after popping up a non-modal JPopupMenu

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I'm having a rather strange problem with a large, Swing-based application on which I'm currently working.
My next step is to try to reproduce the problem with a simple application, so I don't have that yet. I'm posting now because I just want to see if I'm missing something obvious.

The application has a main JFrame on which they've added many various components, including a menu bar of command buttons.

My change was to add a non-modal JPopupMenu-based dialog. The dialog provides Submit/Cancel buttons for an edit control that doesn't have any way to do those things otherwise.

After the JPopupMenu is shown (using show(component, x, y)) the application requests focus back in the original edit control, so the user can continue editing data there.

The problem is that once the JPopupMenu is shown, the very next mouse click goes to mouse click heaven (is ignored, or otherwise not seen). If I click on one of the command buttons (or any other component which should otherwise be able to accept focus), nothing happens.

Could the JPopupMenu be interacting badly with the Glass Pane or Layered Pane somehow?

I added a MouseListener to my JPopupMenu instance (as well as the JPanel I put in it) and it is not getting the mouse click events either. I can't tell where that first mouse click goes.

Like I said, I will be working on creating a simplified example to see if I can replicate the problem. In the meantime, does anyone have any ideas about what might be going wrong here?

Thanks,
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Frankly speaking I have trouble understanding what the problem is. If you can let us know what the expected behavior is it would help clarify things better.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I'm sorry it wasn't clear.

When the user sets focus in a text input field and begins typing, there is a small pop-up containing Save and Cancel buttons which is made visible. This pop-up is supposed to look like an extension of the input field. However, we want the pop-up to hide when the use clicks elsewhere in the application - for example, if they click on one of the command buttons along the top of the application.

However, the first mouse click after the pop-up is made visible is lost. As a result, the user has to click twice.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Still not clear (to me anyway).

To increase your chances of getting help you should create a SSCCE, that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
one thing to try is wrapping this request in a SwingUtilities.invokeLater
"After the JPopupMenu is shown (using show(component, x, y)) the application requests focus back in the original edit control"
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
That code is actually called from the Document Listener, so it's already in the event thread. Or is the point to make sure the rest of the event queue is flushed before requesting focus?

I'll try it, of course - I'm just curious what you're thinking.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
just to make sure it gets added at the end.

I've seen a number of problems (particularly focus) solved by using SwingUtilities.invokeLater

doesn't always work, but worth a try
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Thanks, I'll give it a try (I can't get at the code until Monday). By the way, focus is being given to the text input control, as desired, it's just that if I click on a command button on the tool bar, the first click is lost.

Thanks,
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
This morning I was able to try making the requestFocus() call from within an invokeLater() call, but this didn't help.

I did spend quite a bit of time over the weekend trying to create a simple reproducible case, but wasn't able to reproduce the problem. There must be something in the real application which I'm not accounting for.

Like I mentioned, I placed mouse listeners on the non-modal pop-up, but the missing click is not going there.
Can anyone help me figure out a way to determine where that first mouse click is going?

Thanks,
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Here is perhaps another piece of the puzzle. I need to show the pop-up using the JMenuPopup show(Component, x, y) method.
This is because the background of the pop-up needs to be transparent. If I use setVisible(true), the pop-up uses heavyweight components, and they don't support transparency.

However, for a test, I tried showing the pop-up using the setVisible(true) method, and although the background of the pop-up was not transparent, I no longer had the problem with the first click getting lost. Of course, I need the background of the JPopupMenu to be transparent, so (I believe) I need to use the show(Component, x, y) method to make the pop-up visible.

When I use show(Component, x, y), I pass the the parent of my text editor control (which is the JPanel used as the content pane).

Does this provide any explanation why the first mouse click is getting lost and what I might be able to do about it?

Thanks,
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I played around with this, and confirm that transparency with show(..) works OK, but not for setVisible(..),
but the funny thing is that if you look into show()'s source, it calls setVisible(..)

I added a button with mouseListener to remove the popup, and the clicks work first time every time.

I guess the only thing that can be done is to create a small app that works OK, then add/modify
things one at a time (to replicate main app), until you find the culprit.

[edit]
another difference between show/setVisible, without removing/hiding the popup
show: click somewhere the popup displays, click somewhere else and first one vanishes, new one shows i.e. only a single popup shows
setVisible: popup displays at every click i.e. multiple popups show
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Thanks for all your help. After working on this for a couple days, the company decided to go in a different direction, so we've scrapped that code If I make the pop-up visible using setLocation(x,y) then setVisible(true), it doesn't have the problem (although the background of the dialog can't be made transparent).

I believe there was something to the actual application which I was just not able to reproduce in a simple text application.

Anyway, I guess if they don't want me to work on it, I don't need to

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mouse click getting lost after popping up a non-modal JPopupMenu