File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JPopupMenus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopupMenus" Watch "JPopupMenus" New topic


Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi everyone,

I have registered a JTextField to a KeyListener class that causes a JPopupMenu to appear when certain letters are typed into the textfield. I want to be able to just carry on typing if I don't want to choose any of the options on the menu, but at the moment I have to manually click the focus back into the textfield to make the popupmenu disappear.

So I want the popupmenu to disappear as soon as I make another keystroke.

Does anyone know a way I could do this?

I looked at the api for popupmenus and see there is a firepopupmenucancelled method. Does anyone know if the keylistener is still active while the popupmenu is up? (ie is it registering other keystokes but just not displaying anything, or is it just shut down for some reason while the popupmenu is displayed). Because if it is still active, then maybe the firepopupmenucancelled is the way forward. I'll give it a go, but would be grateful for any tips or revelatory info.



I have just put a println message in my keylistener class that prints out a message every time I touch a key. It wouldn't print out anything while the popup menus were up! Hmmm...
[ December 11, 2008: Message edited by: Joe Lemmer ]

Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

You could check out javax.swing.Popup and javax.swing.PopupFactory. A little example:

You can use a ListSelectionListener to notice clicks ("activation") of an item in the JList, and call popup.hide() when you need it.

How To Ask Questions How To Answer Questions
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Rob,

Thanks for your answer and v. helpful code. Just what I needed. I've checked out popupfactory and popup in the api as well.

Thanks again for your help.


Joe Lemmer
I agree. Here's the link:
subject: JPopupMenus
It's not a secret anymore!