Java Reference
In-Depth Information
want to notify our Observer classes of, an OptionUpdate value update is constructed and sent
to all the observers.
For example, when the user leaves the locationField or the portNumber field, then the fol-
lowing code is executed:
public void focusLost(FocusEvent e) {
if (DB_LOCATION_LABEL.equals(e.getComponent().getName())
&& ( ! locationField.getText().equals(location))) {
location = locationField.getText();
updateObservers(OptionUpdate.Updates.DB_LOCATION_CHANGED,
location.trim());
}
if (SERVER_PORT_LABEL.equals(e.getComponent().getName())
&& ( ! portNumber.getText().equals(port))) {
port = portNumber.getText();
updateObservers(OptionUpdate.Updates.PORT_CHANGED, port.trim());
}
}
Assuming that the user has changed one of these fields, the following method is called:
private void updateObservers(OptionUpdate.Updates updateType, Object payLoad) {
OptionUpdate update = new OptionUpdate(updateType, payLoad);
observerConfigOptions.setChanged();
observerConfigOptions.notifyObservers(update);
}
Note We called the setChanged method on the Observable object before calling the notifyObservers
method. If you call notifyObservers without first calling setChanged , no Observer s will be notified.
That is all we need to do to notify however many observers we may have. While it may
appear that we have gone to a lot of work to pass some information back to either the Client
Database Location dialog box or to the server GUI, the advantage is that we have decoupled
the panel from the users of the panel—any class can use this panel, and simply by registering
themselves as an Observer of the panel they can get notification whenever anything changes
on the panel.
The next two sections—“The Client Database Location Dialog Box” and “The Server
GUI”—show the other half of the Observer-Observable pair. Both will be Observer s of the
panel described here.
The Client Database Location Dialog Box
Having created a common frame for the various modes, we can add it to a dialog box, as
shown in Listing 8-30.
Search WWH ::




Custom Search