Java Reference
In-Depth Information
Responding to Item Changes
Most items in a Form fire events when the user changes them. Your application can listen for
these events by registering an ItemStateListener with the Form using the following method:
public void setItemStateListener(ItemStateListener iListener)
ItemStateListener is an interface with a single method. This method is called every time
an item in a Form is changed:
public void itemStateChanged(Item item)
Listing 6-3 creates a Form with two items, an interactive Gauge and a StringItem . As you
adjust the Gauge , its value is reflected in the StringItem using the ItemStateListener mechanism.
Listing 6-3. GaugeTracker Source Code
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GaugeTracker
extends MIDlet
implements ItemStateListener, CommandListener {
private Gauge mGauge;
private StringItem mStringItem;
public GaugeTracker() {
int initialValue = 3;
mGauge = new Gauge("GaugeTitle", true, 5, initialValue);
mStringItem = new StringItem(null, "[value]");
itemStateChanged(mGauge);
}
public void itemStateChanged(Item item) {
if (item == mGauge)
mStringItem.setText("Value = " + mGauge.getValue());
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
}
Search WWH ::




Custom Search