Java Reference
In-Depth Information
118
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
to program the close-
setDefaultCloseOperation
. 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
window button in a
JFrame
window event
window
listener
fires an event known as a
JFrame
window event
. A
can use the method
to register a
window
JFrame
setWindowListener
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
. A window listener is any class that satisfies the
interface.
WindowEvent
WindowListener
WindowEvent
WindowListener
The method headings in the
interface are given in Display 18.1. If a
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:
class implements the
WindowListener
public void windowDeiconified(WindowEvent e)
{}.
Search WWH ::




Custom Search