Eclipse Corner Article

Getting Started with SWT

Exploring the features of the Eclipse Standard Widget Toolkit

Summary
This article contains several examples that show how to use the features of the Eclipse Widget toolkit, SWT. Each example builds upon the ones before. The first example shows how to create an empty window. The second example adds some buttons to the window. The third example registers a listener for events on one of the buttons.



The examples below show how to use SWT to create stand-alone applications, and provide a good basis for experimenting with SWT. The Eclipse Workbench defines the UI paradigm and how editors and views are integrated. It provides the display and shell that are created below so you don't need to create them for you Workbench plug-in user interfaces.

For further details on SWT, take a look at the widget hierarchy and supported style bits. Then try modifying the example below with different widgets, styles, and actions.

Creating your first window

// Create a shell. 
Display display = new Display(); // The "parent" of a shell is the display it is created on.

Shell shell =   
                 

     
                 

   new Shell (display); // Create the top level window. 
shell.open (); // Open it

// This is the event loop.

while (!shell.isDisposed ()) 
  if (!display.readAndDispatch ()) display.sleep ();

Adding a widget

This example builds upon the first example and adds two buttons: A push button and a check box. Notice how the constructors in SWT require you to pass the parent widget and style bits. Style bits are used to specify values required to create the underlying OS resource. In this case it specifies the type of button to create. The code added by this example is marked in italic.
Display display = new Display();

Shell shell = new Shell (display);

Button b1 = new Button (shell, SWT.PUSH); 
b1.setText ("Button 1");
b1.setBounds (0, 0, 100, 60);
Button b2 =      
  
    
        
  
    
   new Button (shell, SWT.CHECK);
b2.setText ("Button 2"); 
b2.setBounds (0, 60, 100, 45);

shell.open (); 
while (!shell.isDisposed ()) 
  if (!display.readAndDispatch ()) display.sleep ();

Adding event handlers

Event handling is done by registering listeners in the same way as in AWT. The code added by this example is marked in italic.
Shell shell = new Shell (); 
Button b1 = new Button (shell, SWT.PUSH); 
b1.setText ("Button 1"); 
b1.setBounds (0, 0, 100, 60);

b1.addMouseListener(new MouseListener() { 
  public void mouseDown(MouseEvent e) { 
      System.out.println("mouseDown:"); 
  }; 
  public void mouseDoubleClick(MouseEvent e) { 
    System.out.println("mouseDoubleClick:"); 
  }; 
  public void mouseUp(MouseEvent e) { 
    System.out.println("mouseUp:"); 
 }; 
});

Button b2 = new Button (shell, SWT.CHECK); 
b2.setText ("Button 2"); 
b2.setBounds (0, 60, 100, 45); 
shell.open ();
  
Display display =
  
   shell.getDisplay (); 
while (!shell.isDisposed ()) 
  if (!display.readAndDispatch ()) display.sleep ();