5
|
1 |
<?xml version="1.0" encoding="UTF-8"?>
|
|
2 |
<cheatsheet title="Create a Hello World SWT application">
|
|
3 |
<intro href="/org.eclipse.platform.doc.user/reference/ref-cheatsheets.htm">
|
|
4 |
<description>
|
|
5 |
This cheat sheet shows you how to create a "Hello World" application
|
|
6 |
that uses the Standard Widget Toolkit (SWT). The application will
|
|
7 |
simply display an empty window to the user.<br/>
|
|
8 |
<br/>
|
|
9 |
If you need help at any step, click the (?) to the right. Let's get
|
|
10 |
started!
|
|
11 |
</description>
|
|
12 |
</intro>
|
|
13 |
<item title="Open the Java perspective" dialog="true" skip="false" href="/org.eclipse.platform.doc.user/concepts/concepts-4.htm">
|
|
14 |
<description>
|
|
15 |
If you're not already in the Java perspective, in the main menu
|
|
16 |
select <b>Window</b> > <b>Open Perspective</b> > <b>Java</b>
|
|
17 |
or click on the "Click to Perform" link below.
|
|
18 |
</description>
|
|
19 |
<command serialization="org.eclipse.ui.perspectives.showPerspective(org.eclipse.ui.perspectives.showPerspective.perspectiveId=org.eclipse.jdt.ui.JavaPerspective)" required="false" translate="">
|
|
20 |
</command>
|
|
21 |
</item>
|
|
22 |
<item title="Download SWT standalone" dialog="true" skip="false" href="/org.eclipse.platform.doc.isv/guide/swt.htm">
|
|
23 |
<description>
|
|
24 |
Open your Web browser to http://download.eclipse.org/eclipse/downloads/,
|
|
25 |
select the latest release build, and find the <b>SWT Binary and Source</b>
|
|
26 |
download.<br/>
|
|
27 |
<br/>
|
|
28 |
Save the file to your disk; you do not need to extract the archive.
|
|
29 |
</description>
|
|
30 |
</item>
|
|
31 |
<item title="Import the SWT project" dialog="true" skip="false" href="/org.eclipse.platform.doc.user/tasks/tasks-importproject.htm">
|
|
32 |
<description>
|
|
33 |
Import the SWT project from the main menu via File > Import...,
|
|
34 |
and select <b>Existing Projects into Workspace</b>. Specify the
|
|
35 |
<b>archive</b> file you downloaded and click <b>Finish</b>.<br/>
|
|
36 |
<br/>
|
|
37 |
This will create the org.eclipse.swt project which we will need
|
|
38 |
to compile and run the application.
|
|
39 |
</description>
|
|
40 |
<command serialization="org.eclipse.ui.file.import(importWizardId=org.eclipse.ui.wizards.import.ExternalProject)" required="false" translate="">
|
|
41 |
</command>
|
|
42 |
</item>
|
|
43 |
<item title="Create a Java project" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/concepts/concepts-3.htm">
|
|
44 |
<description>
|
|
45 |
Now we need a project to store our own source code. In the main
|
|
46 |
toolbar, click on the <b>New Java Project</b> button, or click on
|
|
47 |
the link below. Enter <b>HelloWorldSWT</b> for the project name,
|
|
48 |
then click <b>Finish</b>.
|
|
49 |
</description>
|
|
50 |
<command serialization="org.eclipse.ui.newWizard(newWizardId=org.eclipse.jdt.ui.wizards.JavaProjectWizard)" required="false" translate="">
|
|
51 |
</command>
|
|
52 |
</item>
|
|
53 |
<item title="Configure the Java project" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/reference/ref-123.htm">
|
|
54 |
<description>
|
|
55 |
Since our project requires SWT, we need to specify this in the
|
|
56 |
project properties. Right-click on the project and select
|
|
57 |
<b>Properties</b>.<br/>
|
|
58 |
<br/>
|
|
59 |
In the <b>Java Build Path</b> page open the <b>Projects</b> tab,
|
|
60 |
add the org.eclipse.swt project, then click <b>OK</b>.
|
|
61 |
</description>
|
|
62 |
</item>
|
|
63 |
<item title="Create a class" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/gettingStarted/qs-9.htm">
|
|
64 |
<description>
|
|
65 |
The next step is to create a new class. In the main toolbar,
|
|
66 |
click on the <b>New Java Class</b> button (or the link below).
|
|
67 |
If not already specified, select <b>HelloWorldSWT/src</b> as the source folder. Enter <b>HelloWorldSWT</b> for the class name and select the checkbox to
|
|
68 |
create the <b>main()</b> method, then click <b>Finish</b>.<br/>
|
|
69 |
<br/>
|
|
70 |
The Java editor will automatically open showing your new class.
|
|
71 |
</description>
|
|
72 |
<command serialization="org.eclipse.ui.newWizard(newWizardId=org.eclipse.jdt.ui.wizards.NewClassCreationWizard)" required="false" translate="">
|
|
73 |
</command>
|
|
74 |
</item>
|
|
75 |
<item title="Write the Java code" dialog="true" skip="false" href="/org.eclipse.platform.doc.isv/guide/swt_widgets.htm">
|
|
76 |
<description>
|
|
77 |
In the Java editor, enter the following Java code in the
|
|
78 |
<b>main()</b> method:<br/>
|
|
79 |
<br/>
|
|
80 |
Display display = new Display();<br/>
|
|
81 |
Shell shell = new Shell(display);<br/>
|
|
82 |
shell.setText("Hello world!");<br/>
|
|
83 |
shell.open();<br/>
|
|
84 |
while (!shell.isDisposed()) {<br/>
|
|
85 |
if (!display.readAndDispatch()) display.sleep();<br/>
|
|
86 |
}<br/>
|
|
87 |
display.dispose();<br/>
|
|
88 |
<br/>
|
|
89 |
You will get <b>compile errors</b>. Right click in the Java editor
|
|
90 |
and select Source > Organize Imports, then <b>save</b> your
|
|
91 |
changes.
|
|
92 |
</description>
|
|
93 |
</item>
|
|
94 |
<item title="Run your Java application" dialog="true" skip="false" href="/org.eclipse.jdt.doc.user/gettingStarted/qs-12.htm">
|
|
95 |
<description>
|
|
96 |
To <b>run</b> your application, right-click on your class in the
|
|
97 |
Package Explorer and select <b>Run As</b> > <b>Java
|
|
98 |
Application</b>. A new empty window should appear with the
|
|
99 |
title "Hello world!".<br/>
|
|
100 |
<br/>
|
|
101 |
Congratulations! You have successfully created a Hello World SWT
|
|
102 |
application!
|
|
103 |
</description>
|
|
104 |
</item>
|
|
105 |
</cheatsheet>
|