2

Capture events on a window

  1. import java.awt.Frame;
  2. import java.awt.event.*;
  3.  
  4. public class SimpleWindow1 {
  5.  
  6.     static ApplicationWindow appwin;
  7.  
  8.     public static void main(String[] args) {
  9.         appwin = new ApplicationWindow("Application");
  10.         appwin.addWindowListener(appwin);
  11.         appwin.setBounds(100, 100, 300, 200);
  12.         appwin.setVisible(true);
  13.     }
  14. }
  15.  
  16. @SuppressWarnings("serial")
  17. class ApplicationWindow extends Frame implements WindowListener {
  18.     ApplicationWindow(String s) {
  19.         super(s);
  20.     }
  21.  
  22.     public void windowClosing(WindowEvent e) {
  23.         System.exit(0);
  24.     }
  25.  
  26.     public void windowActivated(WindowEvent e) {
  27.         System.out.println("Activated");
  28.     }
  29.  
  30.     public void windowClosed(WindowEvent e) {
  31.         System.out.println("Closed");
  32.     }
  33.  
  34.     public void windowDeactivated(WindowEvent e) {
  35.         System.out.println("Deactivated");
  36.     }
  37.  
  38.     public void windowDeiconified(WindowEvent e) {
  39.         System.out.println("Deiconified");
  40.     }
  41.  
  42.     public void windowIconified(WindowEvent e) {
  43.         System.out.println("Iconified");
  44.     }
  45.  
  46.     public void windowOpened(WindowEvent e) {
  47.         System.out.println("Opened");
  48.     }
  49. }

Compile and run the program then click outside and inside the window, minimize it and restore it. All these events are captured and traced by the code. To quit the program, choose Close from the menu of the window manager.

$ javac SimpleWindow1.java
$ java SimpleWindow1
Opened
Activated
Deactivated
Activated
Deactivated
Iconified
Deiconified
Activated
Deactivated
Activated

  1. import java.awt.Frame;
  2. import java.awt.event.*;
  3.  
  4. public class SimpleWindow2 implements WindowListener {
  5.  
  6.     static Frame appwin;
  7.  
  8.     public static void main(String[] args) {
  9.         appwin = new Frame("Application");
  10.         appwin.addWindowListener(new SimpleWindow2());
  11.         appwin.setBounds(100, 100, 300, 200);
  12.         appwin.setVisible(true);
  13.     }
  14.  
  15.     public void windowClosing(WindowEvent e) {
  16.         System.exit(0);
  17.     }
  18.  
  19.     public void windowActivated(WindowEvent e) {
  20.         System.out.println("Activated");
  21.     }
  22.  
  23.     public void windowClosed(WindowEvent e) {
  24.         System.out.println("Closed");
  25.     }
  26.  
  27.     public void windowDeactivated(WindowEvent e) {
  28.         System.out.println("Deactivated");
  29.     }
  30.  
  31.     public void windowDeiconified(WindowEvent e) {
  32.         System.out.println("Deiconified");
  33.     }
  34.  
  35.     public void windowIconified(WindowEvent e) {
  36.         System.out.println("Iconified");
  37.     }
  38.  
  39.     public void windowOpened(WindowEvent e) {
  40.         System.out.println("Opened");
  41.     }
  42. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].