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.

356Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProNote that the c<strong>on</strong>structor takes a String argument—that’s the text thatwill appear in the butt<strong>on</strong>. A butt<strong>on</strong> may also have an ic<strong>on</strong> (image) in it (more<strong>on</strong> that in just a bit). These butt<strong>on</strong>s, as created, d<strong>on</strong>’t do anything. Whenclicked <strong>on</strong> by the user, they will behave as real butt<strong>on</strong>s (depress, then release),but no acti<strong>on</strong> will occur. Yet.16.7.2.5 Acti<strong>on</strong>s for Butt<strong>on</strong>sWe need to attach an acti<strong>on</strong> to each butt<strong>on</strong>, which is little more than a specialclass to hold the code that you want to be run when the butt<strong>on</strong> is pressed. Wecan define the acti<strong>on</strong> as an an<strong>on</strong>ymous inner class, so that the code is rightthere, inline with the rest of our code. Then we just attach that code to thebutt<strong>on</strong>. Here is an example of that for our close butt<strong>on</strong> (the <strong>on</strong>e labeled Quit):234 Acti<strong>on</strong>Listener closActi<strong>on</strong> = new Acti<strong>on</strong>Listener()235 {236 public void237 acti<strong>on</strong>Performed(Acti<strong>on</strong>Event e)238 {239 System.exit(0);240 }241 } ;Acti<strong>on</strong>Listener is an interface—a very simple interface that defines just<strong>on</strong>e method, acti<strong>on</strong>Performed(). You can take any class, have it extendActi<strong>on</strong>Listener, and then define an acti<strong>on</strong>Performed() method for it.That class can then serve as the acti<strong>on</strong> for a butt<strong>on</strong>. Here we just create an inlineclass that does nothing but the acti<strong>on</strong>Performed() method, and a prettysimple <strong>on</strong>e at that. It simply exits.We could define the acti<strong>on</strong> elsewhere, and then just use the reference tothe acti<strong>on</strong>. If we had put the declarati<strong>on</strong> of closActi<strong>on</strong> at a higher lexicalscope (out at the beginning of the class definiti<strong>on</strong>, for example) then other UIelements could also use this acti<strong>on</strong>. Of course, if you’re going to share youracti<strong>on</strong> between GUI elements, be sure that you write the code to be reentrant.Lines 244–267 (still within the createButt<strong>on</strong>s()method) define theacti<strong>on</strong> for the butt<strong>on</strong> labeled New Subaccount. Line 268 c<strong>on</strong>nects it to thebutt<strong>on</strong>. D<strong>on</strong>’t pay attenti<strong>on</strong> to the specifics of this acti<strong>on</strong> just yet. We’ll discussit in detail below, <strong>on</strong>ce we know more about the other objects. Here is howthat acti<strong>on</strong> is built:

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

Saved successfully!

Ooh no, something went wrong!