Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPopupMenus

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers

Joe

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
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards

Joe Lemmer
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic