< Day Day Up > |
Recipe 8.2 Creating an SWT Application8.2.1 ProblemYou want to create a new SWT application. 8.2.2 SolutionImport 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 DiscussionIn 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:
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.
Example 8-3. FirstSWTClass.javapackage 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 AlsoRecipe 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). |
< Day Day Up > |