swt

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#

VersionRelease Date
4.72017-06-12
4.6.32017-03-01
4.6.22016-11-24
4.6.12016-09-07
4.62016-06-06
4.5.22016-02-12
4.5.12015-09-04
4.52015-06-03
4.4.22015-02-04
4.4.12014-09-25
4.42014-06-06
4.3.22014-02-21
4.3.12013-09-11
4.32013-06-05
4.2.22013-02-04
4.2.12012-09-14
4.22012-06-08
4.1.22012-02-23
4.1.12011-09-12
4.12011-06-20
42010-07-27
3.8.22013-01-31
3.8.12012-09-14
3.82012-06-08
3.7.22012-02-08
3.7.12011-09-09
3.72011-06-13
3.6.22011-02-10
3.6.12010-09-09
3.62010-06-08
3.5.22010-02-11
3.5.12009-09-17
3.52009-06-11
3.4.22009-02-11
3.4.12008-09-11
3.42008-06-17
3.3.22008-02-21
3.3.1.12007-10-23
3.3.12007-09-21
3.32007-06-25
3.2.22007-02-12
3.2.12006-09-21
3.22006-06-29
3.1.22006-01-18
3.1.12005-09-29
3.12005-06-27
3.0.22005-03-11
3.0.12004-09-16
32004-06-25
2.1.32004-03-10
2.1.22003-11-03
2.1.12003-06-27
2.12003-03-27
2.0.22002-11-07
2.0.12002-08-29
22002-06-27
12001-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:

enter image description here


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.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow