18
Swing II
18.1 Window Listeners 1002
Example: A Window Listener Inner Class 1004
The
dispose
Method 1008
The
WindowAdapter
Class 1009
18.2 Icons and Scroll Bars 1010
Icons 1010
Scroll Bars 1017
Example: Components with Changing Visibility 1023
18.3 The
Graphics
Class 1026
Coordinate System for Graphics Objects 1027
The Method
paint
and the Class
Graphics
1027
Drawing Ovals 1033
Drawing Arcs 1033
Rounded Rectangles
★
1037
paintComponent
for Panels 1038
Action Drawings and
repaint
1038
Some More Details on Updating a GUI
★
1044
18.4 Colors 1044
Specifying a Drawing Color 1045
Defining Colors 1046
The
JColorChooser
Dialog Window 1048
18.5 Fonts and the
drawString
Method 1051
The
drawString
Method 1051
Fonts 1054
Chapter Summary 1057
Answers to Self-Test Exercises 1058
Programming Projects 1061
SavitchJava.book Page 1001 Sunday, February 11, 2007 6:34 PM
18
18
Swing II
Window listeners?
I thought windows were for looking not for listening.
Student answer on an exam
Introduction
Although this is the third chapter on Swing, we have entitled it “Swing II” because
Chapter 17, entitled “Swing I,” and this chapter are really one unit. This chapter is a
continuation of Chapter 17, presenting more details about designing regular Swing
GUIs. Chapter 18 on applets is a side issue that may be read after this chapter if you
prefer.
Prerequisites
This chapter uses material from Chapter 17 (and its prerequisites).
Section 18.2 on icons and scroll bars is not used in subsequent sections and so may
be skipped or postponed.
18.1
Window Listeners
A man may see how this world goes with no eyes.
Look with thine ears. . . .
WILLIAM SHAKESPEARE,
King Lear
In Chapter 17 we used the method
setDefaultCloseOperation
to program the close-
window button in a
JFrame
. This allows for only a limited number of possibilities for what
happens when the close-window button is clicked. When the user clicks the close-window
button (or either of the two accompanying buttons), the
JFrame
fires an event known as a
window event
. A
JFrame
can use the method
setWindowListener
to register a
window
listener
to respond to such window events. A window listener can be programmed to
respond to a window event in any way you wish. Window events are objects of the class
WindowEvent
. A window listener is any class that satisfies the
WindowListener
interface.
The method headings in the
WindowListener
interface are given in Display 18.1. If a
class implements the
WindowListener
interface, it must have definitions for all seven of
these method headings. If you do not need all of these methods, then you can define the
ones you do not need to have empty bodies, like this:
public void windowDeiconified(WindowEvent e)
{}.
window event
window
listener
WindowEvent
WindowListener
SavitchJava.book Page 1002 Sunday, February 11, 2007 6:34 PM
Window Listeners
1003
Window Listeners
1003
Display 18.1
Methods in the
WindowListener
Interface
The
WindowListener
interface and the
WindowEvent
class are in the package
java.awt.event
.
public void windowOpened(WindowEvent e)
Invoked when a window has been opened.
public void windowClosing(WindowEvent e)
Invoked when a window is in the process of being closed. Clicking the close-window button causes
an invocation of this method.
public void windowClosed(WindowEvent e)
Invoked when a window has been closed.
public void windowIconified(WindowEvent e)
Invoked when a window is iconified. When you click the minimize button in a
JFrame
, it is iconified.
public void windowDeiconified(WindowEvent e)
Invoked when a window is deiconified. When you activate a minimized window, it is deiconified.
public void windowActivated(WindowEvent e)
Invoked when a window is activated. When you click in a window, it becomes the activated window.
Other actions can also activate a window.
public void windowDeactivated(WindowEvent e)
Invoked when a window is deactivated. When a window is activated, all other windows are deacti-
vated. Other actions can also deactivate a window.
The
WindowListener
Interface
When the user clicks any of the three standard
JFrame
buttons (for closing the window, min-
imizing the window, and resizing the window), that generates a window event. Window
events are sent to window listeners. In order to be a window listener, a class must imple-
ment the
WindowListener
interface. The method headings for the
WindowListener
inter-
face are given in Display 18.1.
SavitchJava.book Page 1003 Sunday, February 11, 2007 6:34 PM
1004
CHAPTER 18 Swing II
1004
CHAPTER 18 Swing II
EXAMPLE:
A Window Listener Inner Class
Display 18.2 gives an example of a
JFrame
class with a window listener class that is an inner
class. The window listener inner class is named
CheckOnExit
. A window listener class need not
be an inner class, but it is frequently convenient to make a window listener class (or other
kind of listener class) an inner class.
The main
JFrame
in Display 18.2 simply displays a message. What is interesting is how the
window listener programs the close-window button. You can apply the window listener used
in this
JFrame
to any
JFrame
. When the close-window button is clicked, a second, smaller
window appears and asks:
"Are you sure you want to exit?"
If the user clicks the
"Yes"
button, the entire program ends and so both windows go away. If the user clicks the
"No"
but-
ton, only the smaller window disappears; the program and the main window continue. Let’s
look at the programming details.
When the close-window button in the main window is clicked, that fires a window event.
The only registered window listener is the anonymous object that is the argument to
addWindowListener
. Below we repeat the relevant line of code, which is in the constructor for
WindowListenerDemo
:
addWindowListener(new CheckOnExit());
This anonymous window listener object receives the window event fired when the close-window
button is clicked and then invokes the method
windowClosing
. The method
windowClosing
cre-
ates and displays a window object of the class
ConfirmWindow
, which contains the message
"Are
you sure you want to exit?"
as well as the two buttons labeled
"Yes"
and
"No"
.
If the user clicks the
"Yes" button, the action event fired by that button goes to the
actionPerformed method, which ends the program with a call to System.exit. If the user
clicks the
"No" button, then the actionPerformed method invokes the method dispose. The
method
dispose, discussed in the next subsection, makes its calling object go away but does
not end the program. The calling object for
dispose is the smaller window (which is an object
of the class
ConfirmWindow), so this smaller window goes away but the main window remains.
Notice that even though we have registered a window listener, which says what should hap-
pen when the close-window button is clicked, we still need to invoke the method
setDefaultCloseOperation. When the close-window button is clicked, the policy set by
setDefaultCloseOperation is always carried out in addition to any action by window listeners. If we
do not include any invocation of
setDefaultCloseOperation, then the default action is to make the
window go away (but not to end the program). We do not want our main window to go away, so we
set the policy as follows:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
This means that clicking the close-window button causes no action other than the actions of
any window listeners. If you are using a window listener to set the action of the close-window
button, you invariably want an invocation of
setDefaultCloseOperation with the argument
JFrame.DO_NOTHING_ON_CLOSE.
SavitchJava.book Page 1004 Sunday, February 11, 2007 6:34 PM
Window Listeners 1005Window Listeners 1005
Display 18.2 A Window Listener (part 1 of 3)
1 import javax.swing.JFrame;
2 import javax.swing.JPanel;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.Color;
6 import javax.swing.JLabel;
7 import javax.swing.JButton;
8 import java.awt.event.ActionListener;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.WindowListener;
11 import java.awt.event.WindowEvent;
12 public class WindowListenerDemo extends JFrame
13 {
14 public static final int WIDTH = 300; //for main window
15 public static final int HEIGHT = 200; //for main window
16 public static final int SMALL_WIDTH = 200; //for confirm window
17 public static final int SMALL_HEIGHT = 100;//for confirm window
18 private class CheckOnExit implements WindowListener
19 {
20 public void windowOpened(WindowEvent e)
21 {}
22 public void windowClosing(WindowEvent e)
23 {
24 ConfirmWindow checkers = new ConfirmWindow();
25 checkers.setVisible(true);
26 }
27 public void windowClosed(WindowEvent e)
28 {}
29 public void windowIconified(WindowEvent e)
30 {}
31 public void windowDeiconified(WindowEvent e)
32 {}
33 public void windowActivated(WindowEvent e)
34 {}
This WindowListener
class is an inner class.
A window listener must
define all the method
headings in the
WindowListener interface,
even if some are trivial
implementations.
(continued)
SavitchJava.book Page 1005 Sunday, February 11, 2007 6:34 PM