Java - SWT - Hello World tutorial

Hello World!
It seems to me that I already seen this expression somewhere.

Well, let's go?

First thing to know is that we need two SWT widgets to make a window appear:

  • Display
  • Shell

Then a loop while to tell the program to continue displaying the window until someone close it.
Now, the code.

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Helloworld {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
       
        shell.setSize(300, 300);
        shell.setText("Hello World");
        shell.open();
       
        while(!shell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();
        display.dispose();
    }
}

Right click on the HelloWorld class and run it as a Java Application.
The window is coming and displays the title Hello World.

Really easy, isn't it? surprise

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.