Java Reference
In-Depth Information
244 ActionListener creatAction = new ActionListener()
245 {
246 public void
247 actionPerformed(ActionEvent e)
248 {
249 Account child;
250 // get the info via a Dialog (of sorts)
251 if (askem == null) {
252 askem = new AcctDialog(frame, "New Subaccount");
253 } else {
254 askem.clear();
255 askem.setVisible(true);
256 }
257 String subName = askem.getName();
258 String subAmnt = askem.getAmnt();
259
260 // if empty, assume the operation was cancelled, else:
261 if ((subName != null) && (subName.length() > 0)) {
262 child = current.createSub(subName, subAmnt);
263 setStatus();
264 model.fireTableDataChanged(); // notify the table
265 }
266 }
267 };
268 creat.addActionListener(creatAction);
Looking at the constructor for an Account , we see that we need three
things: a User object (who will own the subaccount), a name for the new sub-
account, and the dollars to be allocated to this subaccount. To keep our exam-
ple simpler, we will always use the current user as the User for creating the new
Account . That means we only need some way to get the name and dollar
amount.
In the GUI world, this sort of information is typically provided in a dialog
box, a window that has blanks to be filled in (Figure 16.5). Then, when the
dialog is closed, we can ask that dialog for the values that the user provided.
Swing has some ready-to-use dialogs for warnings or for simple single value
inputs. Since we want to get two pieces of data, we need to create our own
dialog and display it.
What may seem strange about the createAction() is that we only create
the dialog once (line 252), when the reference to it ( askem ) is null (line 251).
Thereafter, we simply clear out the previous values (line 254) and make the
dialog visible again (line 255). That is all that it takes to use the dialog more
Search WWH ::




Custom Search