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

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

C++ (Object and Class)

The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language. Object Oriented Programming is a paradigm that provides many concepts such as  inheritance, data binding, polymorphism etc. The programming paradigm where everything is represented as an object is known as truly object-oriented programming language.  Smalltalk  is considered as the first truly object-oriented programming language. OOPs (Object Oriented Programming System) Object  means a real word entity such as pen, chair, table etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation C++ Object In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an ent...