Creating an SWT Application


To create the Browser project, you're going to have to know how SWT applications work. Take a look at Figure 8.2, where you'll see just about the simplest such application. It just displays the text "An SWT label, very cool." in an SWT Label widget inside an SWT window.

Figure 8.2. An SWT application.


This application, LabelProject.java, starts importing the SWT classes it needs and creating a new LabelProject object in the main method:

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class LabelProject {         public static void main(String [] args)       {           new LabelProject();       }         .         .         . } 

In the constructor, you create a new GUI handler of the SWT Display class, and then you create a new Shell object using that Display object. It's that Shell object that corresponds to the window you see in Figure 8.2. Here's how you create these objects:

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class LabelProject {       Display display;       Shell shell;       public static void main(String [] args)       {           new LabelProject();       }       LabelProject()       {        display = new Display();        shell = new Shell(display);        shell.setSize(200, 200);         .         .         . } 

You can find the significant methods of the SWT Shell class in Table 8.1. This class is an important one; it's the basis of the browser window you see in Figure 8.1.

Table 8.1. Significant Methods of the org.eclipse.swt.widgets.Shell Class

Method

Does This

void addShellListener(ShellListener listener)

Adds the given listener to the listeners that will be notified when events occur in the widget

void close()

Closes the widget, just as if the user clicked the close button

void dispose()

Disposes of the operating system resources connected to this the widget

Rectangle getBounds()

Returns a Rectangle object holding the widget's size and location

boolean getEnabled()

Returns true if the widget is enabled. Returns false otherwise

Point getLocation()

Returns a point holding the widget's location

Region getRegion()

Returns a Region object holding the widget's location

Point getSize()

Returns a point holding the widget's size

boolean isEnabled()

Returns true if the widget is enabled. Returns false otherwise

boolean isVisible()

Returns true if the widget is visible. Returns false otherwise

void open()

Opens the widget, making it visible

void removeShellListener(ShellListener listener)

Removes the listener from the widget

void setEnabled(boolean enabled)

Enables the widget if you pass a value of true, and disables it if you pass false

void setRegion(Region region)

Sets the shape of the shell to the region you pass this method

void setVisible(boolean visible)

Makes the widget visible if you pass it a value of true. Makes the widget invisible otherwise


Now you create an SWT label to hold the text displayed in Figure 8.2. To create the label, you pass the shell object to the label's constructor. Then you can use the label's setText method to set the displayed text, and you can use the setBounds method to specify where you want it (this centers text in the label with the SWT.CENTER style):

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class LabelProject {       Display display;       Shell shell;       Label label;       public static void main(String [] args)       {           new LabelProject();       }       LabelProject()       {        display = new Display();        shell = new Shell(display);        shell.setSize(200, 200);        label = new Label(shell, SWT.CENTER);        label.setText("An SWT label, very cool.");        label.setBounds(5, 20, 180, 40);         .         .         . } 

You can find the significant methods of the SWT Label class in Table 8.2.

Table 8.2. Significant Methods of the org.eclipse.swt.widgets.Label Class

Method

Does This

Point computeSize(int wHint, int hHint, boolean changed)

Returns the preferred size of the label

int getAlignment()

Returns a constant indicating the position of text or an image in the label

Image getImage()

Returns the label's image, if it has one, or null otherwise

String getText()

Returns the label's text, if there is any, or null otherwise

void setAlignment(int alignment)

Sets how text and images should appear in the label

void setImage(Image image)

Sets the image that will appear in the label

void setText(String string)

Sets the text that will appear in the label


All that's left is to display the shell and handle the window-closing event, which happens when the user clicks the close button, by disposing of the display object:

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class LabelProject {       Display display;       Shell shell;       Label label;       public static void main(String [] args)       {           new LabelProject();       }       LabelProject()       {        display = new Display();        shell = new Shell(display);        shell.setSize(200, 200);         .         .         .        shell.open();        while(!shell.isDisposed()) {           if(!display.readAndDispatch()) display.sleep();        }        display.dispose();     } } 

That completes LabelProject.java. To compile it, make sure swt.jar is in your classpath. For example, if swt.jar is in the same directory as LabelProject.java, set the classpath this way:

 %set classpath=swt.jar;. 

Then compile LabelClass.java this way (this assumes javac is in your path; otherwise, prefix it with the correct path):

 %javac LabelClass.java 

To run the resulting LabelClass.class file, you have to tell Java where to find the SWT native code support, such as in win32-3063.dll and swt-win32-3063.dll in Windows. Assuming you've copied the needed files to the same directory where LabelClass.class is, you can execute that class file like this:

 %java -Djava.library.path=. LabelClass 

Running this application should give you the results you see in Figure 8.2.

That's the basics of an SWT applicationat least one that does nothing but displaying some text. The Browser project is going to have to do a lot more, however, such as handling SWT events such as button clicks.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net