Skip to main content

Close AWT Window

How to close AWT Window in Java

We can close the AWT Window or Frame by calling dispose() or System.exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.

The WindowAdapter class implements WindowListener interfaces. It provides the default implementation of all the 7 methods of WindowListener interface. To override the windowClosing() method, you can either use WindowAdapter class or WindowListener interface.

If you implement the WindowListener interface, you will be forced to override all the 7 methods of WindowListener interface. So it is better to use WindowAdapter class.

Different ways to override windowClosing() method

There are many ways to override windowClosing() method:

  • By anonymous class
  • By inheriting WindowAdapter class
  • By implementing WindowListener interface

Close AWT Window Example 1: Anonymous class

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame{  
  5.     WindowExample(){  
  6.         addWindowListener(new WindowAdapter(){  
  7.             public void windowClosing(WindowEvent e) {  
  8.                 dispose();  
  9.             }  
  10.         });  
  11.         setSize(400,400);  
  12.         setLayout(null);  
  13.         setVisible(true);  
  14.     }  
  15. public static void main(String[] args) {  
  16.     new WindowExample();  
  17. }  

Output:

java awt close window example 1

Close AWT Window Example 2: extending WindowAdapter

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class AdapterExample extends WindowAdapter{  
  4.     Frame f;  
  5.     AdapterExample(){  
  6.         f=new Frame();  
  7.         f.addWindowListener(this);  
  8.           
  9.         f.setSize(400,400);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.     }  
  13. public void windowClosing(WindowEvent e) {  
  14.     f.dispose();  
  15. }  
  16. public static void main(String[] args) {  
  17.     new AdapterExample();  
  18. }  
  19. }  

Close AWT Window Example 3: implementing WindowListener

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame implements WindowListener{  
  5.     WindowExample(){  
  6.         addWindowListener(this);  
  7.           
  8.         setSize(400,400);  
  9.         setLayout(null);  
  10.         setVisible(true);  
  11.     }  
  12.       
  13. public static void main(String[] args) {  
  14.     new WindowExample();  
  15. }  
  16. public void windowActivated(WindowEvent e) {}  
  17. public void windowClosed(WindowEvent e) {}  
  18. public void windowClosing(WindowEvent e) {  
  19.     dispose();  
  20. }  
  21. public void windowDeactivated(WindowEvent e) {}  
  22. public void windowDeiconified(WindowEvent e) {}  
  23. public void windowIconified(WindowEvent e) {}  
  24. public void windowOpened(WindowEvent arg0) {}  
  25. }  
Anurag Rana Educator CSE/IT

Comments

Post a Comment

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&