Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

show and hide a JPopupMenu

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like it should be really simple but I just can't get it to work.

I want a JPopupMenu to appear when the user clicks on a button.

I then when the menu to disappear when the user clicks anywhere other than on the menu.

The only problem I've got is that I'd like the same to happen when the button is clicked a second time.

Logically, to me at least, the following should work:



However, even though I can see the menu, the code always indicates it is not showing and not visible - so the menu never hides again by clicking the button.

Am I missing something?
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to reinvent the wheel?
Check out the API docs for JComponent#setComponentPopupMenu and what it does.
 
Harold Lime
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm definitely missing something...

I've look into that, and run this code:
http://www.java2s.com/Code/JavaAPI/javax.swing/JComponentsetComponentPopupMenuJPopupMenupopup.htm

But I still can't figure out how to make it do what I want.

Maybe I'm using a JPopupMenu for something it isn't suitable for?

Basically I want a JButton that works in the same way a JMenuBar. 1 ckick shows the menu, 2nd click hides it again. Clicking anywhere other than the menu should hide the menu also.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've checked your code, and the problem is that the mouse press on the JButton hides the JPopupMenu, leading it to be shown again when the mouse is released.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Rob said. There were some concurrency issues to sort out, and there may be a better way to do this (I certainly hope so!) but this fulfils the requirements.
 
Harold Lime
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant.

Thanks for the help guys.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic