02 - Working With Eclipse Standard Widget Toolkit: Page 2 of 3

Most Commonly Used Widgets

All the widgets have been defined in org.eclipse.swt.widgets package. In This section we will discuss most commonly used widgets. Most likely you will use these widgets during eclipse plug-in development. Best way to learn these widgets is through examples, so ill focus this discussion based on examples. You can refer to SWT API document for further reading. All API’s are self-explanatory and quite simple.

1. Label Widget

Labels are used to display text messages/labels. Labels do not take part in event model and hence do not generate user interaction events.

public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("This is a label");
      myShell.setBounds(100, 100, 200, 50);
      myShell.setLayout(new FillLayout());
       Label label = new Label(myShell, SWT.CENTER);
       label.setText("Hello World");
     Color red = new Color(myDisplay, 255, 0, 0);
     label.setForeground(red);
     myShell.open();
     while (!myShell.isDisposed()) {
        if (!myDisplay.readAndDispatch()) myDisplay.sleep();
     }
     red.dispose();
     myDisplay.dispose();
}

2. Button Widget

Buttons are used to generate special event called selection event when pressed or released. While creating a button we can use different styles so that this control represent different type of UI element. For example, we can use SWT.ARROW to create arrow button widget or SWT.CHECK to create checkbox.

public class MYButton {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("This is my button");
      myShell.setBounds(120, 120, 220, 120);
      myShell.setLayout(new FillLayout());
      final Button button = new Button(myShell, SWT.PUSH);
       button.setText("Click");
      button.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
           button.setText("You clicked me!");
         }
      });
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
 }
}

3. Text Widget

Eclipse plugin text field widget is used to take user input. It automatically shows scroll bar if provided text is more then control’s length

public class MYText {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My Text Box");
      myShell.setBounds(120, 120, 220, 120);
      myShell.setLayout(new FillLayout());
      final Text text = new Text(myShell, SWT.MULTI);
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

4. Combo Widget

Eclipse plugin Combo widget helps in displaying multiple options to user. User is allowed to select single option from the list of options.

public class MyCombo {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My Combo");
      myShell.setBounds(120, 120, 220, 120);
      myShell.setLayout(new FillLayout(SWT.VERTICAL));
     final Combo myCombo = new Combo(myShell,SWT.READ_ONLY);
      myCombo.setItems(new String[]
         {"option1", "option2", "option3", "option4", "option5"});
      myCombo.setText("option5");
      myCombo.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
            System.out.println("you selected me: " + myCombo.getText());
          }
      });
      myShell.open();
       while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

5. Table Widget

Table widget is used to display items in rows and columns. The columns of a table are defined by TableColumn.

public class MyTable {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My Table");
      myShell.setBounds(120, 120, 220, 120);
      myShell.setLayout(new FillLayout());
      final Table myTable = new Table(myShell,
         SWT.SINGLE | SWT.FULL_SELECTION);
      TableColumn col1 =
         new TableColumn(myTable, SWT.NULL);
      col1.setText("First Column");
      col1.pack();
      TableColumn col2 =
         new TableColumn(myTable, SWT.NULL);
      col2.setText("Second Column");
      col2.pack();
      TableItem tableItem1 = new TableItem(myTable, SWT.NULL);
      tableItem1.setText(new String[] {"A1", "A2"});
      TableItem tableItem2 = new TableItem(myTable, SWT.NULL);
      tableItem2.setText(new String[] {"B1", "B2"});
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

6. Tree Widget

The tree widget is used to display data in a hierarchical manner. Typically tree has many tree items. For understanding purposes you can call tree item an "Tree Parent" if this item contains other items. Tree parent can contain other Tree Parents. or simply leaf nodes. A user navigates through a tree by expanding and collapsing items

public class MyTree {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My Tree");
      myShell.setBounds(120, 120, 220, 220);
      myShell.setLayout(new FillLayout());
      final Tree tree = new Tree(myShell, SWT.SINGLE);
      for (int i = 1; i < 4; i++) {
         TreeItem parent1 = new TreeItem(tree, 0);
         parent1.setText("Paren1 - " + i);
         for (int j = 1; j < 4; j++) {
            TreeItem parent2 = new TreeItem(parent1,0);
            parent2.setText("Parent2 - " + j);
            for (int k = 1; k < 4; k++) {
               TreeItem child = new TreeItem(parent2, 0);
               child.setText("Child - " + k);
            }
         }
      }
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

7. Menu Widget

The Menu widget is used to display user set of actions. Typical example of menu is the file menu on top of this browser window. Menu can further contain submenu’s and so on.

public class MyMenu {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      final Shell myShell = new Shell(myDisplay);
      myShell.setText("My Menu");
      myShell.setBounds(110, 110, 210, 110);
      Menu myBar = new Menu(myShell, SWT.BAR);
      myShell.setMenuBar(myBar);
      MenuItem fileMenuItem = new MenuItem(myBar, SWT.CASCADE);
      fileMenuItem.setText("&This is my Menu");
      Menu subMenuItem = new Menu(myShell, SWT.DROP_DOWN);
      fileMenuItem.setMenu(subMenuItem);
      MenuItem selectMenuItem = new MenuItem(
         subMenuItem, SWT.NULL);
      selectMenuItem.setText("&Hello\tCtrl+S");
      selectMenuItem.setAccelerator(SWT.CTRL + 'S');
      selectMenuItem.addSelectionListener(
         new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
            System.out.println("Hello Selected!");
         }
      });
      MenuItem thisIsSeperator = new MenuItem(subMenuItem, SWT.SEPARATOR);
      MenuItem exitMenuItem = new MenuItem(subMenuItem, SWT.NULL);
      exitMenuItem.setText("&Bye");
      exitMenuItem.addSelectionListener(new SelectionAdapter(){
         public void widgetSelected(SelectionEvent event) {
            myShell.dispose();
         }
      });
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

8. Composite Widget

This is a very important widget since it is used to group together all other widgets. Composite widget can be used as a container for other widgets. We can use layout managers (Discussed next) for positioning and placement of other widgets inside composite widget.

public class MyComposite {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My Composite");
      myShell.setBounds(120, 120, 220, 220);
      Composite composite = new Composite(
         myShell,SWT.BORDER);
      composite.setBounds(35, 35, 155, 127);
      final Button button = new Button(composite,SWT.PUSH);
      button.setBounds(30, 30, 110, 85);
      button.setText("ClickMe");
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

Like us on Facebook