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...

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE Customers exploring the field of business intelligence for the first time often lead with: What is the difference between a data warehouse and a data mart? The next question follows as predictably as night follows day: which one does my company need? Let me start by saying that the two terms are often confused. Indeed, some people in the industry use them virtually interchangeably, which is unfortunate, because they do reflect a valuable hierarchical difference. The Data Warehouse A "data warehouse" will typically contain the full range of business intelligence available to a company from all sources. That data consists of transaction-processing records, corporate and marketing data, and other business operations information; for example, a bank might include loans, credit card statements, and demand deposits data, along with basic customer information. This internal data is frequently combined with statistica...