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?
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.
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.
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.
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?
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?
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.
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
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
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop