Solved

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

Posted on 2011-02-11
4
746 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where to store the queries for modification of table 4 55
more than one jdk and one jre 1 34
expectj telnet failing 5 26
hibernate jars 4 9
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now