Training Index
Converting Event Handlers: ItemListener and WindowListener
By Greg Voss, JavaSoft
[Tutorial Contents]
Now things get interesting. You're going to add
methods to ChoiceApplet0
that can be called by typing their names in
the TextField. Alternately, you can select
the names from the Choice component.
Start by changing the names of the
Strings that appear in the Choice
component:
public void init()
{
theChoice.add("big");
theChoice.add("small");
theChoice.add("unknown");
add(theChoice);
...
}
|
Next, upgrade the event handling for Choice;
use an ActionListener similar to the one
used by the TextField:
theChoice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(
"ENTER----->theChoice ActionListener");
String mName = (String) e.getItem();
System.out.println("mName: " + mName);
String pString =
e.paramString(); // useful for Debugging
System.out.println("pString: " + pString);
theTextField.setText(mName);
System.out.println(
"EXIT------>theChoice ActionListener");
}
});
|
Currently, only diagnostic information is printed in
the event handler, but eventually you'll used the
String returned by getItem to do a method
lookup. One interesting side effect in the handler
is that the TextField is set to whatever string
was just selected in the Choice component:
theTextField.setText(mName);
Now that you've upgraded the event handling mechanism for Choice
to JDK 1.1, you can get rid of the old action and
handleEvent methods:
public boolean action(Event event, Object arg)
{
System.out.println("ENTER----->action");
if (event.target == theChoice)
System.out.println(
theChoice.getSelectedItem() + " selected");
System.out.println("EXIT------>action");
return true;
}
public boolean handleEvent(Event e)
{
System.out.println(e.toString());
return super.handleEvent(e);
}
|
Finally a couple minor upgrades are in order. Change
the following deprecated calls in main.
Change:
theFrame.resize(500,300);
theFrame.show();
to:
theFrame.setSize(500,300);
theFrame.setVisible(true);
These kinds of changes are covered in: How
to Convert Programs to the 1.1 AWT API In fact the
updateAWT script mentioned in this document was used to
upgrade the ChoiceApplet to use JDK 1.1 events.
Contining the upgrade,
modify the appletFrame class to implement
the WindowListener interface.
class appletFrame extends Frame
implements WindowListener {
appletFrame() {
appletFrame() {
addWindowListener(this);
}
...
}
|
Now that you've made appletFrame
implement WindowListener,
you need to implement each of the methods
declared in the interface:
public void windowClosed(WindowEvent event) {
}
public void windowDeiconified(WindowEvent event) {
}
public void windowIconified(WindowEvent event) {
}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
|
The only handler that carries out any function
is windowClosing:
public void windowClosing(WindowEvent event) {
System.out.println("ENTER-----appletFrame.windowClosing");
this.setVisible(false);
dispose();
System.out.println("EXIT-----appletFrame.windowClosing");
System.exit(0);
}
|
This now does what the old handleEvent method did
inside appletFrame. However, note that the
updateAWT script changed another deprecated method call.
The line:
this.hide();
was changed to:
this.setVisible(false);
You can use TestChoiceApplet03.htmlto test the applet.
Program Source Code
A makefile
for this lesson automates source code compilation.
You may want to look at the final source file for
ChoiceApplet03.java
to verify any changes.
|