Academic Java    Java Tutorial  >  Swing  >  WindowAdapter

WindowAdapter Example

Java WindowAdapter EXAMPLE output Java WindowAdapter EXAMPLE output The WindowAdapter is an abstract convenience class for receiving window events. The methods in the WindowAdapter class, there are 10 in all, are invoked appropriately for window events, but they do nothing.

It is therefore necessary to extend the class and overwrite those of the 10 methods with which the program will deal, as we have done here, with just four of them.
// WindowAdapter Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class WindowAdapterExample {

   public static void main(String[] args) {

      JFrame f = new JFrame();
      f.addWindowListener(new Listener());

      JLabel label = new JLabel("minimize the frame, see output",JLabel.CENTER);
      f.getContentPane().setBackground(Color.cyan);
      f.getContentPane().add(label);

      f.setSize(300,200);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);

   }
}

class Listener extends WindowAdapter {

   public void windowActivated(WindowEvent e) {
      System.out.println("Window Activated");
   }

   public void windowDeiconified(WindowEvent e) {
      System.out.println("Window Deiconified");
   }

   public void windowIconified(WindowEvent e) {
      System.out.println("Window Iconified");
   }

   public void windowOpened(WindowEvent e) {
      System.out.println("Window Opened");
   }

}