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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:46:26+00:00 2026-06-14T10:46:26+00:00

I have a fairly basic JFrame here, and I want to make the window

  • 0

I have a fairly basic JFrame here, and I want to make the window close automatically when the user clicks outside it. Is it possible to make the window close when the user clicks outside it (by somehow detecting clicks outside the window?).

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

public class ExampleJFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("How can I make this window close when I click outside it?");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JLabel jlbempty = new JLabel("");
        jlbempty.setPreferredSize(new Dimension(200, 200));
        frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}
  • 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-14T10:46:27+00:00Added an answer on June 14, 2026 at 10:46 am

    Detecting a click outside the frame is difficult, as it can be a click on any other application to which java has no access.

    You could try with a FocusListener as shown below

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.WindowConstants;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    public class AutoCloseFrameDemo {
      public static void main( String[] args ) {
        final JFrame frame = new JFrame( "Test" );
        frame.addFocusListener( new FocusListener() {
          private boolean gained = false;
          @Override
          public void focusGained( FocusEvent e ) {
            gained = true;
          }
    
          @Override
          public void focusLost( FocusEvent e ) {
            if ( gained ){
              frame.dispose();
            }
          }
        } );
        frame.add( new JLabel( "testlabel" ) );
    
        frame.pack();
        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        frame.setVisible( true );
      }
    }
    

    This works for this simple use-case. Not yet sure what will happen if you put e.g. a JTextField in the frame and the textfield gets focus. If that causes the JFrame to loose focus as well, your application will rather be useless.

    Edit

    A bit more robust solution might be to attach a listener to the KeyboardFocusManager

    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.WindowConstants;
    import java.awt.BorderLayout;
    import java.awt.KeyboardFocusManager;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyVetoException;
    import java.beans.VetoableChangeListener;
    
    public class AutoCloseFrameDemo {
      public static void main( String[] args ) {
        final JFrame frame = new JFrame( "Test" );
    
        KeyboardFocusManager.getCurrentKeyboardFocusManager().
            addVetoableChangeListener( "focusedWindow",
                                       new VetoableChangeListener() {
                                         private boolean gained = false;
    
                                         @Override
                                         public void vetoableChange( PropertyChangeEvent evt ) throws PropertyVetoException {
                                           if ( evt.getNewValue() == frame ) {
                                             gained = true;
                                           }
                                           if ( gained && evt.getNewValue() != frame ) {
                                             frame.dispose();
                                           }
                                         }
                                       } );
    
        frame.add( new JTextField( 10 ), BorderLayout.NORTH );
        frame.add( new JTextField( 10 ), BorderLayout.SOUTH );
    
        frame.pack();
        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        frame.setVisible( true );
      }
    }
    

    This allows to switch focus between the different text fields in the frame.

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

Sidebar

Related Questions

I think I have a fairly basic question here. I'm not trying to waste
Hey I have a fairly basic question about regular expressions. I want to just
I have a fairly basic data.table in R, with 250k rows and 90 columns.
I have a fairly basic TCP server keeping track of a couple connections and
I am making a webapp. I have a fairly basic question about javascript performance.
I have a parent-child relationship setup that is fairly basic. The end result is
basic c++ question i'm fairly sure. if i have a base class with a
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects
I have a fairly basic SQL Server Reporting Services report that is using nested
I have a fairly basic dialog maker for jquery that works in 2 out

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.