Changes

Jump to: navigation, search

HelloSWT Application Using Eclipse

1,790 bytes added, 19:52, 27 February 2011
Created page with '== HelloSWT Application Using Eclipse == This example demonstrates how to build a simple Hello SWT example GUI using Eclipse. * Start Eclipse and open the Java perspective. * Cre…'
== HelloSWT Application Using Eclipse ==
This example demonstrates how to build a simple Hello SWT example GUI using Eclipse.
* Start Eclipse and open the Java perspective.
* Create a new Java project. Name it HelloSWT. Click Next.
* Open the Libraries tab and click Add Variable.
* Click Configure Variables.
* Click New.
* Set the name to LIB_SWT.
* Click File.
* From the Places menu, select File System.
* Go to /usr/lib64/eclipse directory.
* Select swt.jar and click Ok.
* Click Ok again.
* You should see LIB_SWT set as a classpath variable.
* Click Ok.
* Then click Finish.

You should end up with this:
[[Image:swt-1.png|left|thumb]]<br style="clear:left;" />

Now add a new class to your project. Call this class HelloSWT, set the package name to ca.on.senecac.scs, select "public static void main" and paste in this code.

<source lang="java">
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class HelloSWT {

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);

Text helloSWT = new Text(shell, SWT.NONE);
helloSWT.setText("Hello SWT");
helloSWT.pack();

shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
</source>
You should now have something like this.
[[Image:swt-2.png|left|thumb]]<br style="clear:left;" />
Finally, save your work and then run the project as a Java application.
You should get this small window showing somewhere on your desktop.
[[Image:swt-3.png|left|thumb]]<br style="clear:left;" />
Congratulations, you're done!
63
edits

Navigation menu