• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPopupMenu disappear after mouse exits...

 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get the JPopupMenu to disappear after the mouseExits and after a wait of 3 seconds if the mouse does not come back in. However it's not working as I planned. The JPopupMenu has a few JMenuItem's on them which are all set to setOpaque(false). And therein lies the issue. If I set the menuitems to setOpaque(true) than my code works fine... however its only when they are setOpaque(false) that the JPopupMenu will disappear after 3 regardless of if the mouse is over them or not... I over rode JPopupMenu and added a MouseListener to get if the mouse enters or exits... here is the code for the mouseListener methods which is the only thing I over rode ( besides paintComponent but that does not apply here)..

subclassed JPopupMenu MouseListener Methods ... also implements Runnable to get a timer going..


here is the JPopupMenu code


any ideas?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the mouse exited is being recieved by the JPopupMenu and not the JMenuItem?
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh you were correct. I simply removed the MouseListener and the MouseMotionListener from each of the JMenuItem's and it worked perfectly.

here is the code I added to each of the JMenuItem's...
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My solution has presented a problem and I can't seem to get around it. With my previous solution applied the JMenuItem's no longer do anything when the user clicks them... this makes sense because there is no mouseListener to get a click. So I tried to add my own MouseListener after I took the previous one off.... All this did was bring me back to my original problem and the JMenuItem's acting as I've never touched them.. here is my code which does not work...



Is there some type of something I should be setting, or has anyone done something like this before?
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also tried consuming the MouseEvent's but that did not work either... It seems that even though i'm adding a new MouseListener, somehow the JMenuItem's is getting a MouseExited event which is closing my JPopupMenu... is there a way to get around this, and yes I've tried consuming the exit event.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I've solved the problem.. I consumed only the MouseExit event which was the problem... but the bigger problem came from the sprinkling of seperator's that i had within the popupmenu. It seems that everytime the mouse rolled over these seperator's it was sending a MouseExited event... which seems odd to me... but consuming the MouseEvent and simply drawing my own seperator works in case anyone else has the same problem.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I so totally lied about all of that... It seemed to have worked for a minute or so exactly how it was suppose to but now it does not.... any ideas anyone?
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have narrowed down the problem... The JMenuItem's seem to be giving the underlying JPopupMenu a mouseExited event whenever the mouse leaves the particular JMenuItem. Consuming the appropriate events does not work. Removing the MouseListener and MouseMotionListener solved the problem but than and the JMenuItem's are no longer clickable. Adding my own MouseListener and/or MouseMotionListener simply reverts back to my original problem. So I guess the question is, is there a way to stop the JMenuItem from giving the mouseExited to the JPopupMenu?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a thought...

attack it from the opposite side, instead of trying to find out when it exits the popup,
include in the mouseListener code that triggers the popup a mouseEntered.

theory is, when exiting the popup, a mouseEntered event should fire for the underlying panel/container/whatever,
and the mouseEntered code would firstly check for the popup's visibility
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Michael: I believe what your saying is what I did in my first or second post here. The JPopupMenu implements Runnable so that when the mouse leaves it triggers a thread to wait 3 seconds than set it to setVisible(false). If the mouse enters again than I interrupt the counting thread and start all over.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying to try it without any listeners added to the popup,
instead add the listener to the panel/table/whatever that triggers the popup showing.

when leaving the popup, a mouseEntered could then be detected in the 'panel/table/whatever'

but, having thought about it a bit more, it's going to be as messy as what you're trying to do now,
1) the popup would have to be totally within the bounds of the 'panel/table/whatever'
2) the timer would have to get the location of the mousePointer to see if it's over the popup.

if you posted a simple compilable program, you might get more people looking at it
 
Tim Chittka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have a solution.

The JPopupMenu isn't the right component to add the MouseListener too. Think about the items. They are in front of it. So the mousepointer isn't in the JPopupMenu but in the items.

Because of that I add the MouseListener to every added JMenuItem. After every exited/enter-pair of events I check if there is an armed item. If not, the mousepointer should be outside of the JPopupMenu. I found one exception: The mousepointer is above an item. I press the arrow key up or down to change the selected item. Now I can left the popupmenu with the mousepointer to the side of that unselected item. The selected item won't be changed until I move the mousepointer to another item. But if the user decides using the keyboard to select an item I think hiding the popupmenu automatically isn't what he expects.



I hope I could help you.

Best regards
Tim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!