Java After Hours: 10 Projects Youll Never Do at Work
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.
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.
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. |