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 8000267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:44:19+00:00 2026-06-04T15:44:19+00:00

When my application loads, which is made using netbeans, the first JTextField is automatically

  • 0

When my application loads, which is made using netbeans, the first JTextField is automatically focused, and in this JTextField, I have written “Enter your Username”, it will go away when the user clicks on this field, but when the application is loaded, this field is focused, means I can’t see “Enter your Username”, how to unfocus it on startup ?

  • 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-06-04T15:44:20+00:00Added an answer on June 4, 2026 at 3:44 pm

    A log-in would be best done in a modal dialog, but that introduces problems in that the method requestFocusInWindow() must be called after the component is visible, but that is blocked by the fact the dialog is modal!

    This example uses Rob Camick’s RequestFocusListener (as presented in Dialog Focus) to manage focus after the dialog is visible.

    Login with focused password field

    Note: That is how it appears before the user does anything. The password field is focused by default.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class LoginRequired {
    
        LoginRequired() {
            JFrame f = new JFrame("Login Required");
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            f.setResizable(false);
            f.setSize(400, 300); // not recommended, but used here for convenience
            f.setLocationByPlatform(true);
            f.setVisible(true);
    
            showLogin(f);
        }
    
        private void showLogin(JFrame frame) {
            JPanel p = new JPanel(new BorderLayout(5,5));
    
            JPanel labels = new JPanel(new GridLayout(0,1,2,2));
            labels.add(new JLabel("User Name", SwingConstants.TRAILING));
            labels.add(new JLabel("Password", SwingConstants.TRAILING));
            p.add(labels, BorderLayout.LINE_START);
    
            JPanel controls = new JPanel(new GridLayout(0,1,2,2));
            JTextField username = new JTextField("Joe Blogs");
            controls.add(username);
            JPasswordField password = new JPasswordField();
            password.addAncestorListener(new RequestFocusListener(false));
            controls.add(password);
            p.add(controls, BorderLayout.CENTER);
    
            JOptionPane.showMessageDialog(
                frame, p, "Log In", JOptionPane.QUESTION_MESSAGE);
            System.out.println("User Name: " + username.getText());
            System.out.println("Password: " + new String(password.getPassword()));
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                new LoginRequired();
            });
        }
    }
    
    /**
     *  Convenience class to request focus on a component.
     *
     *  When the component is added to a realized Window then component will
     *  request focus immediately, since the ancestorAdded event is fired
     *  immediately.
     *
     *  When the component is added to a non realized Window, then the focus
     *  request will be made once the window is realized, since the
     *  ancestorAdded event will not be fired until then.
     *
     *  Using the default constructor will cause the listener to be removed
     *  from the component once the AncestorEvent is generated. A second constructor
     *  allows you to specify a boolean value of false to prevent the
     *  AncestorListener from being removed when the event is generated. This will
     *  allow you to reuse the listener each time the event is generated.
     */
    class RequestFocusListener implements AncestorListener
    {
        private boolean removeListener;
    
        /*
         *  Convenience constructor. The listener is only used once and then it is
         *  removed from the component.
         */
        public RequestFocusListener()
        {
            this(true);
        }
    
        /*
         *  Constructor that controls whether this listen can be used once or
         *  multiple times.
         *
         *  @param removeListener when true this listener is only invoked once
         *                        otherwise it can be invoked multiple times.
         */
        public RequestFocusListener(boolean removeListener)
        {
            this.removeListener = removeListener;
        }
    
        @Override
        public void ancestorAdded(AncestorEvent e)
        {
            JComponent component = e.getComponent();
            component.requestFocusInWindow();
    
            if (removeListener)
                component.removeAncestorListener( this );
        }
    
        @Override
        public void ancestorMoved(AncestorEvent e) {}
    
        @Override
        public void ancestorRemoved(AncestorEvent e) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which loads up c# source files dynamically and runs them
I have a sample application which loads 2 records ro database and then fetches
I have a C# application which loads at startup, and logs data to a
I have made an application in javascript using HTML fields in asp.net, as asp
i have made an application in which i need to load the google maps.
I have an ExtJs application which loads quite a good number of javascript files.
My company produces a cross-platform server application which loads its configuration from user-editable configuration
I have a Java Application which has to load an DLL with a few
I have an application which takes time to load so i would like to
I have a application which use two layouts in portrait ans landscape. To load

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.