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.

17.3 Porting BudgetPro to SWT393• addKeyListener()• addMouseListener()• addMouseMoveListener()• addMouseTrackListener()• addPaintListener()• addTraverseListener()There are others. SWT naming c<strong>on</strong>venti<strong>on</strong>s define an interface forwhich each add...Listener() method is named. For example, there is aSelecti<strong>on</strong>Listener interface. Many such interfaces have multiple methods,each to handle a distinct kind of event; for example, the MouseListener interfacedefines separate methods to handle a butt<strong>on</strong> down event, a butt<strong>on</strong> releaseevent, and a double-click event. As in Swing, it is comm<strong>on</strong> to implement eventlisteners as an<strong>on</strong>ymous inner classes that implement the listener interface.However, since it is comm<strong>on</strong> to be interested <strong>on</strong>ly in some (or even <strong>on</strong>ly <strong>on</strong>e)listener event, it is annoying to have to implement the full interface, since youhave to provide method implementati<strong>on</strong>s for every event. For this reas<strong>on</strong>, SWTalso provides classes called adapters that implement “do-nothing” methods forevery listener event. These also follow a naming c<strong>on</strong>venti<strong>on</strong>. For example, theadapter for the MouseListener interface is a class named MouseAdapter; theSelecti<strong>on</strong>Listener interface has an adapter named Selecti<strong>on</strong>Adapter,and so <strong>on</strong>.For us, this means that we are going to create a reference to an an<strong>on</strong>ymousinner class that implements the Selecti<strong>on</strong>Listener interface by extendingthe Selecti<strong>on</strong>Adapter class. This is probably the weirdest comm<strong>on</strong> codec<strong>on</strong>struct in Java. Let’s take a direct look at that method (Example 17.2).If you can correctly answer the following questi<strong>on</strong>, then you can be reas<strong>on</strong>ablyassured that you do, in fact, understand what is going <strong>on</strong> here. Would theprogram compile and run correctly if the type of the upActi<strong>on</strong> variable werechanged to Selecti<strong>on</strong>Adapter? The answer is in the footnote. 1414. Yes, it would. The reas<strong>on</strong> is that the addSelecti<strong>on</strong>Listener() method takes an argumentof type Selecti<strong>on</strong>Listener. Both Selecti<strong>on</strong>Listener and Selecti<strong>on</strong>Adapterare of that base type. Aren’t Objects w<strong>on</strong>derful?

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

Saved successfully!

Ooh no, something went wrong!