Java Reference
In-Depth Information
196
// Handler for window deiconified event
197
public void windowDeiconified ( WindowEvent event )
198
{
199
}
200
// Handler for window activated event
201
public void windowActivated ( WindowEvent event )
202
{
203
}
204
// Handler for window deactivated event
205
public void windowDeactivated ( WindowEvent event )
206
{
207
}
208 }
FIGURE 10-41
Line 16 in Figure 10-41 indicates that the StockFrame class implements the
ActionListener interface and the WindowListener interface. The ActionListener
interface provides the ability to listen for mouse clicks on a button, once the lis-
tener interface is registered with the button; the WindowListener interface listens
for the user opening or closing the window. In the StockDemo application, once
a user has finished entering stocks in the StockListDemo application, the user
closes the StockFrame window by clicking the Log out button or the Close
button on the StockFrame user interface. When the StockFrame window closes,
the LogonFrame window must be redisplayed. The StockFrame window, thus,
must hide itself, dispose of its resources, and call the callback method of the
Activator — LogonFrame, in this case — that originally created the StockFrame
object. The StockFrame object, therefore, must listen for these actions by the
user, using the ActionListener and the WindowListener interfaces coded in line
16. Additionally, the StockFrame object must listen for when its window is
opened in order to position the insertion point in the text field for entering a
stock symbol.
The StockFrame will use the callback mechanism to take a reference to the
caller object — in this case, the LogonFrame object — and call its activate()
method. As previously noted, however, the call to the caller object is polymor-
phic, which means that StockFrame never needs to refer to an object of type
LogonFrame, but only to an object of type Activator, as in line 25 in Fig-
ure 10-41. This has two implications. First, any object can be called back by
StockFrame as long as that object implements the Activator interface and passes
a reference to itself when creating a StockFrame object. Second, such objects can
be referenced generically by StockFrame as objects of type Activator, regardless
of whatever type these objects are. StockFrame, therefore, is reusable by any
object implementing the Activator interface.
Line 27 begins a StockFrame constructor, which contains references to a
User object and to an Activator object. Lines 29 through 133 create the user
interface and functionality for the StockFrame window, including displaying the
window centered and preventing the user from adding duplicate stock symbols
to the list.
Lines 135 through 168 add the actionPerformed() method required by
the ActionListener interface. Lines 170 through 175 include code for the
logoutUser() method, which hides the current window, disposes of its resources,
and invokes the callback method, activate(), in the calling object, LogonFrame.
Search WWH ::




Custom Search