Run the “Hello, World” SWT application

Launch the eclipse.exe application by double-clicking on it. Click on the green checkmark to go to Tutorials. Click on Create a Hello World SWT application.

The right panel contains series of nine steps marked with isosceles right triangles. Here are hints to help you follow them.

  1. Download SWT standalone.
    Go to http://www.eclipse.org/downloads/. In the Popular Projects box on the right, click on 4. Standard Widget Toolkit (SWT) to go to the SWT page.
    ReleasesStableWindows (or OS X) → BuffaloSave to Disk.
    An swt-….zip icon will appear on the desktop. Don’t open it.
  2. Import the SWT project.
    FileImport…
    In the Import window, open the General folder by clicking on the minus sign (Mac users, click on the triangle). Double-click on Existing Projects into Workspace. In the Import Projects window, press the Select archive file: radio button. To its right, press the Browse… button. Browse to the swt-….zip file that you just downloaded to your desktop, and select it. Press the Open button. When you return to the Import Projects window, press the Finish button. The project org.eclipse.swt should appear in the Package Explorer. Also, a org.eclipse.swt folder will appear in your workspace folder.
  3. Configure the Java project.
    On a PC, “Right-clicking on the project” means to right-click on the word HelloWorldSWT in the Package Explorer. On a Mac OSX, it means to hold down the control key as you click on HelloWorldSWT, and select Properties. When adding org.eclipse.swt, you must check the checkbox in front of it in the Required Project Selection window. Then press the OK button in that window, and the OK button in the Properties for HelloWorldSWT/Java Build Path window.
  4. Create a class.
    The source folder for your new Java class should be the same as the class name, HelloWorldSWT. Remember to check the checkbox for
    public static void main(String[] args).
    Ignore the message that says “The use of the default package is discouraged.” A HelloWorldSWT.java file should appear in the Java editor.
  5. Write the Java code.
    Paste the following statements into the HelloWorldSWT.java file in the Java editor. Pay attention to uppercase vs. lowercase. The exclamation point means “not”.
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Hello world!");
    shell.open();
    
    while (!shell.isDisposed()) {
    	if (!display.readAndDispatch()) {
    		display.sleep();
    	}
    }
    
    display.dispose();
    

    When you have pasted in the statements, the words Display and Shell (two copies of each) should be underlined in zigzag red. Select SourceOrganize Imports to add the following two import statements to your HelloWorldSWT.java file above your class.

    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    
    import declarations will also appear in the Outline.

    Ambitious people can insert the following immediately below the shell.open(); statement. If SourceOrganize Imports asks you which type of Label to import, select org.eclipse.swt.widgets.Label.

    Label label = new Label(shell, SWT.BORDER);
    label.setText("This is a label.");
    label.setBounds(20, 20, 100, 40);
    
  6. Run your Java application.
    This time, the application will create a window instead of writing to the console.