Getting started with swt
Remarks#
This section provides an overview of what swt is, and why a developer might want to use it.
It should also mention any large subjects within swt, and link out to the related topics. Since the Documentation for swt is new, you may need to create initial versions of those related topics.
Versions#
Version | Release Date |
---|---|
4.7 | 2017-06-12 |
4.6.3 | 2017-03-01 |
4.6.2 | 2016-11-24 |
4.6.1 | 2016-09-07 |
4.6 | 2016-06-06 |
4.5.2 | 2016-02-12 |
4.5.1 | 2015-09-04 |
4.5 | 2015-06-03 |
4.4.2 | 2015-02-04 |
4.4.1 | 2014-09-25 |
4.4 | 2014-06-06 |
4.3.2 | 2014-02-21 |
4.3.1 | 2013-09-11 |
4.3 | 2013-06-05 |
4.2.2 | 2013-02-04 |
4.2.1 | 2012-09-14 |
4.2 | 2012-06-08 |
4.1.2 | 2012-02-23 |
4.1.1 | 2011-09-12 |
4.1 | 2011-06-20 |
4 | 2010-07-27 |
3.8.2 | 2013-01-31 |
3.8.1 | 2012-09-14 |
3.8 | 2012-06-08 |
3.7.2 | 2012-02-08 |
3.7.1 | 2011-09-09 |
3.7 | 2011-06-13 |
3.6.2 | 2011-02-10 |
3.6.1 | 2010-09-09 |
3.6 | 2010-06-08 |
3.5.2 | 2010-02-11 |
3.5.1 | 2009-09-17 |
3.5 | 2009-06-11 |
3.4.2 | 2009-02-11 |
3.4.1 | 2008-09-11 |
3.4 | 2008-06-17 |
3.3.2 | 2008-02-21 |
3.3.1.1 | 2007-10-23 |
3.3.1 | 2007-09-21 |
3.3 | 2007-06-25 |
3.2.2 | 2007-02-12 |
3.2.1 | 2006-09-21 |
3.2 | 2006-06-29 |
3.1.2 | 2006-01-18 |
3.1.1 | 2005-09-29 |
3.1 | 2005-06-27 |
3.0.2 | 2005-03-11 |
3.0.1 | 2004-09-16 |
3 | 2004-06-25 |
2.1.3 | 2004-03-10 |
2.1.2 | 2003-11-03 |
2.1.1 | 2003-06-27 |
2.1 | 2003-03-27 |
2.0.2 | 2002-11-07 |
2.0.1 | 2002-08-29 |
2 | 2002-06-27 |
1 | 2001-11-07 |
Installation or Setup
Detailed instructions on getting swt set up or installed.
Creating a new SWT progam
Create a new text file named HelloWorld.java
and paste this code in it:
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class HelloWorld
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("Hello World!");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
When you start the program it will look something like this:
A closer look at the Hello World application
The Hello World application consists of a HelloWorld
class definition and a main
method.
The main method defines a Display
and a Shell
. The display acts as the interface between SWT and the underlying operating system. It handles the platform event model in the form of the SWT event loop. The shell represents a single window of the desktop or window manager.
Widgets are added to the shell by specifying the shell in the constructor of the widget. In this example we create a Label
. A label is a widget that can display text or an image. In this case we set the text “Hello World!” to it. The widget is added to the shell by specifying our shell as the first argument in the constructor.
To make the label visible in the shell we either have to set a fixed size to it or we need to tell its parent (the shell) how to layout its children.
The FillLayout
is the simplest SWT Layout
. It organizes all its children in a single row or column and forces them to have the same size.
The following lines tell the shell to apply its layout and become visible:
shell.pack();
shell.open();
Last but most importantly, we need to define the event loop of the SWT program. The event loop is needed to transfer the user input events from the underlying operating system widgets to the SWT event system.
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
This loop will run until the shell is disposed. Once this happens, the display is disposed as well and the program will terminate. While the program is looping, it will read the next operating system event and transfer it to SWT. If there is no event, the thread will sleep until the next event arrives.