JClass Elements

PreviousNextIndex

6

Exit Frame

Features of JCExitFrame  Properties  Methods and Constructors  Examples 


6.1 Features of JCExitFrame

A subclass of JFrame that listens for window close events and exits the application when the event is received, or hides the window so that it can be made visible later on. There is a JFrame constant in JavaTM2 v1.3 called EXIT_ON_CLOSE that performs the same function.

It is useful for applications containing a single frame. If you used the utility frames available in JClass 3.x versions of jclass.contrib, it is useful to know that this replaces DemoFrame and ContribFrame.


6.2 Properties

A JCExitFrame has the same properties as a JFrame, and one additional one:

exitOnClose

A Boolean property that determines whether the application should exit when the user closes the frame or when close() is called (default: true). If set to false, the frame is hidden; it can be made visible later.

Note: Compare this to using JFrame.EXIT_ON_CLOSE in JDK 1.3, which performs the same function.

For a full listing of the properties, please see Appendix A, Bean Properties Reference.


6.3 Methods and Constructors

Methods

JCExitFrame subclasses from JFrame, making it a JFrame with a built-in mechanism for catching window-closing events. The following methods report or control which action is taken when a window-closing event is received.

getExitOnClose()

Returns false if the window will be hidden rather than exiting when a window-closing event is received.

setExitOnClose()

A Boolean method that determines whether the application should exit when the user closes the frame or when close() is called (default: true). If set to false, the frame is hidden; it can be made visible later.

Constructors

There are two constructors. The default constructor provides an untitled frame while the other accepts a parameter which is used to set the frame's title.

JCExitFrame()

Default constructor.

JCExitFrame(String
  title)

The parameter provides a title for the frame.


6.4 Examples

Use a JCExitFrame as you would a JFrame, and manage window closing events using the exitOnClose property.

import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.klg.jclass.util.swing.*;

public class ExitFrameExample {

static String message0 = "Many JClass examples and demos use a JCExitFrame.";
static String message1 = "\n\nKeep in mind that you can hide a JCExitFrame \nrather than disposing of it entirely.";
static String message = message0 + message1;

static JTextArea messageArea = new JTextArea(message);

public static void main(String[] args){
String title = "A Basic Frame That Responds to Window-Closing Events";
JCExitFrame frame;

frame = new JCExitFrame(title);
frame.setSize(450, 100);
frame.setVisible(true);
frame.setExitOnClose(false); // Hide the window
// instead of closing it.



messageArea.setFont(new Font("Times-Roman", Font.BOLD, 14));
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
messageArea.setVisible(true);

}
}

Figure 29 :  A JCExitFrame containing a JTextArea.


PreviousNextIndex