8.2. Creating an SWT Application
Problem
You want to create a new SWT application.
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.
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
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.