Java Reference
In-Depth Information
You also need to implement a corresponding itemStateChanged() method. Since the
itemStateChanged() method is called for changes of all Item s in the Form , you need to check
the item parameter indicating the event source first. If the source of the event is the currency
ChoiceGroup , you set the labels of the amount and fraction TextField s correspondingly:
public void itemStateChanged (Item item) {
if (item == currency) {
int index = currency.getSelectedIndex();
switch (index) {
case 0: amountWhole.setLabel ("Dollar");
amountFraction.setLabel ("Cent");
break;
case 1: amountWhole.setLabel ("Euro");
amountFraction.setLabel ("Cent");
break;
case 2: amountWhole.setLabel ("Yen");
amountFraction.setLabel ("Sen");
}
}
}
Just adding the interface and implementing the corresponding methods is not sufficient to enable the
MIDlet to receive state changes. Additionally, you need to register your ItemStateListener at
the Form containing the currency item. You do so by calling the setItemStateListener()
method in the TeleTransfer constructor:
public TeleTransfer() {
mainForm.append (senderAccount);
...
mainForm.append (currency);
mainForm.setItemStateListener(this);
}
Figure 3.5 shows the new version of the TeleTransfer example, where the labels are changed
depending on the state of the currency ChoiceGroup .
Figure 3.5. The TeleTransfer MIDlet extended to change the labels depending on the
state of the currency ChoiceGroup .
Using Commands for User Interaction
Now you can enter all the information required for a telegraphic transfer, but you have no means to
initiate the actual transfer.
In contrast to desktop computers, which have plenty of screen space for displaying buttons or menus, a
different approach is necessary for mobile devices. Some devices provide so-called soft buttons, which
are buttons without fixed functionality that are assigned dynamically depending on the application
context. The number of soft buttons may vary if they are available. Other mobile devices do not even
 
 
 
Search WWH ::




Custom Search