Home Segments Top Top Previous Next

702: Mainline

Rather than define a listener class, ApplicationClosingWindowListener, as in Segment 685, you can define LocalWindowListener as an inner class inside your definition of the MovieApplication class. The two class definitions differ only in that one appears inside the MovieApplication class.

Then, you can alter the constructor of the MovieApplication class defined in Segment 694 to create a LocalWindowListener instance, and you can connect that instance to the movie application frame:

import javax.swing.*;
import java.awt.event.*;
public class MovieApplication extends JFrame {
 public static void main (String argv []) {
  new MovieApplication("Movie Application");
 }
 public MovieApplication(String title) {
  super(title);
  setSize(300, 100);
  addWindowListener(new LocalWindowListener());                 
  show();
 }
 // Define window adapter                                       
 private class LocalWindowListener extends WindowAdapter {      
  public void windowClosing(WindowEvent e) {                    
   System.exit(0);                                              
  }                                                             
 }                                                              
}