The class which process the window event needs to be implemented this interface and an object of this class should be registered with a component by using addWindowListener() method. Following is the syntax.
[java]public interface WindowListener
extends EventListener[/java]
Example
[java]package splessons;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingListenerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JFrame aboutFrame;
public SwingListenerDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingListenerDemo swingListenerDemo = new SwingListenerDemo();
swingListenerDemo.showWindowListenerDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("SPlessons - Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showWindowListenerDemo(){
headerLabel.setText("Listener in action: WindowListener");
JButton okButton = new JButton("OK");
aboutFrame = new JFrame();
aboutFrame.setSize(300,200);;
aboutFrame.setTitle("SPlessons - WindowListener Demo");
aboutFrame.addWindowListener(new CustomWindowListener());
JPanel panel = new JPanel();
panel.setBackground(Color.white);
JLabel msglabel
= new JLabel("Stop thinking start coding."
,JLabel.CENTER);
panel.add(msglabel);
aboutFrame.add(panel);
aboutFrame.setVisible(true);
}
class CustomWindowListener implements WindowListener {
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
aboutFrame.dispose();
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
}
}[/java]
Output
When compile the code following is the output will be generated.