Java Reference
In-Depth Information
18 Swing II
Window listeners?
I thought windows were for looking not for listening.
Student answer on an exam
Introduction
This chapter is a continuation of Chapter 17 , presenting more details about designing
regular Swing GUIs. Chapter 20 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:
window event
window
listener
WindowEvent
Window
Listener
public void windowDeiconified(WindowEvent e)
{}
 
 
Search WWH ::




Custom Search