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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:13:56+00:00 2026-06-10T17:13:56+00:00

I have a JFrame that I want closed when the user clicks off of

  • 0

I have a JFrame that I want closed when the user clicks off of it. I have two JTextFields and a JButton (username, password, submit). When I give them all the FocusListener, anytime the user goes from one field to another the window closes. How can I allow the user to go from field to field and only close it if the user clicks anywhere OUT of the pop up window?

public class LoginForm {

    static JTextField userName;
    static JTextField password;
    static JButton submit;
    JFrame main;
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    UserSession session;

    public LoginForm(){

        Handler handle = new Handler();  //inner class
        LoginFormFocusListener fl = new LoginFormFocusListener();   //inner class

        main = new JFrame("Please Login");

        main.setUndecorated(true);
        main.setBounds((dim.width/2) - (500/2),(dim.height/2) - (150/2),500,150);
        main.setVisible(true);
        main.setAlwaysOnTop(true);
        main.setResizable(false);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        userName = new JTextField(10);
        password = new JTextField(10);
        main.setLayout(new GridLayout(0,1));

        JPanel panel = new JPanel();
        main.add(panel);
        panel.add(new JLabel("Username: "));
        panel.add(userName);
        panel.add(new JLabel("Password: "));
        panel.add(password);

        submit = new JButton("Submit");
        panel.add(submit);

        userName.addFocusListener(fl);
        password.addFocusListener(fl);
        submit.addFocusListener(fl);

        submit.addActionListener(handle);
    }
}

… (unimportant methods and “Handler” class omitted)

public class LoginFormFocusListener implements FocusListener{

    public void focusGained(FocusEvent fe) {
        System.out.println("focus gained...");
        System.out.println("click off of this window to close...");
    }

    public void focusLost(FocusEvent fe){
    System.out.println("focus lost...");
    WindowEvent winEvt = new WindowEvent(main, 0);
    winEvt.getWindow().dispose();
    }

}



//test
public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable(){  
        public void run(){  
        new LoginForm();  
    }  
    });  
}
  • 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-10T17:13:58+00:00Added an answer on June 10, 2026 at 5:13 pm

    Don’t use a FocusListener for this since these are for components that gain and lose the focus, not for top level windows. Perhaps use a WindowListener listening for the window is deactivated or iconified.

    For example:

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class WindowListenerFun {
       public static void main(String[] args) {
          JPanel panel = new JPanel();
          panel.add(new JTextField(10));
          panel.add(new JButton("button"));
          JFrame frame = new JFrame("Bad Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(panel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    
          frame.addWindowListener(new WindowAdapter() {
    
             @Override
             public void windowIconified(WindowEvent wEvt) {
                ((JFrame)wEvt.getSource()).dispose();
             }
    
             @Override
             public void windowDeactivated(WindowEvent wEvt) {
                ((JFrame)wEvt.getSource()).dispose();
             }
    
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with some a JTextField and a JButton. I want it
I have a JFrame with a JSplitPane that is OneTouchExpandable. I want to remember
I have a JTextArea on a JFrame and a JButton. When user types characters
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a program that creates a JFrame and makes it visible. Is there
I have an application that is works fine and the JFrame for it is
I have a JFrame with null layout and two one character JLabels on the
I have a window that has two layers: a static background and a foreground
So I currently have a transparent JFrame that you can click through, but I
I want to add two jPanels to a JFrame side by side. the two

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.