DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 8.2 Creating an SWT Application

8.2.1 Problem

You want to create a new SWT application.

8.2.2 Solution

Import the SWT classes, create an SWT shell, and add the widgets you want to use to that shell. Then use the shell's open method to display it.

8.2.3 Discussion

In this example, we're going to create an SWT window and display text in it. To follow along, create a new Java Eclipse project named FirstSWTApp. Add a class, FirstSWTClass, in the org.cookbook.ch08 class. We'll need to import the SWT classes:

package org.cookbook.ch08;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
        .
        .
        .

In the main method, you create a new SWT Display object, and you use that object to create a Shell object that corresponds to an SWT window. Here are some of the most popular Shell methods:


void addShellListener(ShellListener listener)

Adds the listener to the collection of listeners who will be notified when operations are performed on the shell


void close( )

Closes the shell


void dispose( )

Disposes of the operating system resources associated with the shell


Rectangle getClientArea( )

Returns the shell's client area


boolean isDisposed( )

Returns true if the shell has been disposed, false otherwise


void open( )

Opens the shell on the screen


void setLocation(int x, int y)

Sets the shell's location (measurements are in pixels)


void setText(String s)

Sets the shell's titlebar text


void setSize(int width, int height)

Sets the shell's size

We'll customize the shell by setting its title and size using the setText and setSize methods:

package org.cookbook.ch08;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

public class FirstSWTClass {

    public static void main(String [] args) {
       Display display = new Display( );
       Shell shell = new Shell(display);
       shell.setText("First SWT Application");
       shell.setSize(250, 250);
        .
        .
        .

To display text, we'll create an SWT Label object, displaying in it the text Greetings from SWT. Label widgets are designed simply to display text, and you can use the setText and getText methods to work with that text. Like other widgets, you can use the setBounds method to set the bounds of a label widget; in this case, we'll make the label correspond to the shell's entire client area:

package org.cookbook.ch08;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

public class FirstSWTClass {

    public static void main(String [] args) {
       Display display = new Display( );
       Shell shell = new Shell(display);
       shell.setText("First SWT Application");
       shell.setSize(250, 250);
       Label label = new Label(shell, SWT.CENTER);
       label.setText("Greetings from SWT");
       label.setBounds(shell.getClientArea( ));
        .
        .
        .

To open the window, you call the Shell object's open method. To manage the window, you use the Shell object's isDisposed method to determine when it's been closed. If the window is still open, you call the application's message pump with the Display object's readAndDispatch method. If the shell is closed, you dispose of it with its dispose method, as shown in Example 8-3.

When you're done with a resource in SWT, you should deallocate it with the Widget.dispose method.


Example 8-3. FirstSWTClass.java
package org.cookbook.ch08;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

public class FirstSWTClass {

    public static void main(String [] args) {
       Display display = new Display( );
       Shell shell = new Shell(display);
       shell.setText("First SWT Application");
       shell.setSize(250, 250);
       Label label = new Label(shell, SWT.CENTER);
       label.setText("Greetings from SWT");
       label.setBounds(shell.getClientArea( ));
       shell.open( );
       while(!shell.isDisposed( )) {
          if(!display.readAndDispatch( )) display.sleep( );
       }
       display.dispose( );
    }
}

That completes the code, but if you enter it as it stands, you'll see a lot of squiggly red lines because we haven't added the SWT .jar file to the build path. To give Eclipse access to the SWT classes it needs, see the next recipe.

8.2.4 See Also

Recipe 8.3 on adding SWT JAR files to the build path; Recipe 8.4 on launching an SWT application; Chapter 7 of Eclipse (O'Reilly).

    Previous Section  < Day Day Up >  Next Section