8.8 Handling Multiple Button Events | 411
In the preceding example, we introduced two new methods,disposeand exit. The dis-
posemethod is an instance method associated with a JFrameobject. Its effect is to permanently
remove the frame from the screen. We could also have called setVisible(false), but dispose
does some extra work in the background that saves the JVM from having to clean up after
us. The exitmethod is a class method associated with the Systemclass. It causes the JVM to
terminate execution of our application. Passing it an argument of 0 indicates that the ap-
plication was intentionally ended in a normal manner.
We have now seen how to handle multiple button events in a single handler. In the pre-
ceding Case Study, we saw how to handle events from different buttons by using a handler
for each one. How do we decide which approach to take for a given problem?
When a user interface contains buttons that perform tasks associated with a specific ob-
ject, it makes sense to combine the handling of their events into a single method. If clicking
a button requires processing that is unrelated to other buttons, then it should have its own
event handler. For example, in our Rainfallapplication, each station has its own Enter but-
ton, and a handler that is dedicated to that particular button. Both the button and its han-
dling are integral to a Stationobject. It would not make sense to create a single button
handler that responds to the buttons for all of the stations. On the other hand, if we extend
the definition of a Stationobject to include a Reset button, then it makes perfect sense to have
the handler for a station respond to both the Enter and the Reset buttons appropriately.