Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting focus on JOptionPane text field

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have created a custom text field that I am using in a JOptionPane. When I run the program, the JOptionPane comes up and looks great, however the focus always goes to the first button. I want the focus to start on the text field. I've tried requestFocus(), but this doesn't seem to work. Is there something special that I need to do to get the focus on the text field. See my code below:


Thanks for any help!!
[ April 18, 2007: Message edited by: Jennifer Sohl ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50216
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know.

Went through the JOptionPane page in the API, and the tutorial link on it, and I wasn't convinced there was anything dircetly useful.

Look at this thread. Michael Dunn explained how to go through all the components of a JFrame and remove the close minimise and maximise buttons.

Suggest you try
  • Instantiate your Dialog (as in the tutorial link).
  • Go through all its components,
  • Find the component you wish to have focus,
  • Call its requestFocus method, and finish your search.
  • See whether that works.
  •  
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Romeo Son
    Ranch Hand
    Posts: 92
    Android Eclipse IDE Suse
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jennifer,

    I had the same problem. You don't need the requestFocusInWindow() method.
    Instead, try with JOptionPane.showOptionDialog instead of JOptionPane.showConfirmDialog. The method I suggest, has a last parameter initialValue. If you have a JTextField and you give a String to this last parameter, JOptionPane will initialize the JTextField with that string and it will have the focus. For the options parameter you can use Object[] options = { "OK", "CANCEL" };
    It worked nice for me, hope it will for you too.

    Regards,
    Romeo
     
    Jennifer Sohl
    Ranch Hand
    Posts: 455
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the help! I really appreciate it!
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    IntelliJ IDE Scala Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I have the same problem. I've tried Michael Dunn's approach above but it doesn't seem to work:Anything wrong here?

    I also tried Romeo Son's technique:
    ... incidentally the last field does not have to be a String - it can be null. However if I leave out the "options" Object[], I don't get focus.

    The problem with this second version is now, after entering text in the text field, pressing Enter doesn't automatically select OK.

    1) Any further ideas on how to get JOptionPane.showConfirmDialog (rather than showOptionDialog, which is more complex) to give focus to the text field?

    2) If I have to use showOptionDialog, how do I make Enter select OK, after entering text (as it does for showConfirmDialog)?

    Thanks
     
    Rob Camick
    Ranch Hand
    Posts: 2616
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    See Dialog Focus for an approach similiar to Michael's.
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    IntelliJ IDE Scala Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Rob, that works perfectly.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic