Programming Java Desktop Application Using SWT PDF
Programming Java Desktop Application Using SWT PDF
Programming Java Desktop Application Using SWT PDF
SWT:
Workbench Application:
To create a desktop application using SWT. On the Eclipse, we will create an RCP Plugin
Project. You have 2 options.
Only use the features of the SWT
Using the platform provided by the RCP to RCP Application Workbench programming
In this document, I will guide you to become familiar with basic programming SWT, using
WindowBuilder to drag and drop components into the interface.
3- The settings required before starting
Some required settings before you start:
You need the latest version of Eclipse. There currently is Eclipse 4.4 (Codes LUNA).
http://eclipse.org/downloads/
In my opinion you to download package: "Eclipse IDE for Java EE Developers". The only
different is number of Plugins, for the purpose of different programming. You can install
additional plugins for other purposes if desired.
WindowBuilder, this is a plugin that allows you to design SWT GUI applications using drag and
drop convenience.
See installation instructions at:
http://o7planning.org/web/fe/default/en/document/5699/install-windowbuilder-into-eclipse
4- Some concepts of SWT.
4.1- Display & Shell
The Display and Shell classes are key components of SWT applications.
2
Created Project:
Add libary swt: org.eclipse.swt
6- First Example
This is a simple example, not using the drag and drop tools WindowBuilder.
HelloSWT.java
package org.o7planning.tutorial.swt.helloswt;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloSWT {
public static void main(String[] args) {
7
// Create Display
Display display = new Display();
// Create Shell (Window) from diplay
Shell shell = new Shell(display);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Result:
7- Using WindowBuilder
Next we will create an example for drag and drop with WindowBuilder.
File/New/Other ..
This is the window of WindowBuilder design. It allows you to drag and drop the widgets easily.
8- SWT Widget
8.1- Overview
This is the hierarchy of widgets in SWT.
You can view the demo of the Control at the link below, it's RWT Control, but they are
essentially the same as SWT control.
Demo: http://rap.eclipsesource.com/demo/release/controls/
10
8.3- Controls
9- SWT Layout
9.1- What is Layout?
Put simply, Layout is how to arrange the components on the interface.
11
9.3- FillLayout
FillLayout is the simplest layout class. It lays out widgets in a single row or column, forcing them
to be the same size. Initially, the widgets will all be as tall as the tallest widget, and as wide as
the widest. FillLayout does not wrap, and you cannot specify margins or spacing.
1 FillLayout fillLayout = new FillLayout();
2 fillLayout.type = SWT.VERTICAL;
3 shell.setLayout(fillLayout);
Initial
After resize
fillLayout.type = SWT.HORIZONTAL
(default)
fillLayout.type = SWT.VERTICAL
FillLayoutExample.java
package org.o7planning.tutorial.swt.layout;
import
import
import
import
import
import
import
org.eclipse.swt.SWT;
org.eclipse.swt.layout.FillLayout;
org.eclipse.swt.layout.RowLayout;
org.eclipse.swt.widgets.Button;
org.eclipse.swt.widgets.Composite;
org.eclipse.swt.widgets.Display;
org.eclipse.swt.widgets.Shell;
display.dispose();
}
}
Run result:
9.4- RowLayout
RowLayout is more commonly used than FillLayout because of its ability to wrap, and because
it provides configurable margins and spacing. RowLayout has a number of configuration fields.
In addition, the height and width of each widget in a RowLayout can be specified by setting a
RowData object into the widget using setLayoutData.
The field configuration: Wrap, Pack, Justify:
Initial
After resize
wrap = true
pack = true
justify = false
(defaults)
wrap = false
(clips if not enough space)
pack = false
(all widgets are the same size)
justify = true
(widgets are spread across the
available space)
MarginLeft, MarginTop, MarginRight, MarginBottom, Spacing:
These fields control the number of pixels between widgets (spacing) and the number of pixels
between a widget and the side of the parent Composite (margin). By default, RowLayouts leave
3 pixels for margin and spacing. The margin and spacing fields are shown in the following
diagram.
9.5- GridLayout
GridLayout is the most useful and powerful of the standard layouts, but it is also the most
complicated. With a GridLayout, the widget children of a Composite are laid out in a grid.
GridLayout has a number of configuration fields, and, like RowLayout, the widgets it lays out
can have an associated layout data object, called GridData. The power of GridLayout lies in the
ability to configure GridData for each widget controlled by the GridLayout.
The configuration of the GridLayout:
NumColumns
13
MakeColumnsEqualWidth
9.6- StackLayout
This Layout stacks all the controls one on top of the other and resizes all controls to have the
same size and location. The control specified in topControl is visible and all other controls are
not visible. Users must set the topControl value to flip between the visible items and then call
layout() on the composite which has the StackLayout.
package org.o7planning.tutorial.swt.swtsubclass;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class MyButton extends Button {
public MyButton(Composite parent, int style) {
super(parent, style);
}
// You have to override this method.
@Override
protected void checkSubclass() {
// No need to do anything.
}
}
15
TopComposite
File/New/Other...
16
TopComposite.java
package org.o7planning.tutorial.swt.module;
import org.eclipse.swt.widgets.Composite;
public class TopComposite extends Composite {
private Text text;
/**
* Create the composite.
* @param parent
* @param style
*/
public TopComposite(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout(SWT.HORIZONTAL));
Composite composite = new Composite(this, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Button btnPreferredSite = new Button(composite, SWT.CHECK);
btnPreferredSite.setText("Preferred Site");
Label lblColumnWidth = new Label(composite, SWT.NONE);
lblColumnWidth.setText("Column width");
text = new Text(composite, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
1));
}
@Override
protected void checkSubclass() {
}
}
BottomComposite
17
BottomComposite.java
package org.o7planning.tutorial.swt.module;
import org.eclipse.swt.SWT;
public class BottomComposite extends Composite {
private Text text;
/**
18
MainComposite
Similarly create class MainComposite:
19
20
Right-click on the Category Pallette "My Composite" to add TopComposite & BottomComposite.
21
22
Now TopComposite & BottomComposite are easily drag and drop on the other Composite.
MainComposite.java
package org.o7planning.tutorial.swt.module;
import
import
import
import
import
org.eclipse.swt.SWT;
org.eclipse.swt.layout.FillLayout;
org.eclipse.swt.layout.GridData;
org.eclipse.swt.layout.GridLayout;
org.eclipse.swt.widgets.Composite;
12- JFace
24