Java Reference
In-Depth Information
Figure 16.5 Dialog for creating a new subaccount
than once. We could throw away the dialog (or let it get garbage-collected) by
declaring it internal to the actionPerformed() method. Then on each button
press the dialog would need to be recreated. Well, it's slower to do it that way,
and for a button click we want quick response—so we keep it around from one
use to the next. When the user closes the dialog, all that really does is makes it
invisible; to reuse it, we make it visible again.
Notice, too, that in either case—creating the dialog or making it visi-
ble—control does not return to our method until the user has dismissed the
dialog. That's because it's a modal dialog, one that allows no other interaction
with the application until the user has responded to this dialog.
The dialog is dismissed (finished, ended, put away) simply by making it
no longer visible. For example:
73 dialog.setVisible(false); // go away
New to our application, in AcctDialog , is the JTextField . On lines 22
and 23 we declare two of them, one for the account name and the other for the
amount.
22 nameField = new JTextField(25);
23 amntField = new JTextField(9);
The size that we pass in to the constructor is the number of characters; it
sets a maximum for that field, but also gives a clue to some layout managers as
to how big the field needs to be.
Speaking of layout managers, we use a few here, including a BoxLayout ,
to format the buttons relative to each other; a BorderLayout , to hold the
overall dialog; and a newer layout manager, the SpringLayout , which is new
as of Java 1.4. The Swing Tutorial provides a handy utility class for dealing
Search WWH ::




Custom Search