Swing Window Adapter



Swing Window Adapter

  1. WindowAdapter class is an abstract adapter class in Java Swing.
  2. WindowAdapter class is for receiving window events.
  3. Methods specified in WindowAdapter class are empty.
  4. WindowAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.WindowAdapter class –

public abstract class WindowAdapter
   extends Object
      implements WindowListener, 
                 WindowStateListener, 
                 WindowFocusListener

Class constructors

WindowAdapter()

Class methods

No. Method Return Type Parameter Invoked when
1 windowActivated() void WindowEvent window is activated.
2 windowClosed() void WindowEvent window has been closed.
3 windowClosing() void WindowEvent window is in the process of being closed.
4 windowDeactivated() void WindowEvent window is de-activated.
5 windowDeiconified() void WindowEvent window is de-iconified.
6 windowGainedFocus() void WindowEvent Window is set to be the focused Window
7 windowIconified() void WindowEvent window is iconified.
8 windowLostFocus() void WindowEvent Window is no longer the focused Window
9 windowOpened() void WindowEvent window has been opened.
10 windowStateChanged() void WindowEvent window state is changed.

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

WindowAdapter Example

AdapterExample.java

package com.c4learn;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showWindowApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);
      
      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });
      
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showWindowApapter() {
      headLabel.setText("WindowAdapter Example");

      final JFrame innerFrame = new JFrame();
      innerFrame.setSize(300, 200);
      innerFrame.setTitle("WindowAdapter Demo");
      
      innerFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            innerFrame.dispose();
         }
      });
      
      JLabel msglabel = new JLabel("Welcome to Swing", JLabel.CENTER);
      innerFrame.add(msglabel);
      innerFrame.setVisible(true);
   }
}

After ruuning program in the eclipse you will see below output

window_adapter