Eclipse

SWT Examples

SWT Examples

SWT Examples are useful little programs that are written in SWT. These are typically much larger and more comprehensive than SWT Snippets.

The following SWT Examples can be downloaded from the Eclipse download page:

    ControlExample on Vista
  • ControlExample
  • CustomControlExample
  • AddressBook
  • BrowserExample
  • ClipboardExample
  • DNDExample (Drag and Drop)
  • FileViewer
  • GraphicsExample
  • HelloWorld [1-5]
  • HoverHelp
  • ImageAnalyzer
  • JavaViewer
  • LayoutExample
  • PaintExample
  • TextEditor
  • OLEExample (win32 only, OLE)
  • OleWebBrowser (win32 only, OLE)

ControlExample, CustomControlExample, LayoutExample, and PaintExample are also available as Eclipse plugins, and the following examples are only available as Eclipse plugins:

  • BrowserDemo in the org.eclipse.swt.examples.browser.demos project
  • WebBrowser in the org.eclipse.swt.examples.browser project

There are several ways to download and run the SWT examples:

Running the SWT Examples inside eclipse
  1. To get the examples, go to the eclipse download page: http://download.eclipse.org/eclipse/downloads/index.php
  2. Click on the eclipse build that you would like examples for (i.e. the eclipse build that you are running; typically the latest Stable Build).
  3. Scroll down until you see "Example Plug-ins". Read the paragraph on installing them, and select the download for your platform.
  4. After installing the examples, the source for them will be in a src.zip file in the appropriate subdirectory of: eclipse/plugins/org.eclipse.sdk.examples.source_<version>/src/
    For example, the 3.1 source for the standalone examples in org.eclipse.swt.examples is in: org.eclipse.sdk.examples.source_3.1.0/src/org.eclipse.swt.examples_3.1.0/swtexamplessrc.zip.

  5. The examples in package org.eclipse.swt.examples run standalone, and the examples in org.eclipse.swt.examples.* packages are eclipse plug-ins.
  6. To run the plug-in SWT examples, restart eclipse, go to Window > Show View... > Other... and expand "SWT Examples".

Alternatively, you can use these (more detailed) steps from the eclipse ISV doc to download, install, and run the eclipse SWT Examples:

  1. Installing the examples
  2. Running the SWT examples as views or with the SWT Example Launcher
  3. Running the SWT standalone examples
  4. SWT Examples Overview

Launching standalone SWT Examples inside eclipse

  1. Load SWT into your workspace.
  2. Load the SWT examples into your workspace by one of the methods listed above.
  3. Run a standalone example by selecting the main class (e.g. org.eclipse.swt.examples.controlexample.ControlExample) and selecting
    Run > Run As > Java Application (Note: prior to Eclipse 3.3 use Run > Run As > SWT Application).
Running standalone SWT Examples outside of eclipse

To run the ControlExample and CustomControlExample standalone: (NOTE: these instructions will only work for eclipse 3.1 and newer).

  1. Go to the download page for the eclipse version you are running. For example, for eclipse 3.4.2 this is:
    http://download.eclipse.org/eclipse/downloads/drops/R-3.4.2-200902111700/index.php.
  2. Scroll down to the "Example Plug-ins" section, download the examples zip for your platform, and unzip it to the same place that you originally unzipped eclipse (answer Yes when it asks if you want to overwrite the license files). Let's say that you unzip both eclipse and its examples to d:\
  3. (NOTE: If you are using eclipse 3.3 or newer then you can skip this step)
    Open the file d:\eclipse\plugins\org.eclipse.swt.win32.win32.x86_3.X.X.jar in winzip and extract the following 3 files into d:\eclipse\plugins
    • swt-awt-win32-XXXX.dll
    • swt-gdip-win32-XXXX.dll
    • swt-win32-XXXX.dll
  4. Open a DOS window (on Windows) or a terminal window (on Mac or Linux) and navigate to the examples plugin directory, for example on Windows, type:
    d:
    cd d:\eclipse\plugins\org.eclipse.swt.examples_3.X.X
  5. To run the example, type the following line: (NOTE: Change the "win32.win32.x86" to your platform).
    <pathToYourJRE>\bin\java -classpath .;..\org.eclipse.swt_3.X.X.jar;..\org.eclipse.swt.win32.win32.x86_3.X.X.jar;.\swtexamples.jar org.eclipse.swt.examples.controlexample.ControlExample
    (NOTE: If you are using eclipse 3.2.2 or earlier, you also need -Djava.library.path=..)

    This is the ControlExample. There is a tab for each control, and you can change the various styles, etc, to see what changes in the controls. To run the CustomControlExample simply use the class name CustomControlExample instead of ControlExample in step 5.