Where were you in 1982? Check out: Froggy's Lament Lyrics and Froggy's Lament Sound Clip
We don't want to deal with honor code cases, and neither do you, so don't do anything that might be considered a violation. Everyone will be much happier.
If there's any doubt in your mind about consulting a source for the test programs, don't do it! If you think it might be OK, check with one of the instructors first.
Last time, we saw how the AWT library makes it easy to write programs with nice, interactive user interfaces. This library provides classes to create components, which are things like buttons, scrollbars and menus.
Recall the 4 steps to use an AWT component:
We looked at the first two steps for scrollbars, menus and buttons. Now on to the rest.
Start with Scrollbars again.
Demo: Boxball using a Scrollbar
Our third step was to write an event handler. In this case, it is the adjustmentValueChanged method at the bottom of Boxball.
Finally, we need to tell our Scrollbar to report its move events to our adjustmentValueChanged method.
To do this, we call the Scrollbar's addAdjustmentListener method and pass it an instance of a class that contains the adjustmentValueChanged method. Here, we pass it this, since it is our window controller class that contains the adjustmentValueChanged method.
The addAdjustmentListener method certainly isn't expecting a class of type Boxball or even a WindowController. In fact, it wants to be passed something of type AdjustmentListener.
AdjustmentListener is an interface.
Sun Java Docs: AdjustmentListener interface
In order to pass in our Boxball class as the parameter to addAdjustmentListener, we have to make Boxball implement the AdjustmentListener interface. (It does.)
Here, the class that implements AdjustmentListener also happens to be the class that created our Scrollbar and everything else. This is not always the case, and any class that implements AdjustmentListener can be used.
Summary of the steps for the Scrollbar example:
Next we consider how to handle events from our buttons.
Demo: Boxball using Buttons
The basic idea is the same as in the Scrollbar example.
In the Scrollbar event handler, the adjustmentValueChanged method, we didn't use the parameter that was passed in.
What kind of information might we get in that parameter?
The event handlers we wrote previously received a Location that told us the location of the mouse when the particular event occurred.
Since the same event handler can be used for multiple components, it would be good to know which component caused the event. With the scrollbar, this wasn't important since we knew there was just the one scrollbar. But we have multiple buttons, and all will share the event handler in our Boxball class.
It turns out that the parameters to AWT event handlers can be used to find out which component generated the event.
All of the event handlers we'll use have a parameter of a type that implements the EventObject interface. This interface includes a method getSource that returns the source of the event.
Given all of this, we can look at the actionPerformed method of our boxball game with buttons. Basically, we just call getSource on the event passed in, see which button caused the event, and act accordingly.
Now what about our drop-down menu, the Choice component?
Demo: Boxball using Buttons
What information needs to be obtained when the user makes a selection in the Choice component? We need to know which component was used and we need to know which entry was selected.
In our case, the first part is easy - since we have only the one Choice component. We need only determine which entry was selected.
The setup is the same, though a different interface is used to capture the appropriate event types:
So we know that itemStateChanged will be called when the user makes a selection from our Choice component.
The Choice has a method, much like the Scrollbar's getValue method, that returns its state to us. getSelectedItem returns a String containing the text label of the menu item that is currently selected.
Given that, we compare it to the three strings we placed in the Choice when we created it to find out what action to take.
An important side note here about the comparison of Strings. We will look at Strings in more detail in a few weeks, but note that we don't compare them using == like we use for numbers. We must call the method equals to decide if two strings are equivalent.
It is placed in the CENTER for you automatically.
We saw briefly that the three buttons from the boxball with buttons example needed to be placed in an intermediate container called a Panel. Our panel in that example was placed in the SOUTH, and the buttons allowed to migrate to their own positions from within it.
If we don't like the way the Panel arranges our buttons (or other components), we can give it more guidance. See Slack's Appendix B for more details.