SWT Designer
SWT Designer
1. Introduction
2. Overview
2.1 History Of WindowBuilder
2.2 Components
2.3 Features
3. Install WindowBuilder
4. Create New Project
5. Create New SWT Application
5.1 Design View
5.2 Running the application
6. Adding Controls
6.1 Components
6.2 Component Tree
7. Property Pane
8. Event Handling
8.1 Add Event Handler
8.2 Deleting an Event Handler
9. Layout Managers
10. Palette
10.1 Swing Designer Palette
11. Morphing
1. Introduction
The WindowBuilder is available at Eclipse WindowBuilder. It is composed of WindowBuilder
Engine, SWT, eRCP, XWT & Swing Designer. WindowBuilder Engine provides a rich API for
creating UI designers. It is very modular with dozens of extension points. It provides pluggable
support for different languages and parsers. It provides both Java (Swing, SWT/RCP, eRCP, GWT)
and XML (XWT, GWT UiBinder, Android) based UI frameworks.
WindowBuilder is built as a plug-in to Eclipse and the various Eclipse-based IDEs (RAD, RSA,
MyEclipse, JBuilder, etc.). The plug-in builds an abstract syntax tree (AST) to navigate the source
code and uses GEF to display and manage the visual presentation. The generated code doesn’t
require any additional custom libraries to compile and run: all of the generated code can be used
without having WindowBuilder Pro installed. WindowBuilder Pro can read and write almost any
format and reverse-engineer most hand-written Java GUI code. It also supports free-form code
editing (make changes anywhere…not just in special areas) and most user re-factorings (you can
move, rename and subdivide methods without a problem).
2. Overview
In this section we will provide some overview of WindowBuilder.
2.2. Components
WindowBuilder is composed of the following major components:
• Source View: Embedded version of Eclipse Java editor provides all of the services you
normally find in the Java editor
• Design View
• Component Tree
• Property Pane
• Palette
• Wizards
• Toolbars & Context Menus
2.3 Features
WindowBuilder supports WYSIWYG & Bi-directional Code Generation. It supports powerful &
flexible Code parser. It supports Internationalization, Visual Inheritance and UI factories. It also
supports Morphing, Widget & layout managers and graphical menu editing.
3. Install WindowBuilder
To install WindowBuilder, go to Help=>Eclipse Marketplace… In the Find text box enter
WindowBuilder.
Figure 1. Eclipse Marketplace
Since the WindowBuilder plugin is already installed we don’t get the Install button but instead we
get the Update button.
01 import org.eclipse.swt.widgets.Display;
02 import org.eclipse.swt.widgets.Shell;
03 public class HelloWorld {
04
05 protected Shell shell;
06
07 /**
08 * Launch the application.
09 * @param args
10 */
11 public static void main(String[] args) {
12 try {
13 HelloWorld window = new HelloWorld();
14 window.open();
15 } catch (Exception e) {
16 e.printStackTrace();
17 }
18 }
19
20 /**
21 * Open the window.
22 */
23 public void open() {
24 Display display = Display.getDefault();
25 createContents();
26 shell.open();
27 shell.layout();
28 while (!shell.isDisposed()) {
29 if (!display.readAndDispatch()) {
30 display.sleep();
31 }
32 }
33 }
34
35 /**
36 * Create contents of the window.
37 */
38 protected void createContents() {
39 shell = new Shell();
40 shell.setSize(450, 300);
41 shell.setText("SWT Application");
42 }
43 }
6. Adding Controls
In this section we will see how we can add controls in our application. Let’s say we want to create a
login form which will have two text boxes, Username and Password and one Login button. To
create a Label drag the ‘Label’ item in the application window. Do the same for Textboxes and
buttons. We can change the text which is displayed for the item from the Properties window. Once
you have added the above mentioned items in the application window, your Design window will
look something like below:
Figure 9. Application Design Window
In the Structure window you can see the items which have been added in the application window. If
you click on any item you can see the properties of that item in the Properties window. Now if you
save your changes and go to the Source tab you will see that Eclipse has updated the source code to
reflect the changes. The main changes are in the createContents() method. You will see that Eclipse
had added code to add the newly added elements in the Design tab
6.1 Components
In this section we will see how we can perform actions on the components in Design view.
8. Event Handling
Events occur when the user interacts with the UI. The appropriate event-handling code is then
executed. In order to know when events occur, event handlers must first be added to your
components. The tool makes it very easy to add and remove event listeners to your components.
9. Layout Managers
A program written in Java may be deployed on multiple platforms. If you were to use standard UI
design techniques, specifying absolute positions and sizes for your UI components, your UI won’t
be portable. What looks fine on your development system might be unusable on another platform.
To solve this problem, Java provides a system of portable layout managers. You use these layout
managers to specify rules and constraints for the layout of your UI in a way that will be portable.
Layout managers can provide the following advantages:
• Correctly positioned components that are independent of fonts, screen resolutions, and
platform differences.
• Intelligent component placement for containers that are dynamically resized at runtime.
• Ease of translation. If a string increases in length after translation, the associated
components stay properly aligned.
When writing Java applications, you may need to use layouts to give your windows a specific look.
A layout controls the position and size of children in a container. Layout classes are subclasses of
the abstract class Layout. Both SWT and Swing provide several standard layout classes, and you
can write custom layout classes. Other UI toolkits embed implicit layout managers in each panel
type.
In Java, positioning and sizing does not happen automatically. Applications can decide to size and
place a container’s children initially, or in a resize listener. Another option is to specify a layout
class to position and size the children. If children are not given a size, they will have zero size and
they cannot be seen.
The layout manager sets the sizes and locations of the components based on various factors such as:
• The layout manager’s layout rules.
• The layout manager’s property settings, if any.
• The layout datas associated with each component.
• The size of the container.
Each layout manager has characteristic strengths and drawbacks. Grid-based layouts and constraint-
based layouts are the most useful and powerful of the standard layouts, but they are also the most
complicated. When using the design view, you can change the layout whenever you like. The tool
adjusts the code as needed on the fly. Change the layout either by explicitly adding a layout
manager to the source code for the container, or by selecting a layout for the composite on the
design surface.
10. Palette
The Palette provides quick access to toolkit-specific components as well as any custom components
installed by the user. The Palette is organized into categories which may be expanded, collapsed or
hidden. To add a components to the Design View, you can:
• Select it in the palette and drop it on the Design View or Component Tree by clicking in the
desired location.
• Use the Choose Component command to select the widget type from Choose Component
dialog.
Multiple widgets of the same type may be added to the Design View by holding down the Ctrl key
when selecting the widget in the Palette. The palette may be customized by right-clicking anywhere
in the palette to access the palette context menu or by opening the Palette Manager dialog. The
position of the Palette may be configured via drag and drop or by right-clicking on the Palette
header and selecting the Dock On command. The Palette may be docked to either the left or right
side of the Design View.
The Palette Manager may be accessed from the palette context menu. The Palette Manager dialog
allows the toolkit-specific palette to be fully configured. Categories and entries may be added,
edited, rearranged and removed and JAR files imported. Items may be rearranged using the Up and
Down buttons or via drag and drop. The Collapse All and Expand All buttons show or hide all of the
elements in the list. Items may be hidden from the palette by unchecking the checkbox next to their
category or component name.
10.1.1 Containers
JPanel – A generic lightweight container.
JScrollPane – Provides a scrollable view of a lightweight component. A JScrollPane manages a
viewport, optional vertical and horizontal scroll bars, and optional row and column heading
viewports.
JSplitPane – JSplitPane is used to divide two (and only two) Components. The two Components
are graphically divided based on the look and feel implementation, and the two Components can
then be interactively resized by the user.
JTabbedPane – A component that lets the user switch between a group of components by clicking
on a tab with a given title and/or icon.
JToolBar – A component that is useful for displaying commonly used Action’s or controls.
JDesktopPane – A container used to create a multiple-document interface or a virtual desktop.
JInternalFrame – A lightweight object that provides many of the features of a native frame,
including dragging, closing, becoming an icon, resizing, title display, and support for a menu bar.
10.1.2 Layouts
Absolute Layout – An absolute layout displays components with specified bounds.
FlowLayout – A flow layout arranges components in a left-to-right flow, much like lines of text in a
paragraph. Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left
to right until no more buttons fit on the same line.
BorderLayout – A border layout lays out a container, arranging and resizing its components to fit
in five regions: north, south, east, west, and center. Each region may contain no more than one
component.
GridLayout – The GridLayout class is a layout manager that lays out a container’s components in a
rectangular grid. The container is divided into equal-sized rectangles, and one component is placed
in each rectangle.
GridBagLayout – The GridBagLayout class is a flexible layout manager that aligns components
vertically and horizontally, without requiring that the components be of the same size. Each
GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component
occupying, one or more cells.
CardLayout – A CardLayout object is a layout manager for a container. It treats each component in
the container as a card. Only one card is visible at a time, and the container acts as a stack of cards.
The first component added to a CardLayout object is the visible component when the container is
first displayed.
BoxLayout – A layout manager that allows multiple components to be laid out either vertically or
horizontally. The components will not wrap so, for example, a vertical arrangement of components
will stay vertically arranged when the frame is resized.
FormLayout – JGoodies FormLayout is a powerful, flexible and precise general purpose layout
manager. It aligns components vertically and horizontally in a dynamic rectangular grid of cells,
with each component occupying one or more cells.
SpringLayout – A SpringLayout lays out the children of its associated container according to a set
of constraints for each side.
GroupLayout – GroupLayout mixes grid layout and free form layout.
10.1.4 Components
JLabel – A display area for a short text string or an image, or both. A label does not react to input
events. As a result, it cannot get the keyboard focus. A label can, however, display a keyboard
alternative as a convenience for a nearby component that has a keyboard alternative but can’t
display it.
JTextField – A lightweight component that allows the editing of a single line of text.
JComboBox – A component that combines a button or editable field and a drop-down list. The user
can select a value from the drop-down list, which appears at the user’s request. If you make the
combo box editable, then the combo box includes an editable field into which the user can type a
value.
JButton – An implementation of a “push” button.
JCheckBox – An implementation of a check box — an item that can be selected or deselected, and
which displays its state to the user. By convention, any number of check boxes in a group can be
selected.
JRadioButton – An implementation of a radio button — an item that can be selected or deselected,
and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons
in which only one button at a time can be selected.
JToggleButton – An implementation of a two-state button — an item that can be selected or
deselected, and which displays its state to the user.
JTextArea – A JTextArea is a multi-line area that displays plain text.
JFormattedTextField – A lightweight component that allows the editing of a single line of text
using a predefined mask.
JPasswordField – A lightweight component that allows the editing of a single line of text where
the view indicates something was typed, but does not show the original characters.
JTextPane – A text component that can be marked up with attributes that are represented
graphically.
JEditorPane – A text component to edit various kinds of content.
JSpinner – A lightweight component that allows the lets the user select a number or an object value
from an ordered sequence.
JList – A component that allows the user to select one or more objects from a list.
JTable – The JTable is used to display and edit regular two-dimensional tables of cells.
JTable on JScrollPane – A JTable placed within a JScrollPane.
Table model column – A JTable model column.
JTree – A control that displays a set of hierarchical data as an outline.
JProgressBar – A component that, by default, displays an integer value within a bounded interval.
A progress bar typically communicates the progress of some work by displaying its percentage of
completion and possibly a textual display of this percentage.
JScrollBar – An implementation of a scrollbar. The user positions the knob in the scrollbar to
determine the contents of the viewing area. The program typically adjusts the display so that the end
of the scrollbar represents the end of the displayable contents, or 100% of the contents.
JSeparator – A horizontal or vertical line used to separate other controls.
JSlider – A component that lets the user graphically select a value by sliding a knob within a
bounded interval. The slider can show both major tick marks and minor tick marks between them.
The number of values between the tick marks is controlled with setMajorTickSpacing and
setMinorTickSpacing.
10.1.5 Menu
JMenuBar – A menu bar.
JPopupMenu – A popup menu.
JMenu – Either a pull-down or cascaded menu.
JMenuItem – A simple menu item.
JCheckBoxMenuItem – A checkbox menu item.
JRadioButtonMenuItem – A radio button menu item.
11. Morphing
It can be frustrating to suddenly want a Group instead of a Composite or a Combobox rather than a
List after you have already completed the design of the composite. It is very easy to remedy this by
using the tool.
The editor allows you to morph similar components from one type to another. When a component is
morphed from one type to another, the properties that are the same between the two types are kept.
This allows quick design changes without having to recreate all the components.
To morph a component from one type to another, right-click on the component and select a
component type to morph to from the Morph cascading menu.
In addition to morphing to common sibling types, you can also morph a component into any
subclass or into any other arbitrary component type.
Figure 15. Morphing