Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Swing JFrame

Part:   1   2  3 

This chapter discusses the following problems:

  • How to close frame and terminate application.
  • How to interrupting Frame threads.
  • How to display Chinese characters in frame title.
  • How to draw graphics in frames.
  • How to draw Chinese characters in frames.

Closing Frame and Terminating Application

Problem: I have a frame window displayed on the screen and I want to close the frame and terminate the application, when user invokes the close command from the window's system menu, or clicks on the close icon from the window's system icon list.

Solution 1: You can use the JFrame.setDefaultCloseOperation() method to change the default behavior option of JFrame responding to the window closing event. Select the EXIT_ON_CLOSE option will terminate the application immediately. Of course, when the application is terminated, all frames will be closed automatically. The following sample code, JFrameClose1.java, shows you how to do this.

/**
 * JFrameClose1.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import javax.swing.*;
public class JFrameClose1 {
   public static void main(String[] a) {
      JFrame f = new JFrame();
      f.setTitle("Closing Frame with Default Close Operation");
      f.setBounds(100,50,500,300);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
   }
}

Solution 2: You can extend JFrame class to have your own frame class, so that you can override the default processWindowEvent() method to terminate the application. Of course, calling System.exit(0) is the quickest way to terminate an application. The following sample code, JFrameClose2.java, shows you how to do this.

/**
 * JFrameClose2.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.event.*;
import javax.swing.*;
public class JFrameClose2  {
   public static void main(String[] a) {
      MyJFrame f = new MyJFrame();
      f.setTitle("Closing Frame with Process Window Event");
      f.setBounds(100,50,500,300);
      f.setVisible(true);
   }
   static class MyJFrame extends JFrame {
      protected void processWindowEvent(WindowEvent e) {
         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
         }
      }
   }
}

Solution 3: You can create a new window event listener class and add an object of this listener class to the JFrame object. In the new window event listener, you can implement your owner version of windowClosing() handler method. Of course, the quickest way to create a new window event listener class is to extend the WindowAdapter class. The following sample code, JFrameClose3.java, shows you how to do this.

/**
 * JFrameClose3.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.event.*;
import javax.swing.*;
public class JFrameClose3  {
   public static void main(String[] a) {
      JFrame f = new JFrame();
      f.setTitle("Closing Frame with Window Listener");
      f.setBounds(100,50,500,300);
      f.addWindowListener(new MyWindowListener());
      f.setVisible(true);
   }
   static class MyWindowListener extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }
   }
}

(Continued on next part...)

Part:   1   2  3 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Swing JFrame