Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

JOptionPane focus

Thread view: 
Ian Wilson - 27 Apr 2006 11:15 GMT
When I use JOptionPane's static methods to display some JTextFields,
initial focus is on one of the buttons, is it possible to set initial
focus to the JTextField?

example code:

import javax.swing.*;
import java.awt.*;

public class MyApp3 {

  public static JFrame frame;

  private static void createAndShowGUI() {
    JFrame frame = new JFrame("A test app");
    frame.setBounds(100,100,  300, 200);
    frame.setVisible(true);
    getPassword();
  }

  private static void getPassword() {
    JPanel panel = new JPanel();
    JTextField nameField = new JTextField(10);
    JPasswordField passwordField = new JPasswordField(10);
    panel.setLayout(new GridLayout(2,2, 5,10)); // rows,cols,vgap,hgap
    panel.add (new JLabel("Name", SwingConstants.RIGHT));
    panel.add (nameField);
    panel.add (new JLabel("Password", SwingConstants.RIGHT));
    panel.add (passwordField);
    int choice = JOptionPane.showConfirmDialog(
      MyApp3.frame,                  // parent
      panel,                         // "message"
      "Database Log-in",             // title
      JOptionPane.OK_CANCEL_OPTION,  // optiontype
      JOptionPane.PLAIN_MESSAGE      // messagetype: no icon
    );
    // do something with choice and credentials
  }
   
  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() { createAndShowGUI(); }
    });
  }

}
Michael Dunn - 27 Apr 2006 11:23 GMT
> When I use JOptionPane's static methods to display some JTextFields, initial focus is on one of
> the buttons, is it possible to set initial focus to the JTextField?
[quoted text clipped - 41 lines]
>
> }

    JPanel panel = new JPanel();
    final JTextField nameField = new JTextField(10);//<-----final
    javax.swing.SwingUtilities.invokeLater(new Runnable() {//<-----add
      public void run() { nameField.requestFocusInWindow(); }});//<----add
    JPasswordField passwordField = new JPasswordField(10);
Ian Wilson - 27 Apr 2006 11:53 GMT
> "Ian Wilson" wrote
>
>> When I use JOptionPane's static methods to display some JTextFields, initial focus is on one of
>> the buttons, is it possible to set initial focus to the JTextField?

<example code snipped for brevity>

>      JPanel panel = new JPanel();
>      final JTextField nameField = new JTextField(10);//<-----final
>      javax.swing.SwingUtilities.invokeLater(new Runnable() {//<-----add
>        public void run() { nameField.requestFocusInWindow(); }});//<----add
>      JPasswordField passwordField = new JPasswordField(10);

Thanks for the quick response Michael, that did the trick.

I can't say I understand this, (e.g. why 'final', how does invokeLater
tie in with the chronological sequence in which GUI objects appear and
are allocated focus? I'm surprised that the nameField.requestFocus isn't
superceded by something similar (e.g. OKButton.requestFocus) being done
chronologically later by the JOptionPane constructor. Is there any
recommended reading material that might help me understand it?
Michael Dunn - 27 Apr 2006 19:44 GMT
>>> When I use JOptionPane's static methods to display some JTextFields, initial focus is on one of
>>> the buttons, is it possible to set initial focus to the JTextField?
[quoted text clipped - 14 lines]
> done chronologically later by the JOptionPane constructor. Is there any recommended reading
> material that might help me understand it?

final is required because nameField is a local variable, and is accessed from an inner class

the api docs might explain invokeLater a bit better
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html#invokeLa
ter(java.lang.Runnable
)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.