It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Setting focus on JOptionPane text field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting focus on JOptionPane text field" Watch "Setting focus on JOptionPane text field" New topic

Setting focus on JOptionPane text field

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: Oct 13, 2005
Posts: 46429
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

    Joined: Jun 09, 2003
    Posts: 4632
    Romeo Son
    Ranch Hand

    Joined: Mar 12, 2007
    Posts: 92

    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.


    SCJP 1.4, SCJD, OCE EJBD 6
    Jennifer Sohl
    Ranch Hand

    Joined: Feb 28, 2001
    Posts: 455
    Thanks for the help! I really appreciate it!
    Luigi Plinge
    Ranch Hand

    Joined: Jan 06, 2011
    Posts: 441

    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)?

    Rob Camick
    Ranch Hand

    Joined: Jun 13, 2009
    Posts: 2454
    See Dialog Focus for an approach similiar to Michael's.
    Luigi Plinge
    Ranch Hand

    Joined: Jan 06, 2011
    Posts: 441

    Thanks Rob, that works perfectly.
    I agree. Here's the link:
    subject: Setting focus on JOptionPane text field
    It's not a secret anymore!