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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16.7 Getting Down to Cases: Designing a GUI for BudgetPro357244 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>);We defined the acti<strong>on</strong> for the View Subaccount butt<strong>on</strong> (as we said youcould) elsewhere in the program. Its acti<strong>on</strong> is defined in lines 54–75. Then <strong>on</strong>line 271 we c<strong>on</strong>nect the acti<strong>on</strong> to the butt<strong>on</strong>. (We’ll get back to this butt<strong>on</strong>’sacti<strong>on</strong>, too, <strong>on</strong>ce we’ve discussed the JTable.) But after we’ve attached the acti<strong>on</strong>,we also disable the butt<strong>on</strong> (line 273).270 // functi<strong>on</strong> is to get selecti<strong>on</strong> from table and cd there271 view.addActi<strong>on</strong>Listener(cdActi<strong>on</strong>);272 // but it starts off disabled, since there is no data yet273 view.setEnabled(false);In Swing, a butt<strong>on</strong> is either enabled or disabled. Enabled butt<strong>on</strong>s are theactive <strong>on</strong>es <strong>on</strong> which you can click. Disabled butt<strong>on</strong>s are grayed out and notresp<strong>on</strong>sive to clicks. We can make a butt<strong>on</strong> either active or inactive with amethod <strong>on</strong> the butt<strong>on</strong> called setEnabled() whose argument is aboolean—true to enable the butt<strong>on</strong>, false to disable it. For example:

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

Saved successfully!

Ooh no, something went wrong!