Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:19:33+00:00 2026-05-23T11:19:33+00:00

I have a Swing app that has a JDialog pop up and ask for

  • 0

I have a Swing app that has a JDialog pop up and ask for a username and password. I thought it’d be good to have the keyboard focus already in the username field, but everything I’ve tried so far doesn’t work (even though one solution I tried works for a different text field in the program), so….I need some help. Here’s my code:

//JTextField usernameField = ...
JDialog dialog = pane.createDialog("Password:");
dialog.setVisible(true);


//Take 1
usernameField.requestFocusInWindow();

//Take 2
dialog.addWindowFocusListener(new WindowAdapter() {
        public void windowGainedFocus(WindowEvent e) {
            usernameField.requestFocusInWindow();
        }
});

//Take 3 - This is what I used elsewhere quite successfully
dialog.addComponentListener(new ComponentAdapter() {
    public void componentShown(ComponentEvent e ) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                usernameField.requestFocusInWindow();
            }
        });
    }
});

For what it’s worth, this is with Linux / X11 / Openbox. And when I use GTK, I have to press Tab once to select the appropriate field, but when I use Metal, I have to press it twice.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:19:34+00:00Added an answer on May 23, 2026 at 11:19 am

    See Dialog Focus for tips & strategies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Swing app, I have a screen that has a bunch of JTextFields.
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I'm a newbie to swing development. I have a swing app that needs to
I'm maintaining this Swing app that has a print option. Users need to be
I have a Java Swing UI that isn't updating/repainting as I thought it should.
I have a small Java desktop app that uses Swing. There is a data
I have a simple c/c++ app that has an optional TCL interpreter with the
I have a java swing App that connects to a remote Unix box using
We have a Swing app that processes relatively large amounts of data. For instance
I have a Swing app, that I start using JNLP. When I start using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.