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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:54:03+00:00 2026-05-26T21:54:03+00:00

On click, How to close only one Frame not both or whole application? (I

  • 0

On click, How to close only one Frame not both or whole application?
(I have also tried with AWT Event dispatch, EDT)

package test;

import java.awt.*;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;

public class Test11 extends Frame implements MouseListener
{
  public static Frame gp;  
  public Test11()
  { 
    try {            
       this.setLayout (new BorderLayout ());
       Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
       this.setBounds(screen.width-400,33,400, 400);   
       this.setBackground(Color.red);
       this.addWindowListener(new WindowAdapter()
       {
         public void windowClosing(WindowEvent we) 
         {
           System.exit(0);
         }
       });       
       this.addMouseListener(this);       
       this.setVisible(true);
    } finally {
    }    
  }

  /* How do i do AWT Event Dispatch (EDT): to cloase AWT window? */
  public static void main(String[] args) throws InterruptedException, InvocationTargetException 
  {
    /* EDT: AWT Event Dispatch
    EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    eventQueue.push(new MyEventQueue()); */

    /* Simple close */
    EventQueue.invokeAndWait(new Runnable() 
    {
      public void run() 
      {
        System.out.println("Run: Window 1");
        gp = new Test11();
        gp.setVisible(true);      
        //gp.setVisible(false);
      }
    });

    /* Simple close */
    EventQueue.invokeAndWait(new Runnable() 
    {
      public void run() 
      {
        System.out.println("Run: Window 2");
        new Test11().setVisible(true);
      }
    });

  }

  @Override
  public void mouseClicked(MouseEvent me) 
  {
    System.out.println("Clicked: out of Window1 or Window2, close only any one not whole application");
    System.exit(0);
  }

  @Override
  public void mousePressed(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void mouseReleased(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void mouseEntered(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void mouseExited(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /* EDT: Extended class
  private static class MyEventQueue extends EventQueue 
  {
    public void postEvent(AWTEvent theEvent) 
    {
      System.out.println("Event Posted"); 
      super.postEvent(theEvent);
    }
  }
  */  
}

Follow up:

import java.awt.*;
import java.awt.event.*;

public class Test11 extends Frame 
{
    public static Frame window1;
    public static Frame window2;

    public Test11(String title) {
        super(title);
        setSize(400, 400);
        setBackground(Color.red);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.out.println(
                    getTitle() +
                    " says Bye-Bye!  " +
                    new java.util.Date());
                dispose();
            }
        });
        setLocationByPlatform(true);
    }

    public static void main(String[] args) {
        /* AFAIU starting the GUI on the EDT only applies to Swing.*/
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                System.out.println("Run: Window 1");
                window1 = new Test11("Window 1");
                window1.setVisible(true);

                System.out.println("Run: Window 2");
                window2 = new Test11("Window 2"); 
                window2.setVisible(true);
            }
        });

        /* Remotely: i need to close thi window, not manually */
        window2.setVisible(false);
        /* failed then try */
        window2.dispose();

        /* Now: I should have only Window1, but that i am not able to make yet */
    }
}
  • 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-26T21:54:03+00:00Added an answer on May 26, 2026 at 9:54 pm
    import java.awt.*;
    import java.awt.event.*;
    
    public class Test11 extends Frame {
    
        public Test11(String title) {
            super(title);
            setSize(400, 400);
            setBackground(Color.red);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    System.out.println(
                        getTitle() +
                        " says Bye-Bye!  " +
                        new java.util.Date());
                    dispose();
                }
            });
            setLocationByPlatform(true);
        }
    
        public static void main(String[] args) {
            /* AFAIU starting the GUI on the EDT only applies to Swing.*/
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("Run: Window 1");
                    (new Test11("Window 1")).setVisible(true);
                    System.out.println("Run: Window 2");
                    (new Test11("Window 2")).setVisible(true);
                }
            });
        }
    }
    

    Typical Output

    Run: Window 1
    Run: Window 2
    Window 1 says Bye-Bye!  Mon Nov 14 10:20:25 EST 2011
    Window 2 says Bye-Bye!  Mon Nov 14 10:20:35 EST 2011
    Press any key to continue . . .
    

    Update 1

    This code closes “Window 2” programatically. The problem with your version was ‘timing’, caused by the call to invoke later (what do you think that means?). It can be fixed in one of two relatively easy ways.

    1. The kludge. Add a Swing Timer/ActionListener set to go off 2 seconds after the main runs. For that route, take out the ‘comment part’ of all the commented code lines in the main.
    2. The better solution. Remove the call to EventQueue.invokeLater(), which is irrelevant to AWT components.

    Here is the altered code:

    import java.awt.*;
    import java.awt.event.*;
    // since the OP has not taken the time to explain 'why AWT',
    // I choose to make life easy by using a Swing class.
    import javax.swing.Timer;
    
    public class Test11 extends Frame
    {
        public static Frame window1;
        public static Frame window2;
    
        public Test11(String title) {
            super(title);
            setSize(400, 400);
            setBackground(Color.red);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    System.out.println(
                        getTitle() +
                        " says Bye-Bye!  " +
                        new java.util.Date());
                    dispose();
                }
            });
            setLocationByPlatform(true);
        }
    
        public static void main(String[] args) {
            // AFAIU starting the GUI on the EDT only applies to Swing.
            //EventQueue.invokeLater(new Runnable() {
            //    public void run() {
                    System.out.println("Run: Window 1");
                    window1 = new Test11("Window 1");
                    window1.setVisible(true);
    
                    System.out.println("Run: Window 2");
                    window2 = new Test11("Window 2");
                    window2.setVisible(true);
            //    }
            //});
    
            //ActionListener closeWindow = new ActionListener(){
            //  public void actionPerformed(ActionEvent ae) {
                    System.out.println(
                        window2.getTitle() +
                        " says Bye-Bye!  " +
                        new java.util.Date());
                    /* failed then try */
                    window2.dispose();
            //  }
            //};
            //Timer timer = new Timer(2000,closeWindow);
            //timer.setRepeats(false);
            //timer.start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an slider animation but on clX.click event #close div hides before it
I have a function and it works when you click close: it slides up.
I'd like to have a a div show only when one button is clicked
Does Dreamweaver CS 3 have a JavaScript debugger? The only information on anything close
I have 12 dialogs on one page. I only want them to open if
$(a.close).click(function() { var id = $(this).attr(id); alert(id); $(this).parents(div.venue:first).fadeOut(Fast); return false; }); Any ideas why
This is the file I'm using $.load() to load into DOM: <script type=text/javascript> $('.close').click(function()
I want to close the dialog box when you click outside of the dialog,
In order to get my setup a bit closer to one click deployment, I
if i run Server App. Exception occurs: on Dinle.Start() System.Net.SocketException - Only one usage

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.