By Tim Hatton
Price: $29.95 USD
£20.95 GBP
Cover | Table of Contents | Colophon
http://www.eclipse.org/downloads/index.php
online for the SWT download (the web page is shown in Figure 1-1).
Notepad or vi, with no changes necessary to the code.Notepad or vi,
do this by specifying command-line parameters at compile and
execution time, as shown in Figure 1-4 and
Figure 1-5.
javac know
where to find the SWT libraries so that it can compile your
applications.
java is
-Djava.library.path=
pathtolibrary,
substituting the actual path to the location where you extracted the
SWT library files.
SWT) and the path to the location of the
swt.jar file. Click OK to return to the dialog
shown in Figure 1-11, with your new entry displayed.
Display
and Shell.
Display is the class responsible for managing the
interaction between all SWT widgets and the underlying operating
system. It is in Display that you find methods
that enable you to directly query the operating system for
information about things such as which control currently has the
focus and what windows are currently open and attached to the
display. You will not need to interact directly with the display very
often.Shell, is much more important to
the programmer. Instances of Shell represent
windows which are currently being managed by the desktop (on MS
Windows) or the windows manager (on Unix or Linux systems). Shells
can be created either directly on the display, or within the confines
of a parent shell. In this chapter, you will learn to develop code
that creates both types of shells.Display class.Display to the
Shell constructor to create an instance of the
Shell class.import org.eclipse.swt.widgets.*;
public class SimpleShell {
SimpleShell( ) {
Display d = new Display( );
Shell s = new Shell(d);
s.setSize(500,500);
s.open( );
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
main( )
method in each example.Runner, shown in Example 2-2.public class Runner {
public static void main(String[] args){
SimpleShell ss = new SimpleShell( );
}
}
Runner class (which is a bit
of a pain, admittedly).main()
method in each example class you
create, like this:import org.eclipse.swt.widgets.*;
public class SimpleShell {
// Class body from previous lab
public static void main(String [] args)
{
SimpleShell ss = new SimpleShell( );
}
}
Runner class method
so that I can keep the example code clean of code not directly
related to the technique being demonstrated.main( ) method you must create a
Run Configuration. To do this, select
Run Run from the Eclipse menu. This invokes the dialog shown in Figure 2-2.
Runner class on the Main tab, then check the
Arguments tab to make certain you passed the location of the SWT
native library to the JRE, as you learned to do in Chapter 1.Shell class, you do
that by using a second constructor that enables you to pass a
style—
an integer value the class uses to determine what attributes to show
or hide.SWT. The SWT
class is
located in the
org.eclipse.swt
package. For shells, the enumerated
values are BORDER
,
CLOSE, MIN,
MAX, NO_TRIM,
RESIZE, and TITLE. Also, two
convenience
values—SHELL_TRIM
and
DIALOG_TRIM
—combine several of the style attributes
to create two common looks for windows.Shell
class
has multiple constructors. You utilized one of those earlier when you
created an instance of your first shell:Shell s = new Shell(d);
Display class as an argument. To open an instance
of Shell
that accepts a style value, you must
use a different constructor:Shell(Display display, int style)
int
value to control all the attributes. This means that
you must have some way
to combine the enumerated values in any combination you require. In
Java programming, you can combine these values using the
| operator, as follows:Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);
SHELL_TRIM or DIALOG_TRIM, to
specify the two most common combinations.SimpleShell example and pass the
Shell constructor a different combination of
styles.SimpleShell class, modified to specify a window
that has a close button, has no min or max button, and is resizable.import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class ShellStylesExample {
ShellStyles( ) {
Display d = new Display( );
Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);
s.setSize(300,300);
s.open( );
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
SimpleShell example is that the second constructor
form is used and the style parameter is used to specify the desired
window attributes.Runner
program to load up an instance
of ShellStylesExample, as demonstrated in
Example 2-4
.public class Runner {
public static void main(String[] args){
ShellStylesExample sse = new ShellStylesExample ( );
}
}
Runner class. You should have the idea down pat by
now.ShellStylesExample is as shown in
Figure 2-3. You see only a close button displayed
on the titlebar and the window is resizable.
MessageBox and other common forms of dialogs are
created from another set of SWT classes and are discussed in Chapter 17.Shell class:Shell(Shell parent)
Shell(Shell parent, int style)
Shell that is suitable for opening within the
confines of a parent shell.import org.eclipse.swt.widgets.*;
public class ChildShell {
ChildShell(Shell parent
){
Shell child = new Shell(parent);
child.setSize(200,200);
child.open( );
}
}
ChildShell constructor enables you to pass in
a reference to the shell that will serve as the parent:ChildShell(Shell parent)
Shell
constructor that accepts a reference to another instance of
Shell as a parent rather than as a
Display reference:Shell child = new Shell(parent);
childChildShell, using
ChildShellExample as the parent, simply by
creating new instances of ChildShell.ChildShell for each window desired.import org.eclipse.swt.widgets.*;
public class ChildShellExample {
Display d = new Display( );
ChildShellExample( ) {
Shell s = new Shell(d);
s.setSize(500,500);
s.open( );
ChildShell cs1 = new ChildShell(s);
ChildShell cs2 = new ChildShell(s);
ChildShell cs3 = new ChildShell(s);
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
ChildShellExample
demonstrated how to open a child
window within the confines of a parent window, but this is not the
same as opening a true dialog. A dialog window is one that halts
processing of code in the parent window until the user takes some
action in the dialog. In ChildShellExample, the
parent window code continued to execute even while the child window
was opened. You can see the effect if you execute Example 2-8.import org.eclipse.swt.widgets.*;
public class ChildShellExample {
Display d = new Display( );
ChildShellExample( ) {
Shell s = new Shell(d);
s.setSize(500,500);
s.open( );
ChildShell cs1 = new ChildShell(s);
System.out.println("Execution Continues");
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
ChildShellExample,
you see that the message is printed to the Console immediately after
the child window is opened. What if you need to wait for the user to
take some action in the child window before knowing how to proceed?
For this, you must use a special form of window known as a
dialog. The SWT provides the capability to work
with dialogs in the form of the
Dialog
class.Dialog class enables you to create custom
dialogs—those on which you can place any widget you desire. A
dialog is simply another type of shell, except
that it extends the Dialog class, which
encapsulates some additional methods and accepts additional style
attributes.Dialog class has two style attributes:
SWT.APPLICATION_MODAL
and
SWT.SYSTEM_MODAL
.
APPLICATION_MODAL, as the name implies, will cause
the dialog to halt all processing in the application until the dialog
is dismissed. setText( )
method
on the Shell class:s.setText("A Shell Example");
ShellDialogExample, you notice that there is a
generic "window" icon on the right
side of the titlebar. Although this icon does not appear on all
platforms, when it does appear the user will almost certainly expect
that it reflect the type of application and not be the generic
"window" icon.setImage( )
method
of the Shell class to specify the image you wish
to display, as shown in Example 2-11.import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class ProfessionalShell {
ProfessionalShell( ) {
Display d = new Display( );
Shell s = new Shell(d);
s.setSize(500,500);
s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
s.setText("A Shell Example");
s.open( );
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
ProfessionalShell
generates Figure 2-7, showing a distinct
improvement over the generic
"window" icon from previous
examples.
Shell class is
only the first step toward generating a professional GUI that users
will enjoy using. Most GUIs contain many other elements that users
will expect to see in your applications. These include menus, which
are discussed in the next chapter, and toolbars, which are examined
in Chapter 4.Menu and MenuItem,
both located in the org.eclipse.swt.widgets
package.Menu
and MenuItem classes.
Menu is a container class that holds other menus
and menu items. Menu represents the highest level
of any menu system, the menu bar that appears just below the title of
the window. Instances of Menu to serve as the menu
bar and attach it to an instance of the Shell
class.import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class MenuShell {
Display d;
Shell s;
MenuShell( )
{
d = new Display( );
s = new Shell(d);
s.setSize(500,500);
s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
s.setText("A Shell Menu Example");
Menu m = new Menu(s,SWT.BAR );
s.setMenuBar(m);
s.open( );
while(!s.isDisposed( )){
if(!d.readAndDispatch( ))
d.sleep( );
}
d.dispose( );
}
}
Menu m = new Menu(s,SWT.BAR ); s.setMenuBar(m);
Menu, passing it a reference to its containing
Shell, and specifying the
SWT.BAR
style. The menu is then attached to the
window by calling the setMenuBar()
method of the Shell class.Menu
class know whether it serves as a menu bar or a high-level menu item
such as File? You specify the type of menu created
by passing a style attribute to the Menu
constructor. As with Shell, the style attributes
are specified in the SWT
class as enumerated values. For
Menu, there are three values to choose
from—SWT.BAR,
SWT.DROP_DOWN, and
SWT.POP_UP
.MenuShell
won't display a
menu system because no high-level menus
have been created and added to the menu bar. To do that, you need to
create instances of the MenuItem class using the
SWT.CASCADE style.MenuItem
class for each desired drop down and
set the text attribute of each such instance to represent the
clickable text the user will see. These instances of
MenuItem are what the user will see running across
the menu bar (e.g., File). Instances of MenuItem
are attached directly to the menu bar by passing a reference to the
menu bar in the
MenuItem
constructor. These
MenuItem objects must be given the
SWT.CASCADE
style. The setText(
)
method is called to specify the text you
wish to appear on the menu:MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
Menu
instance to attach to the
cascading menu item. This instance of Menu is the
container for the individual menu items that appear when the user
clicks the cascading menu (i.e., when the menu drops down). This time
you pass the Menu constructor the
SWT.DROP_DOWN
style in
addition to a reference to the containing Shell:Menu filemenu = new Menu(s, SWT.DROP_DOWN); file.setMenu(filemenu);
MenuItem to add to
the DROP_DOWN style menu,
passing them a reference to the containing Menu
instance and specifying the SWT.PUSH
style (one of
the other available MenuItem styles):MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
openItem.setText("Open");
MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
SWT.CHECK
,
MenuItem
that has been given the
SWT.SEPARATOR style:MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class MenuShell {
Display d;
Shell s;
MenuShell( ) {
d = new Display( );
s = new Shell(d);
s.setSize(300,300);
s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
s.setText("A Shell Menu Example");
Menu m = new Menu(s,SWT.BAR );
// create a File menu and add an Exit item
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
final MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
openItem.setText("Open");
final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
// create an Edit menu and add Cut, Copy, and Paste items
final MenuItem edit = new MenuItem(m, SWT.CASCADE);
edit.setText("Edit");
final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
edit.setMenu(editmenu);
final MenuItem cutItem = new MenuItem(editmenu, SWT.PUSH);
cutItem.setText("Cut");
final MenuItem copyItem = new MenuItem(editmenu, SWT.PUSH);
copyItem.setText("Copy");
final MenuItem pasteItem = new MenuItem(editmenu, SWT.PUSH);
pasteItem.setText("Paste");
//create a Window menu and add Child items
final MenuItem window = new MenuItem(m, SWT.CASCADE);
window.setText("Window");
final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
window.setMenu(windowmenu);
final MenuItem maxItem = new MenuItem(windowmenu, SWT.PUSH);
maxItem.setText("Maximize");
final MenuItem minItem = new MenuItem(windowmenu, SWT.PUSH);
minItem.setText("Minimize");
// create a Help menu and add an About item
final MenuItem help = new MenuItem(m, SWT.CASCADE);
help.setText("Help");
final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
help.setMenu(helpmenu);
final MenuItem aboutItem = new MenuItem(helpmenu, SWT.PUSH);
aboutItem.setText("About");
s.setMenuBar(m);ChildExample or DialogExample
(from Chapter 2). Such a menu system is shown
in Figure 3-3.SWT.PUSH to SWT.CASCADE, and
then create menu items to attach to it. The following code creates
menu items, with the SWT.PUSH style, with the text
Child and Dialog:final MenuItem openItem = new MenuItem(filemenu, SWT.CASCADE);
openItem.setText("Open");
final Menu submenu = new Menu(s, SWT.DROP_DOWN);
openItem.setMenu(submenu);
final MenuItem childItem = new MenuItem(submenu, SWT.PUSH);
childItem.setText("Child");
final MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
dialogItem.setText("Dialog");
MenuShell by replacing
the code that created the Open menu item with the preceding code, as
shown in Example 3-4.import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class MenuShell {
Display d;
Shell s;
MenuShell( ) {
d = new Display( );
s = new Shell(d);
s.setSize(300,300);
s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
s.setText("A Shell Menu Example");
Menu m = new Menu(s,SWT.BAR );
// create a File menu and add an Exit item
MenuShell
example, nothing happens. You must complete one more task to make
your menu items perform their intended function—you must attach
a Listener to your menu items. As the name
implies, a Listener listens for a particular event
to occur and then takes some action upon the occurrence.Listener is a class that extends one of the
Listener interfaces. When an event occurs, the
event loop for the main shell of the application dispatches that
event to be handled by the Listener attached to
the widget that is causing the event to occur. The result is that one
of the methods of the Listener will be called. It
is in those methods that you develop the code that executes when the
event occurs.Listener
interfaces, all a part of the
org.eclipse.swt.events
or
org.eclipse.swt.widgets
packages. To use a
Listener, first identify the type of event that
you are listening for, then call a method to attach that
Listener to the widget. Finally, add code to the
listener's methods to perform the desired task.SelectionListener to the
MenuItem
childItem in
MenuExample, use the following code:childItem.addSelectionListener(new SelectionListener( ) {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell)maxItem.getParent( ).getParent( );
ChildShell cs = new ChildShell(parent);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
MenuItem, has
one or more add methods that attach a
Listener to the widget. To listen for
mouse click events, add a
SelectionListener
to be called by addSelectionListener()
. You pass addSelectionListener() a reference to a class that implements the
SelectionListener interface. Usually, this is
accomplished by using an
anonymous inner class.SWT.SEPARATOR, SWT.PUSH, and
SWT.CASCADE, which you have exercised earlier,
menu items can have one of two other styles:
SWT.RADIO and SWT.CHECK.RADIO
style assigns multiple menu items to a group of items and allows only
one of those items to be selected at any given time. It is analogous
to the RADIO style for buttons that you will
examine in Chapter 5.CHECK
style simply places a check icon next to a menu item when the user
has selected that option and removes it when the user deselects that
option (by clicking the menu while it is in its checked state).final MenuItem options = new MenuItem(m, SWT.CASCADE);
options.setText("Options");
final Menu optionsmenu = new Menu(s, SWT.DROP_DOWN);
options.setMenu(optionsmenu);
final MenuItem checkItem = new MenuItem(optionsmenu, SWT.CHECK);
checkItem.setText("Checked Option");
final MenuItem optionsseparator = new MenuItem(optionsmenu, SWT.SEPARATOR);
final MenuItem radioItem1 = new MenuItem(optionsmenu, SWT.RADIO);
radioItem1.setText("Radio One");
final MenuItem radioItem2 = new MenuItem(optionsmenu, SWT.RADIO);
radioItem2.setText("Radio Two");
ShellMenu example class
results in the menu shown in Figure 3-4. You can
experiment with the menu to see the effects of clicking the various
options (see Figure 3-5 and ).
Listener to a menu item with the
SWT.CHECK or SWT.RADIO style,
you need to take extra steps to determine the state of the item.
After all, it is the state of the item that controls what action you
take in your code.Listener to a CHECK
or RADIO menu item in exactly the same manner as
you do for a regular menu item. The difference comes in what code you
write in the widgetSelected( ) method for your
Listener. Specifically, the action you take will
depend upon the state of the menu item (whether checked or
unchecked). SWT provides a method to use to determine the
menu's state. A typical Listener
looks like this:checkItem.addSelectionListener(new SelectionListener( ) {
public void widgetSelected(SelectionEvent e) {
if(checkItem.getSelection(