Solved

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

Posted on 2011-02-11
4
750 Views
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);

Thanks
0
Comment
Question by:gdkinney_2
4 Comments
 
LVL 47

Expert Comment

by:for_yan
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);
        pane.setMessageType(JOptionPane.INFORMATION_MESSAGE);      
        JDialog dialog = pane.createDialog(this, title);
        dialog.setModal(false);
       dialog.show();

This focus thing is very sensitive, you need to test

0
 
LVL 5

Accepted Solution

by:
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) {
        textField.requestFocusInWindow();
    }
});
dialog.setVisible(true);
0
 
LVL 92

Assisted Solution

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

you should avoid using requestFocus()

http://helpdesk.objects.com.au/java/how-to-change-focus-to-a-specific-jtextfield
0
 
LVL 1

Expert Comment

by:Shura85
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:
http://download.oracle.com/javase/tutorial/uiswing/misc/focus.html 
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

770 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