• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Focus in a JPopupMenu

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm having quite a bit of trouble getting the focus into a popup menu. Here's what I'm doing:

  • I created a JPopupMenu and added a JList as it's only component.
  • I created a JButton that when clicked on displays the popup menu.

  • What I want is for the popup menu to be displayed and the JList in the menu to have focus so the user can immediately just press the up and down arrows to go up and down through the items. The problem is that it doesn't have the focus. Instead you have to click once in the JList, then you can start moving up and down with the up and down arrows.
    I've tried everything I can think of. Calling list.requestFocus() in the button's actionEvent after the menu has been shown causes a NullPointerException and calling list.requestDefaultFocus() after the menu has been shown doesn't cause errors, but doesn't actually give the list focus.
    Here's my code, maybe you can tell me what I'm doing wrong (this is all just inside a main method in my test class):
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

Instead of using list.requestDefaultFocus() use

menu.requestDefaultFocus(). I tried out and is working ..
Regards,
Jagan Reddy.

 
Jeff SmithX
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I thought I'd tried everything, but apparently I hadn't... that worked great. :-)
Jeff
 
Jeff SmithX
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, this is very odd. The first time I ran it with that code everything worked fine, but now it's not??? After I closed the program, and ran it again the menu didn't have focus. And then, without closing the program (just the menu), I clicked the button again to get the menu up, and whenever the mouse is moved within the bounds of the menu, NullPointerException's are thrown until the list recieves focus by actually clicking in it? Do you experience this same problem, or is it just me?
Thanks,
Jeff
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic