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.

388Chapter 17Other Ways: Alternatives to SwingThe next step is to c<strong>on</strong>vert the GUI members of the class from the Swingclasses to their SWT counterparts. Of course, SWT requires the Display class,which has no analog in SWT, so we add a Display type member named dispjust ahead of the frame member.Next, we change the type of frame from JFrame to Shell. We could renamethe member, 10 but why add to our typing burden? The name is still clearand meaningful, even if it doesn’t match the SWT name. 11 There’s more to itthan just changing the type, however. The c<strong>on</strong>structor call for the JFramedoesn’t match any c<strong>on</strong>structor for Shell. In fact, the Shell c<strong>on</strong>structor requiresa Display object argument, and all subsequent c<strong>on</strong>structors for widgetsand c<strong>on</strong>trols require a Composite as an argument.This is a key difference between Swing and SWT. Swing allows you tobuild GUI comp<strong>on</strong>ents in arbitrary order at arbitrary times and then join themup to the GUI with an add() method call. SWT instead requires that you linkyour comp<strong>on</strong>ents up to the GUI element they bel<strong>on</strong>g to when they are c<strong>on</strong>structed.There are good reas<strong>on</strong>s for this difference. Remember that SWT allocatesnative objects and memory that Java’s garbage collector cannot recover. Becauseof this, SWT makes the promise that if you call the dispose() method <strong>on</strong> anySWT object, it will dispose of it and everything it c<strong>on</strong>tains. That allows you toclean up all resources from an SWT program by calling dispose() <strong>on</strong> the toplevel Display object. If SWT allowed you to build GUI structures independentlyand then graft them <strong>on</strong>to the hierarchy, it could not keep this promise.For this reas<strong>on</strong> (am<strong>on</strong>gst others) SWT objects are always built in a fairly rigidtop-down manner. 12The most direct c<strong>on</strong>sequence of this is that we have to get rid of the c<strong>on</strong>structors<strong>on</strong> these declarati<strong>on</strong>s. We’ll start c<strong>on</strong>structi<strong>on</strong> in the main(). So, awaywith the c<strong>on</strong>structors for the GUI elements. We now need to change theJButt<strong>on</strong>s to Butt<strong>on</strong>s and the JLabels to Labels. Again, if you are using adynamic IDE, you should see your error count skyrocket with these changes10. If you are using Eclipse, this is easily d<strong>on</strong>e throughout your code with the Refactoringfeature.11. All right, I’m being lazy. Write your own book if you d<strong>on</strong>’t like it.12. In some ways, this greatly simplifies SWT programs, but at the cost of some reusability.With Swing, you could c<strong>on</strong>struct a panel or other GUI element and reuse it in many places.You can achieve the same thing in SWT by encapsulating such a c<strong>on</strong>struct in its own class andpassing in a parent to the c<strong>on</strong>structor, but this is a bit more bulky and complex than theSwing way.

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

Saved successfully!

Ooh no, something went wrong!