Java Program to Close the Frame using WindowAdapter Class

This is a Java program to demonstrate the WindowAdapter class to close the frame.

Problem Description

We have to write a program in Java such that it demonstrates the use of the WindowAdapter class to close the frame. The program should demonstrate various WindowAdapter class functions such as window opened function, window activated function, window deactivated function, window iconified function, window deiconified function and window closing function.

The WindowAdapter class is a class which implements the WindowListener interface.

Expected Input and Output

For WindowEvent class, we can have the following 6 different sets of input and output.

1. To test windowOpened: When the frame is opened after creation.

For example:

If the frame is created, that is when the program is first executed
then the expected output is
"Status of Frame : Activated
 Status of Frame : Opened"

2. To test windowDeactivated: When the frame is deactivated.

advertisement
advertisement

For example:

If the frame is deactivated
then the expected output is "Status of Frame : Deactivated"

3. To test windowActivated: When the frame is activated.

Note: Join free Sanfoundry classes at Telegram or Youtube

For example:

If the frame is activated
then the expected output is "Status of Frame : Activated"

4. To test windowIconified: When the frame is iconified.

For example:

If the frame is iconified, that is the frame is minimized
then the expected output is
"Status of Frame : Iconified
 Status of Frame : Deactivated"

5. To test windowDeiconified: When the frame is deiconified.

advertisement

For example:

If the frame is deiconified, that is the frame is maximized
then the expected output is
"Status of Frame : Deiconified
 Status of Frame : Activated"

6. To test windowClosing: When the frame is to be closed.
For example:

If the frame is being closed,
then the expected output is
"Status of Frame : Closing
 Status of Frame : Deactivated"
Problem Solution

1. The program uses the WindowAdapter class to close the frame. The WindowListener interface has seven member methods :
i) public void windowClosing(WindowEvent): This method prints the status of frame as “Closing”, when the frame is being closed.
ii) public void windowClosed(WindowEvent): This method closes the frame.
iii) public void windowIconified(WindowEvent): This method prints the status of frame as “Iconified”, when the frame in minimized.
iv) public void windowDeiconified(WindowEvent): This method prints the status of frame as “Deiconified”, when the frame in maximized.
v) public void windowActivated(WindowEvent): This method prints the status of frame as “Activated”, when the frame is active.
vi) public void windowDeactivated(WindowEvent): This method prints the status of frame as “Deactivated”, when the frame is not active.
vii) public void windowOpened(WindowEvent): This methods prints the status of frame as “Opened”, when the frame is displayed after creation.

advertisement

2. Create a class that inherits the WindowAdapter class and implements the WindowListener interface.
3. Create a frame and associate the WindowListener interface of WindowAdapter class with the frame.
4. Use the functions of WindowListener interface to print the necessary message.

Program/Source Code

Here is source code of the Java Program to handle the keyboard events. The program is successfully compiled and tested using BlueJ on Windows 10 and javac compiler on Fedora 30.

  1. /* Java Program to demonstrate the WindowAdapter Class */
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Window_Adapter extends WindowAdapter
  6. {
  7.     static JFrame frame;
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11.         //Create a frame
  12.         frame=new JFrame("Window Adapter Class");
  13.         frame.setBackground(Color.white);
  14.         frame.setSize(500,500);
  15.         //Create an object of the class
  16.         Window_Adapter obj=new Window_Adapter();
  17.         //Associate WindowListener with frame
  18.         frame.addWindowListener(obj);
  19.         frame.setVisible(true);
  20.     }
  21.     //function to display status as closing when the frame is closed
  22.     @Override
  23.     public void windowClosing(WindowEvent e)
  24.     {   
  25.         System.out.println("Status of frame : Closing");
  26.         windowClosed(e);
  27.     }
  28.     //function to close the frame
  29.     @Override
  30.     public void windowClosed(WindowEvent e)
  31.     {
  32.         frame.dispose();
  33.     }
  34.     //function to display status as iconified when the frame is minimized  
  35.     @Override
  36.     public void windowIconified(WindowEvent e)
  37.     {
  38.         System.out.println("Status of frame : Iconified");
  39.     }
  40.     //function to display status as deiconified when the frame is maximized
  41.     @Override
  42.     public void windowDeiconified(WindowEvent e)
  43.     {
  44.         System.out.println("Status of frame : Deiconfied");
  45.     }
  46.     //function to display status as activated when the frame is active
  47.     @Override
  48.     public void windowActivated(WindowEvent e)
  49.     {
  50.         System.out.println("Status of frame : Activated");
  51.     }
  52.     //function to display status as deactivated when the frame is inactive
  53.     @Override
  54.     public void windowDeactivated(WindowEvent e)
  55.     {
  56.         System.out.println("Status of frame : Deactivated");
  57.     }
  58.     //function to display status as opened when the frame is created
  59.     @Override
  60.     public void windowOpened(WindowEvent e)
  61.     {
  62.         System.out.println("Status of frame : Opened");
  63.     }
  64. }
Program Explanation

1. This program uses the WindowAdapter class to inherit the features of WindowListener interface.
2. A frame is created and WindowListener is associated with the frame.
3. The suitable functions are called by the interface depending on the activities of the frame.
a) When the frame is created, the windowOpened function is called.
b) When the frame is deactivated, the windowDeactivated function is called.
c) When the frame is activated, the windowActivated function is called.
d) When the frame is de-iconified, the windowDeiconified function is called.
e) When the frame is iconified, the windowIconified function is called.
f) When the frame is closing, the windowClosing function is called which further calls the windowClosed function.
g) The windowClosed function disposes off the frame.

Runtime Test Cases

Here’s the run time test cases for WindowAdapter Class.

Test case 1 – Here’s the runtime output of the windowOpened function.
java-program-window-adapter-opened

Test case 2 – Here’s the runtime output of the windowDeactivated function.
java-program-window-adapter-deactivated

Test case 3 – Here’s the runtime output of the windowActivated function.
java-program-window-adapter-activated-cursor-minimize

Test case 4 – Here’s the runtime output of the windowIconified function.
java-program-window-adapter-iconified

Test case 5 – Here’s the runtime output of the windowDeiconified function.
java-program-window-adapter-deiconified

Test case 6 -Here’s the runtime output of the windowClosing function.
java-program-window-adapter-closed

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.