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.

390Chapter 17Other Ways: Alternatives to Swingnow. The original BudgetPro c<strong>on</strong>structor took a JFrame argument, now itwill have to get a Display and a Shell. So we have to allocate a local Displayand a local Shell. We also need to add the Display argument to theBudgetPro c<strong>on</strong>structor.After this is d<strong>on</strong>e, we modify the call to the c<strong>on</strong>structor to pass the localDisplay and Shell to our class instance.Next, we have to set a layout manager. The original applicati<strong>on</strong> used theSwing BorderLayout layout manager. SWT doesn’t have such a critter.Fortunately, the original used <strong>on</strong>ly the north, center, and south positi<strong>on</strong>sof the BorderLayout. SWT has a simple layout manager called aFillLayout that puts its c<strong>on</strong>tained c<strong>on</strong>trols in a single row or column, equallysized. Putting the three c<strong>on</strong>trols in a column will end up looking much likeusing the north, center, and south of a BorderLayout. So we change the callto the frame.setLayout() to pass in a new FillLayout and add theSWT.VERTICAL attribute.The SWT ClassThe SWT class is pretty bare-b<strong>on</strong>es. Its primary use is a library of namedc<strong>on</strong>stants used for attributes to Widget (and other) c<strong>on</strong>structors. You’llsee such SWT.xxxx c<strong>on</strong>stants all over your typical SWT applicati<strong>on</strong>.There are a handful of methods that the SWT class provides, all ofthem static, including error(), which throws an SWTExcepti<strong>on</strong>,getPlatform(), which returns a string with the name of the platform<strong>on</strong> which SWT is running, and getVersi<strong>on</strong>(), which returns an intversi<strong>on</strong> number.It also has a subclass, called OLE, which is a Windows-<strong>on</strong>ly class thatprovides ActiveX support for SWT. Obviously, such use is n<strong>on</strong>portableand n<strong>on</strong>-<strong>Linux</strong>, so we w<strong>on</strong>’t talk any more about it.The next block of code in main() sets the Swing look and feel. SWT hasnothing like this. All SWT applicati<strong>on</strong>s look like native applicati<strong>on</strong>s (we seemto be saying that a lot), so all of this code may be removed.The next block of code calls methods <strong>on</strong> the applicati<strong>on</strong> object (app) that,in the original, c<strong>on</strong>struct the three “chunks” of UI and add them to the frameusing the BorderLayout attributes. Since, as we explained earlier, all SWTc<strong>on</strong>trols must be explicitly joined to a parent c<strong>on</strong>trol when they are c<strong>on</strong>structed,

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

Saved successfully!

Ooh no, something went wrong!