Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code
more resources

Eclipse SWT 101
Never mind the prevailing GUI toolkit controversy. The portable Eclipse Standard Widget Toolkit is a simple solution for adaptable interfaces
by Kevin Jones

April 21, 2005

When Eclipse was first introduced to the world, one of its most contentious features was the Eclipse Standard Widget Toolkit (SWT), which is another toolkit for building user interfaces with Java. It's considered contentious because it competes directly with the official JDK toolkit: Swing. Let's get introduced to SWT and see how it can be used to create user interfaces that mirror the look and feel of the target platform.

ADVERTISEMENT

SWT is a portable user interface toolkit meant to provide efficient access to the user interface facilities of the underlying operating system (see Resources). To achieve this portability, SWT provides a Java API that is the same on all platforms. As an SWT programmer, you shouldn't care about the platform on which your application runs. To achieve efficiency, SWT components use JNI to simply pass calls to the Java components onto the API that the underlying windowing system uses. The upside is that the Java application looks and feels like an application written for that windowing system, be it Windows, GTK, or Carbon. The downside is that the toolkit has to be ported to each platform it supports. While this downside is a limitation at the moment, there is support for eleven platforms.

Before we begin writing SWT code let's take a quick look at setup. An SWT application requires at least two libraries: swt.jar and the native SWT shared library for your operating system. On Windows the shared library is swt-win32-####.dll where #### is the build number. On my machine, where I'm running Eclipse 3.1, the JAR is in %ECLIPSE_HOME%\plugins\org.eclipse.swt.win32_3.1.0\ ws\win32, and the shared libraries are in %ECLIPSE_HOME%\eclipse\plugins\org.eclipse.swt.win32_3.1.0\os\win32\x86. When we run an Eclipse SWT application, the JAR file and the shared libraries need to be available for the application to use.

Let's start with a Hello World project in Eclipse to see how to configure Eclipse to use SWT and to see the basics of the library. In Eclipse create a new project, and once it's created, go to the project properties, select the Libraries tab, and then click the Ad External JARs button. Browse to swt.jar (in your plugins directory) and select Open to add the JAR (see Figure 1). SWT is now included in the project, and you can now build your code. However, if you try and run the application it will fail with an UnsatisfiedLinkError, which we'll fix shortly.

This article requires registration. Please login below or click here to register.
 
E-mail Address:
Password:
Remember me:
 



Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home