Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

757 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

23 Experts available now in Live!

Get 1:1 Help Now