13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.7 Getting Down to Cases: Designing a GUI for BudgetPro371244 Acti<strong>on</strong>Listener creatActi<strong>on</strong> = new Acti<strong>on</strong>Listener()245 {246 public void247 acti<strong>on</strong>Performed(Acti<strong>on</strong>Event 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();259260 // if empty, assume the operati<strong>on</strong> was cancelled, else:261 if ((subName != null) && (subName.length() > 0)) {262 child = current.createSub(subName, subAmnt);263 setStatus();264 model.fireTableDataChanged(); // notify the table265 }266 }267 };268 creat.addActi<strong>on</strong>Listener(creatActi<strong>on</strong>);Looking at the c<strong>on</strong>structor for an Account, we see that we need threethings: a User object (who will own the subaccount), a name for the new subaccount,and the dollars to be allocated to this subaccount. To keep our examplesimpler, we will always use the current user as the User for creating the newAccount. That means we <strong>on</strong>ly need some way to get the name and dollaramount.In the GUI world, this sort of informati<strong>on</strong> is typically provided in a dialogbox, a window that has blanks to be filled in (Figure 16.5). Then, when thedialog 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 valueinputs. Since we want to get two pieces of data, we need to create our owndialog and display it.What may seem strange about the createActi<strong>on</strong>() is that we <strong>on</strong>ly createthe dialog <strong>on</strong>ce (line 252), when the reference to it (askem) is null (line 251).Thereafter, we simply clear out the previous values (line 254) and make thedialog visible again (line 255). That is all that it takes to use the dialog more

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!