Using WindowListener for AWT Frame

Adding WindowListener for AWT frame lets you to handle operations going with the frame.

import java.awt.*;
import java.awt.event.*;
class WindowAction extends Frame
{
public WindowAction()
{
// Set the frame properties
setTitle("Button with ActionListener Demo");
setSize(400,400);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
setVisible(true);
// Add window listener
addWindowListener(new WindowListener(){
public void windowOpened(WindowEvent we)
{
System.out.println("Window Opened");
}
public void windowClosing(WindowEvent we)
{
System.out.println("Window Closing");
System.exit(0);
}
public void windowClosed(WindowEvent we)
{
// Will not be printed
System.out.println("Window Closed");
}
public  void windowIconified(WindowEvent we)
{
setTitle("Iconified");
}
public  void windowDeiconified(WindowEvent we)
{
setTitle("Deiconified");
}
public void windowActivated(WindowEvent we)
{
System.out.println("Window Activated");
}
public void windowDeactivated(WindowEvent we)
{
System.out.println("Window Deactivated");
}
});
}
public static void main(String args[])
{
new WindowAction();
}
}
WindowAction() : Code illustrating WindowListener is written here
new WindowAction() : Create object for the class WindowAction

Next: Using FocusListener on AWT TextField
Previous: Using MouseWheelListener on AWT Frame



Posted by at on

Tags: AWT Event Handling,