Mega Code Archive

 
Categories / Java Tutorial / Swing Event
 

Making a Window Handle its Own Events

import java.awt.event.WindowEvent; import javax.swing.JFrame; public class FrameHandlingEvents extends JFrame {   public FrameHandlingEvents(String title) {     setTitle(title);      // setDefaultCloseOperation(EXIT_ON_CLOSE);     enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);   }   protected void processWindowEvent(WindowEvent e) {     if (e.getID() == WindowEvent.WINDOW_CLOSING) {       System.out.println(WindowEvent.WINDOW_CLOSING);       dispose();        System.exit(0);     }     super.processWindowEvent(e); // Pass on the event   }   public static void main(String[] a) {     FrameHandlingEvents window = new FrameHandlingEvents("Sketcher");     window.setBounds(30, 30, 300, 300);     window.setVisible(true);   } }