programming java desktop application using swt.pdf

24
Programming Java Desktop application using SWT This document is based on: Eclipse 4.4 (!"#$ %n this document& % 'ill introduce ou to the Desktop application programming 'ith SWT. )* +,P (+ich ,lient Plat-orm$ +,P (+ich ,lient Plat-orm$ * #s a plat-orm based on SWT& used -or programming Desktop applications& so -ar it has built a plat-orm that allo's ou to develop desktop*stle applications Workbench& like Eclipse %DE& or programmers can integrate the plugin to Eclipse %DE. ut even i- ou 'ant to use SWT to programming& and do not need to use those provided b the +,P ou should also create an +,P application. SWT: Workbench #pplication: /

Upload: ionizare

Post on 01-Jun-2018

252 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 1/24

Programming Java Desktop application using SWTThis document is based on:

Eclipse 4.4 ( !"#$%n this document& % 'ill introduce ou to the Desktop application programming 'ith SWT.)* +,P (+ich ,lient Plat-orm$ ▴

+,P (+ich ,lient Plat-orm$ * #s a plat-orm based on SWT& used -or programming Desktopapplications& so -ar it has built a plat-orm that allo's ou to develop desktop*st le applicationsWorkbench& like Eclipse %DE& or programmers can integrate the plugin to Eclipse %DE.

ut even i- ou 'ant to use SWT to programming& and do not need to use those provided b the+,P ou should also create an +,P application.

SWT:

Workbench #pplication:

/

Page 2: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 2/24

To create a desktop application using SWT. 0n the Eclipse& 'e 'ill create an +,P PluginPro1ect. 2ou have ) options.

0nl use the -eatures o- the SWT !sing the plat-orm provided b the +,P to +,P #pplication Workbench programming

%n this document& % 'ill guide ou to become -amiliar 'ith basic programming SWT& usingWindo' uilder to drag and drop components into the inter-ace.3* The settings re uired be-ore starting ▴

Some re uired settings be-ore ou start:

2ou need the latest version o- Eclipse. There currentl is Eclipse 4.4 (,odes !"#$. http:55eclipse.org5do'nloads5

%n m opinion ou to do'nload package: 6Eclipse %DE -or Java EE Developers6. The onldi--erent is number o- Plugins& -or the purpose o- di--erent programming. 2ou can installadditional plugins -or other purposes i- desired.

Windo' uilder& this is a plugin that allo's ou to design SWT 7!% applications using drag anddrop convenience.See installation instructions at:

http:55o8planning.org5'eb5-e5de-ault5en5document59 ;;5install*'indo'builder*into*eclipse

4* Some concepts o- SWT. ▴

4./* Displa < Shell ▴

The Displa and Shell classes are ke components o- SWT applications.

)

Page 3: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 3/24

# org.eclipse.s't.'idgets.Shell class represents a 'indo'.

The org.eclipse.s't.'idgets.Displa class is responsible -or managing event loops& -onts& colorsand -or controlling the communication bet'een the !% thread and other threads. Displa is thebase -or all SWT capabilities.

Ever SWT application re uires at least one Displa and one or more Shell instances. The mainShell gets& as a de-ault parameter& a Displa as a constructor argument. Each Shell isconstructed 'ith a Displa and i- none is provided during construction it 'ill use either theDispla 'hich is currentl used or a de-ault one.E=ample:Display display = new Display();Shell shell = new Shell(display);shell.open(); // run the event loop as long as the window is openwhile (!shell.isDisposed()) { // read the next OS event queue and trans er it to a S " event i (!display.read#ndDispat$h()) { // i there are $urrently no other OS event to pro$ess // sleep until the next OS event is availa%le display.sleep(); && // disposes all asso$iated windows and their $o'ponentsdisplay.dispose();4.)* SWT Widgets ▴

SWT 'idgets are located in the packages org.eclipse.s't.'idgets and org.eclipse.s't.custom.Widgets e=tend either the Widget or the ,ontrol class. Several o- these 'idgets are depicted inthe -ollo'ing graphic. This graphic is a screenshot o- the SWT 'idget homepage.

9* ,reate +,P Plugin Pro1ect ▴

%n Eclipse select >ile5"e'50ther...

3

Page 4: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 4/24

4

Page 5: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 5/24

So there is no need to create a Workbench application so 'e can not check in (/$ assho'n belo'.Select ?2es? in the region ()$ to Eclipse +,P #pplication created (+un on the Desktop$&other'ise it 'ill create +#P #pplication (+unning on Web$.

9

Page 6: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 6/24

,reated Pro1ect: #dd libar s't: org.eclipse.s't

%- ou develop +#P& using org.eclipse.rap.r't

Page 7: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 7/24

* >irst E=ample ▴

This is a simple e=ample& not using the drag and drop tools Windo' [email protected]

pa$ age org.o planning.tutorial.swt.helloswt; i'port org.e$lipse.swt.widgets.Display;i'port org.e$lipse.swt.widgets.Shell; pu%li$ $lass *elloS " {

pu%li$ stati$ void 'ain(String+, args) {8

Page 8: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 8/24

// -reate Display Display display = new Display(); // -reate Shell ( indow) ro' diplay Shell shell = new Shell(display);

shell.open();

while (!shell.isDisposed()) { i (!display.read#ndDispat$h()) display.sleep();

& display.dispose(); &&+ight*click on the class @elloSWT& and select +un #s5Java #pplication.

+esult:

8* !sing Windo' uilder ▴

"e=t 'e 'ill create an e=ample -or drag and drop 'ith Windo' uilder.>ile5"e'50ther ..

A

Page 9: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 9/24

This is the 'indo' o- Windo' uilder design. %t allo's ou to drag and drop the 'idgets easil .

;

Page 10: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 10/24

A* SWT Widget ▴

A./* 0vervie' ▴

This is the hierarch o- 'idgets in SWT.

2ou can vie' the demo o- the ,ontrol at the link belo'& it?s +WT ,ontrol& but the areessentiall the same as SWT control.Demo: http:55rap.eclipsesource.com5demo5release5controls5

/B

Page 11: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 11/24

A.)* Widgets can contain other 'idgets (,ontainer$ ▴

A.3* ,ontrols ▴

;* SWT a out ▴

;./* What is a outC ▴

Put simpl & a out is ho' to arrange the components on the inter-ace.

The standard la out classes in the SWT librar are:>ill a out la s out e ual*si ed 'idgets in a single ro' or column

+o' a out la s out 'idgets in a ro' or ro's& 'ith -ill& 'rap& and spacing options7rid a out la s out 'idgets in a grid;.)* 0nline E=ample ▴

This is an e=ample online& allo'ing ou to see ho' the e=ercise o- the a out.

//

Page 12: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 12/24

;.3* >ill a out ▴

>ill a out is the simplest la out class. %t la s out 'idgets in a single ro' or column& -orcing themto be the same si e. %nitiall & the 'idgets 'ill all be as tall as the tallest 'idget& and as 'ide asthe 'idest. >ill a out does not 'rap& and ou cannot speci- margins or spacing./)3

>ill a out -ill a out F ne' >ill a out($G-ill a out.t pe F SWT.HE+T%,# Gshell.set a out(-ill a out$G

%nitial #-ter resi e

-ill a out.t pe F SWT.@0+%I0"T#(de-ault$

-ill a out.t pe F SWT.HE+T%,#

FillLayoutExample.javapa$ age org.o planning.tutorial.swt.layout; i'port org.e$lipse.swt.S ";i'port org.e$lipse.swt.layout. ill ayout;i'port org.e$lipse.swt.layout.0ow ayout;i'port org.e$lipse.swt.widgets.1utton;i'port org.e$lipse.swt.widgets.-o'posite;i'port org.e$lipse.swt.widgets.Display;i'port org.e$lipse.swt.widgets.Shell; pu%li$ $lass ill ayout2xa'ple {

pu%li$ stati$ void 'ain(String+, args) { Display display = new Display(); inal Shell shell = new Shell(display); shell.set ayout(new ill ayout());

// -o'posite parent = new -o'posite(shell3 S ".4O42);

ill ayout ill ayout= new ill ayout(); ill ayout.type= S ".520"6-# ;

parent.set ayout( ill ayout);

1utton %7 = new 1utton(parent3 S ".4O42); %7.set"ext(8178);

1utton %9 = new 1utton(parent3 S ".4O42); %9.set"ext(8198);

1utton %utton: = new 1utton(parent3 S ".4O42); %utton:.set"ext(81utton :8);

// indows %a$ to natural si e. shell.pa$ (); // shell.open(); while (!shell.isDisposed()) { i (!display.read#ndDispat$h()) display.sleep(); & // tear down the S " window

/)

Page 13: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 13/24

display.dispose(); &&+un result:

;.4* +o' a out ▴

+o' a out is more commonl used than >ill a out because o- its abilit to 'rap& and becauseit provides con-igurable margins and spacing. +o' a out has a number o- con-iguration -ields.%n addition& the height and 'idth o- each 'idget in a +o' a out can be speci-ied b setting a+o'Data ob1ect into the 'idget using set a outData.The -ield con-iguration: Wrap, Pack, Justify:

%nitial #-ter resi e

'rap F true

pack F true 1usti- F -alse (de-aults$

'rap F -alse (clips i- not enough space$

pack F -alse (all 'idgets are the same si e$

1usti- F true ('idgets are spread across theavailable space$

argin e-t& arginTop& argin+ight& argin ottom& Spacing:These -ields control the number o- pi=els bet'een 'idgets (spacing$ and the number o- pi=elsbet'een a 'idget and the side o- the parent ,omposite (margin$. de-ault& +o' a outs leave3 pi=els -or margin and spacing. The margin and spacing -ields are sho'n in the -ollo'ing

diagram.

9.5- ri!Layout ▴

7rid a out is the most use-ul and po'er-ul o- the standard la outs& but it is also the mostcomplicated. With a 7rid a out& the 'idget children o- a ,omposite are laid out in a grid.7rid a out has a number o- con-iguration -ields& and& like +o' a out& the 'idgets it la s out

can have an associated la out data ob1ect& called 7ridData. The po'er o- 7rid a out lies in theabilit to con-igure 7ridData -or each 'idget controlled b the 7rid a out.The con-iguration o- the 7rid a out:

"um,olumns

/3

Page 14: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 14/24

ake,olumnsE ualWidth

9."- #tackLayout ▴

This a out stacks all the controls one on top o- the other and resi es all controls to have thesame si e and location. The control speci-ied in top,ontrol is visible and all other controls arenot visible. !sers must set the top,ontrol value to -lip bet'een the visible items and then callla out($ on the composite 'hich has the Stack a out.

9.$- %om&i'e Layout ▴

#bove& 'e have become -amiliar 'ith the standard la out. ,ombining di--erent la out& and the

other container (,omposite& Tab>older& Sash>orm& ..$ 'ill create the desired inter-ace./B* Write the class e=tending -rom the SWT 'idget ▴

Sometimes ou need to 'rite a class e=tending -rom available 'idget class o- SWT. %t isper-ectl normal& but there is a small note& ou need to override the method checkSubclass($'ithout having to do an thing in that method.

/4

Page 15: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 15/24

pa$ age org.o planning.tutorial.swt.swtsu%$lass; i'port org.e$lipse.swt.widgets.1utton;i'port org.e$lipse.swt.widgets.-o'posite; pu%li$ $lass <y1utton extends 1utton {

pu%li$ <y1utton(-o'posite parent3 int style) { super(parent3 style); &

// ou have to override this 'ethod.

>Override prote$ted void $he$ Su%$lass() { // 4o need to do anything. &

&//* odular component inter-aces ▴

%n case ou have designed a comple= inter-ace. The splitting o- designs is necessar and thenput together& this 'ill be easier i- designing on Windo' uilder.Please see the -ollo'ing inter-ace& and 'e 'ill tr to split it up.Suppose that ou 'ant to design an inter-ace like the illustration belo'. (%t is not complicated tosplit the design& but this is an e=ample to illustrate ho' to split inter-ace design$

We can design t'o separate ,omposite and gra-t it on ain,omposite.

/9

Page 16: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 16/24

Top,omposite>ile5"e'50ther...

/

Page 17: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 17/24

%nter-ace Design -or Top,omposite.

Top,omposite.1avapa$ age org.o planning.tutorial.swt.'odule; i'port org.e$lipse.swt.widgets.-o'posite; pu%li$ $lass "op-o'posite extends -o'posite { private "ext text;

/?? ? -reate the $o'posite. ? >para' parent ? >para' style ?/ pu%li$ "op-o'posite(-o'posite parent3 int style) { super(parent3 style); set ayout(new ill ayout(S ".*O06@O4"# ));

-o'posite $o'posite = new -o'posite(this3 S ".4O42); $o'posite.set ayout(new Arid ayout(73 alse));

1utton %tnBre erredSite = new 1utton($o'posite3 S ".-*2-C); %tnBre erredSite.set"ext(8Bre erred Site8);

a%el l%l-olu'n idth = new a%el($o'posite3 S ".4O42); l%l-olu'n idth.set"ext(8-olu'n width8);

text = new "ext($o'posite3 S ".1O0D20); text.set ayoutData(new AridData(S ". 6 3 S ".-24"203 true3 alse3 737));

&

>Override prote$ted void $he$ Su%$lass() { &&

ottom,omposite

/8

Page 18: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 18/24

Similarl & create class ottom,omposite

%nter-ace Design -or ottom,omposite:

(ottom%omposite.javapa$ age org.o planning.tutorial.swt.'odule;

i'port org.e$lipse.swt.S "; pu%li$ $lass 1otto'-o'posite extends -o'posite { private "ext text;

/??

/A

Page 19: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 19/24

? -reate the $o'posite. ? >para' parent ? >para' style ?/ pu%li$ 1otto'-o'posite(-o'posite parent3 int style) { super(parent3 style); set ayout(new ill ayout(S ".*O06@O4"# ));

-o'posite $o'posite = new -o'posite(this3 S ".4O42); $o'posite.set ayout(new Arid ayout(73 alse));

-o'posite $o'posite 7 = new -o'posite($o'posite3 S ".4O42);

Arid ayout gl $o'posite 7 = new Arid ayout(:3 alse); gl $o'posite 7.'argin*eight = E; gl $o'posite 7.'argin idth = E; $o'posite 7.set ayout(gl $o'posite 7); $o'posite 7.set ayoutData(new AridData(S ". 6 3 S ".-24"203 true3 alse373 7));

1utton %tn4ew1utton = new 1utton($o'posite 73 S ".4O42); %tn4ew1utton.set ayoutData(new AridData(S ". 6 3 S ".-24"203 true3

alse3 73 7)); %tn4ew1utton.set"ext(8#dd8);

1utton %tn4ew1utton 7 = new 1utton($o'posite 73 S ".4O42); %tn4ew1utton 7.set ayoutData(new AridData(S ". 6 3 S ".-24"203 true3

alse3 73 7)); %tn4ew1utton 7.set"ext(8Delete8);

1utton %tn4ew1utton 9 = new 1utton($o'posite 73 S ".4O42); %tn4ew1utton 9.set ayoutData(new AridData(S ". 6 3 S ".-24"203 true3

alse3 73 7)); %tn4ew1utton 9.set"ext(8-lear8);

text = new "ext($o'posite3 S ".1O0D20 F S ".<G "6);

text.set ayoutData(new AridData(S ". 6 3 S ". 6 3 true3 true3 73 7));

&

>Override prote$ted void $he$ Su%$lass() { // Disa%le the $he$ that prevents su%$lassing o S " $o'ponents & &

ain,ompositeSimilarl create class ain,omposite:

/;

Page 20: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 20/24

+egister Top,omposite < ottom,omposite into Palette+ight*click the Palette and choose #dd categor ...

"amed Pallete ,ategor : ,omposite

)B

Page 21: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 21/24

+ight*click on the ,ategor Pallette 6 ,omposite6 to add Top,omposite < ottom,omposite.

)/

Page 22: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 22/24

))

Page 23: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 23/24

Similarl add ottom,omposite to 6 ,omposite6 catalog.

"o' Top,omposite < ottom,omposite are easil drag and drop on the other ,omposite.

ain,omposite.1avapa$ age org.o planning.tutorial.swt.'odule; i'port org.e$lipse.swt.S ";i'port org.e$lipse.swt.layout. ill ayout;i'port org.e$lipse.swt.layout.AridData;i'port org.e$lipse.swt.layout.Arid ayout;i'port org.e$lipse.swt.widgets.-o'posite; pu%li$ $lass <ain-o'posite extends -o'posite {

/?? ? -reate the $o'posite. ? >para' parent ? >para' style ?/ pu%li$ <ain-o'posite(-o'posite parent3 int style) { super(parent3 style); set ayout(new ill ayout(S ".*O06@O4"# ));

)3

Page 24: Programming Java Desktop application using SWT.pdf

8/9/2019 Programming Java Desktop application using SWT.pdf

http://slidepdf.com/reader/full/programming-java-desktop-application-using-swtpdf 24/24