Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Focus in a JPopupMenu

 
Jeff Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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):
 
Jagan Reddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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 Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I thought I'd tried everything, but apparently I hadn't... that worked great. :-)
Jeff
 
Jeff Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic