Java Reference
In-Depth Information
static final String [] CURRENCY_FRACTIONS = {"Cent", "Cent", "Sen"} ;
In order to set the labels of the main forms
TextField
s for the whole and the fractional amount
according to the selected currency in the
CurrencyList
, you need a reference back to the main
TeleTransfer
MIDlet. For this reason, you store the
TeleTransfer
reference in a variable
called
teleTransfer
. The reference is set in the constructor of your
CurrencyList
:
TeleTransfer teleTransfer;
In the constructor, you also add currency symbol images to the list. You need to load them, but the call
to the super constructor must be the first statement in a constructor. So you call the constructor of the
super class by specifying the title and type only. Then you create the
Image
s needed for each list
element, which are stored in the MIDlet suite's JAR file. You also call
setCommandListener()
to
register the currency list for handling commands that are issued:
public CurrencyList (TeleTransfer teletransfer) {
super ("Select Currency", Choice.IMPLICIT);
this.teleTransfer = teletransfer;
try {
append ("USD", Image.createImage ("/Dollar.png"));
append ("EUR", Image.createImage ("/Euro.png"));
append ("JPY", Image.createImage ("/Yen.png"));
}
catch (java.io.IOException x) {
throw new RuntimeException ("Images not found");
}
setCommandListener(this);
}
The final step in creating the
CurrencyList
is to implement the
commandAction()
method of
the
CommandListener
interface. As you already know, a
List
of
IMPLICIT
type issues a
List.SELECT_COMMAND
to the registered
CommandListener
whenever a new element is
selected to indicate the selection change. In case of a selection change, you modify the labels of the
main form
TextField
s. The actual labels are obtained from the
String
arrays
CURRENCY_NAMES
and
CURRENCY_FRACTIONS
. Using the
teleTransfer
reference, you can access the
TextField
s. Finally, you call the new method
teleTransfer.back()
, which sets the screen
back to the main form (the
back()
method will be given at the end of this section):
public void commandAction (Command c, Displayable d) {
if (c == List.SELECT_COMMAND) {
teleTransfer.amountWhole.setLabel
(CURRENCY_NAMES [getSelectedIndex()]);
teleTransfer.amountFraction.setLabel
(CURRENCY_FRACTIONS [getSelectedIndex()]);
teleTransfer.back();
}
}
}
Figure 3.7
shows currency
Image
s and abbreviations in the
CurrencyList
.
Figure 3.7. The new
CurrencyList
.
Search WWH ::
Custom Search