Java Reference
In-Depth Information
The next step in the process is to bind all the important GUI components to
fields in that new class. You can use intention actions to create them individually,
or you can edit the file to add the references by hand and then pick their names
from within the Properties Inspector. Opting for the former, select the JP anel on
the component tree view, edit its binding property in the Properties Inspector to
refer to the mainPanel field, and then use the intention action to create that field
in the ACMEGUI class. Repeat those steps for the components listed in table 10.3.
Table 10.3 By binding your components to specific fields in your form class, they can be accessed
and manipulated programmatically.
Field name
Component
JTextField where the user is expected to enter the amount of currency to convert
amountField
JComboBox where the user is expected to select the currency at the beginning of
the conversion
fromCurrency
JComboBox where the user is expected to select the currency at the end of
the conversion
toCurrency
resultField
JTextField where you intend to display results to the user
convertButton
JButton that the user uses to perform the conversion after all the data is collected
quitButton
JButton that the user uses to close the GUI and stop using the application
10.6.2
Creating a constructor
Now that the components are bound to the form class, you need to implement a
constructor. This is made simple by IDEA 's Generate functionality. Open the
source file, select Code | Generate or press Alt+Ins , and select Constructor from
the pop-up menu. Select the Select None button when prompted for fields to ini-
tialize through parameters. IDEA inserts a default, empty constructor.
The GUI needs some initialization, however. Two JC omboBoxes need to be
populated with the list of currencies the system is aware of. You need to populate
those JC omboBoxes in the class constructor so that they'll be properly filled with
the correct data before the GUI is shown on the screen.
So far in the implementation, you haven't created a class to track which cur-
rencies are supported and which aren't—you've only been interested in USD and
CDN . Listing 10.4 shows a simple class that you can programmatically query to
return this list.
 
 
Search WWH ::




Custom Search