How do I make a JTextField get the focus initially in a JPanel shown using JOptionPane.showConfirmDialog ?

Posted on 2011-02-11
Last Modified: 2013-12-29
I have a JTextField on a JPanel.  I am using the JPanel in JOptionPane.showConfirmDialog(myPanel, ....)

The JTextField is on myPanel which uses a Border Layout.  How can I make the JTextField have the focus when I show the JOptionPane?

Normally I would use jTextField.requestFocus(true);

Question by:gdkinney_2
LVL 47

Expert Comment

ID: 34874298

I think txtfield.requestFocusInWindow() worked for me

JOptionPane will probably pop up modal, so you need it make not modal if
you care about focus in another window

   JOptionPane pane = new JOptionPane(message);
        JDialog dialog = pane.createDialog(this, title);

This focus thing is very sensitive, you need to test


Accepted Solution

aciuica earned 250 total points
ID: 34874716
Or if you want to keep it modal you can try something like that:
JOptionPane pane = new JOptionPane(myPanel);
JDialog dialog = pane.createDialog(null, "Title");
dialog.addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
LVL 92

Assisted Solution

objects earned 250 total points
ID: 34875665
> Normally I would use jTextField.requestFocus(true);

you should avoid using requestFocus()

Expert Comment

ID: 35018299
This may be overkill depending on what you are trying to do, but you can define your own FocusTraversalPolicy for the window.  Then when you impolement the getFirstComponenet(Container) method (and optionally, the getDefaultComponenet(Container)).  This will also allow you to define a tab order.  

Here's the link to Java's tutorial - its pretty simple to implement: 

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question