Java Swing Tutorial - Java WindowAdapter() Constructor








Syntax

WindowAdapter() constructor from WindowAdapter has the following syntax.

public WindowAdapter()

Example

In the following code shows how to use WindowAdapter.WindowAdapter() constructor.

/*  ww  w  .  j a  v a 2  s  . co m*/
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Main {
  JFrame window = new JFrame("Main");
  public Main() {
    window.setBounds(30, 30, 300, 300);
    window.addWindowListener(new WindowHandler());
    window.setVisible(true);
  }
  class WindowHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.out.println("closing");
      window.dispose(); // Release the window resources
      System.exit(0); // End the application
    }
  }
  public static void main(String[] args) {
    new Main();
  }
}