Java Reference
In-Depth Information
Listing 10.4
The
Currency.java
class
package com.acme.conversion.currency;
import java.util.HashSet;
public final class Currency {
private static Set allCurrenciesSet;
static {
allCurrenciesSet = new HashSet();
allCurrenciesSet.add("USD");
allCurrenciesSet.add("CDN");
}
public static Iterator getCurrencies() {
return allCurrenciesSet.iterator();
}
}
Now, you can rely on the
Currency
class to provide a list of currencies you support.
You can query the
Currency
class for its list of known currencies and then add a
string into the
JC
omboBoxes' option lists for each occurrence. Listing 10.5 shows
the first version of the form class's constructor.
Listing 10.5
The
ACMEGUI.java
constructor, which populates the GUI's
JComboBoxes
...
public ACMEGUI() {
Iterator currencies = Currency.getCurrencies();
while (currencies.hasNext()) {
String currency = (String)currencies.next();
fromCurrency.addItem(currency);
toCurrency.addItem(currency);
}
}
...
10.6.3
Implementing the Convert functionality
In addition to initializing the
JC
omboBoxes with data, you need to add another
significant piece of functionality: the power behind the
Convert
button. When
this button is clicked, the
GUI
should collect the amount and the currencies from