Java Reference
In-Depth Information
Now that the three panels have been fully. set up, they are added to the SurveyPanel
interface, which completes the work of the constructor method:
GridLayout grid = new GridLayout(3, 1);
setLayout(grid);
add(sub1);
add(sub2);
add(sub3);
There's one extra wrinkle in the SurveyPanel class—a method that enables the Finish
button and disables the Next button when the last question has been reached:
void setFinalQuestion(boolean finalQuestion) {
if (finalQuestion) {
nextButton.setEnabled(false);
finalButton.setEnabled(true);
}
}
In a user interface that uses card. layout, the display of each card usually takes place in
response to an action by the user.
These actions are called events, and they are covered on Day 12, “Responding to User
Input.”
11
A brief introduction demonstrates how the SurveyPanel class is equipped to handle but-
ton clicks.
The class implements ActionListener , an interface in the java.awt.event package:
public class SurveyWizard extends JPanel implements ActionListener {
// more to come
}
This interface indicates that the class can respond to action events, which represent but-
ton clicks, menu choices, and similar user input.
Next, each button's addActionListener( Object ) method is called:
ask[0].nextButton.addActionListener(this);
ask[0].finalButton.addActionListener(this);
Listeners are classes that monitor specific. kinds of user input. The argument to
addActionListener() is the class that's looking for action events. Using this as the
argument indicates that the SurveyPanel class handles this job.
Search WWH ::




Custom Search