Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-11
4
Medium Priority
?
764 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 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