Java Reference
In-Depth Information
the abbreviations specified in the
String
array. The last parameter of the constructor is set to
null
because you do not want
Image
s to be displayed at this time:
ChoiceGroup currency = new ChoiceGroup
("Currency", Choice.EXCLUSIVE,
new String[] {"USD", "EUR", "JPY"} , null);
You still need to add the
currency
ChoiceGroup
to your main
Form
. As for the text fields, this is
done via the
append()
method of
Form
:
mainForm.append (currency);
Figure 3.4
shows the
TeleTransfer
application extended to choose a currency using a
ChoiceGroup
.
Figure 3.4. The
TeleTransfer
MIDlet extended to enable the user to choose a
currency.
Receiving Changes from Interactive UI Items
If you run the new version of the
TeleTransfer
MIDlet, you can change the currency using the
ChoiceGroup
, but the
TextField
labels for Dollar and Cent are not changed accordingly. You
need a way to notify the application if a selection is made in the currency
ChoiceGroup
.
Receiving changes of interactive high-level UI items in MIDP is based on a listener model similar to
AWT. Classes implementing the
ItemStateListener
interface are able to receive notifications for
the following events:
• State changes of a
ChoiceGroup
• Value adjustments of an interactive
Gauge
•
TextField
value changes
•
DateField
changes
The events are sent to the method
itemStateChanged()
of the
ItemStateListener
, where
the item that has changed is given as a parameter. In order to actually receive these events, the
ItemStateChangeListener
must be registered using the
setItemStateListener()
method of the corresponding
Form
.
Now that you know about item state change events, you can add the desired functionality to your
TeleTransfer
MIDlet. First, you need to add the
ItemStateListener
interface to the class
declaration:
public class TeleTransfer extends MIDlet implements ItemStateListener
{
Search WWH ::
Custom Search