This is a Java program to demonstrate the WindowAdapter class to close the frame.
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.
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.
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.
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.
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"
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.
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.
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.
/* Java Program to demonstrate the WindowAdapter Class */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Window_Adapter extends WindowAdapter
{
static JFrame frame;
//Driver function
public static void main(String args[])
{
//Create a frame
frame=new JFrame("Window Adapter Class");
frame.setBackground(Color.white);
frame.setSize(500,500);
//Create an object of the class
Window_Adapter obj=new Window_Adapter();
//Associate WindowListener with frame
frame.addWindowListener(obj);
frame.setVisible(true);
}
//function to display status as closing when the frame is closed
@Override
public void windowClosing(WindowEvent e)
{
System.out.println("Status of frame : Closing");
windowClosed(e);
}
//function to close the frame
@Override
public void windowClosed(WindowEvent e)
{
frame.dispose();
}
//function to display status as iconified when the frame is minimized
@Override
public void windowIconified(WindowEvent e)
{
System.out.println("Status of frame : Iconified");
}
//function to display status as deiconified when the frame is maximized
@Override
public void windowDeiconified(WindowEvent e)
{
System.out.println("Status of frame : Deiconfied");
}
//function to display status as activated when the frame is active
@Override
public void windowActivated(WindowEvent e)
{
System.out.println("Status of frame : Activated");
}
//function to display status as deactivated when the frame is inactive
@Override
public void windowDeactivated(WindowEvent e)
{
System.out.println("Status of frame : Deactivated");
}
//function to display status as opened when the frame is created
@Override
public void windowOpened(WindowEvent e)
{
System.out.println("Status of frame : Opened");
}
}
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.
Here’s the run time test cases for WindowAdapter Class.
Test case 1 – Here’s the runtime output of the windowOpened function.
Test case 2 – Here’s the runtime output of the windowDeactivated function.
Test case 3 – Here’s the runtime output of the windowActivated function.
Test case 4 – Here’s the runtime output of the windowIconified function.
Test case 5 – Here’s the runtime output of the windowDeiconified function.
Test case 6 -Here’s the runtime output of the windowClosing function.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Programming Books
- Apply for Java Internship
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Java Books