Having a border around an SWT list

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);

      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT List Demonstration");
      shell.setLayout(new GridLayout());

      String []items = {"Belgium", "The Netherlands", "France", "Germany", "Italy",
                        "Luxembourg", "Switzerland", "Austria", "Finland", "Iceland" };
      
      // to have a border around your list, "or" SWT.BORDER
      final List list = new List(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
      list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
      list.setItems (items);
            
      shell.open();
      
      shell.addShellListener(new ShellAdapter() {
         public void shellClosed(ShellEvent se) {
            int[] indices = list.getSelectionIndices();
            System.out.println("Selected items:");
            for (int i=0; i<indices.length; i++) {
               System.out.println(list.getItem(indices[i]));   
            }
         }
      });      

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      
      display.dispose();
   }
   
   public static void recurseTree(TreeItem item) {
      System.out.println(item.getText() + "tChecked=" + item.getChecked());
      TreeItem[] treeItems = item.getItems(); 
      for (int i=0; i<treeItems.length; i++) {
         TreeItem treeItem = treeItems[i];
         recurseTree(treeItem);
      }   
   }
}

Using an ArrowButton in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT ToggleButton Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 4;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            System.out.println("integer value for alignment: " + ((Button) event.widget).getAlignment());
         };
      };
 
      createArrowButton(buttonGroup, SWT.LEFT, selectionListener);
      createArrowButton(buttonGroup, SWT.RIGHT, selectionListener);
      createArrowButton(buttonGroup, SWT.UP, selectionListener);
      createArrowButton(buttonGroup, SWT.DOWN, selectionListener);
      
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
   
   public static void createArrowButton(Composite parent, int alignment, 
                                          SelectionListener selectionListener) {
      GridData gridData = new GridData();
      gridData.widthHint = 30;
      gridData.heightHint = 30;
      Button button = new Button(parent, SWT.ARROW);
      button.setLayoutData(gridData);
      button.setAlignment(alignment);
      button.addSelectionListener(selectionListener);
   }
}

Creating a Shell without a border in SWT

Create a Shell with the style SWT.NONE.

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
 
public class Main { 
   static Display display;
   static Shell shell;
   static Color color;
    
   public static void main(String[] args) {
      display = new Display();
      shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 400, 200);
      shell.setText("SWT Shell Demonstration");
      shell.setLayout(new GridLayout());
 
      final Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
      final Button createShellButton = new Button(buttonGroup, SWT.PUSH);
      createShellButton.setText("Open Shell");
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            final Shell childShell = new Shell(SWT.NONE);
            Button closeButton = new Button(childShell, SWT.PUSH);
            closeButton.setBounds(10, 10, 100, 30);
            closeButton.setText("Close Shell");
            closeButton.addListener(SWT.Selection, new Listener() {
               public void handleEvent(Event event) {
                  childShell.dispose();
               }
            });
         
            childShell.setSize (300, 100);
            childShell.setText ("Title of Shell");
            childShell.open ();                  
         };
      };
       
      createShellButton.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      if (color != null && !color.isDisposed()) {
         color.dispose();
      }
      display.dispose();
   }
}

Creating a coolbar in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
 
public class Main {
   public static void main (String [] args) {
      Display display = new Display ();
      Shell shell = new Shell (display);
      shell.setLayout(new GridLayout());
 
      final CoolBar coolBar = new CoolBar(shell, SWT.BORDER);
      coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));  
 
      // create the first ToolBar
      ToolBar toolBar = new ToolBar(coolBar, SWT.NULL);
      ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("jpgIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
      toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("gifIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
      toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("tiffIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
       
      CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
      Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
      size = coolItem.computeSize(size.x, size.y);
      coolItem.setSize(size);
      coolItem.setMinimumSize(toolItem.getWidth(), size.y);
      coolItem.setControl(toolBar);
            
      // create the second ToolBar
      toolBar = new ToolBar(coolBar, SWT.BORDER);
      ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
      item.addSelectionListener(new DropDownSelectionListener(shell));
      item.setImage(getImage("jpgIcon.gif"));
      item = new ToolItem(toolBar, SWT.DROP_DOWN);
      item.addSelectionListener(new DropDownSelectionListener(shell));
      item.setImage(getImage("gifIcon.gif"));
      
      coolItem = new CoolItem(coolBar, SWT.NONE);
      size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
      size = coolItem.computeSize(size.x, size.y);
      coolItem.setSize(size);
      coolItem.setMinimumSize(toolItem.getWidth(), size.y);
      coolItem.setControl(toolBar);
       
      // all the indices that will begin on a new row      
      coolBar.setWrapIndices(new int[] {0, 1});
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            coolBar.setLocked(button.getSelection());
         };
      };
      
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));      
      Button lockButton = new Button(buttonGroup, SWT.CHECK);
      lockButton.setText("Lock the CoolBar!");
      lockButton.addSelectionListener(selectionListener);
                 
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
   }
   
   public static Image getImage(String imageName) {
      ImageData source = new ImageData(Main.class.getResourceAsStream(imageName));
      ImageData mask = source.getTransparencyMask();
      Image image = new Image(null, source, mask);
      return image;
   }   
   
   
   static class DropDownSelectionListener extends SelectionAdapter {
      private Menu menu = null;
      private Shell shell;
      
      public DropDownSelectionListener(Shell shell) {
         this.shell = shell;
      }
      
      public void widgetSelected(SelectionEvent event) {
         System.out.println("Widget is clicked on!");
         if (menu == null) {
            // create menu if it doesn't exist already
            menu = new Menu(shell);
            MenuItem menuItem1 = new MenuItem(menu, SWT.NONE);
            menuItem1.setText("App #1");
            MenuItem menuItem2 = new MenuItem(menu, SWT.NONE);
            menuItem2.setText("App #2");
            MenuItem separatorItem = new MenuItem(menu, SWT.SEPARATOR);
            MenuItem menuItem3 = new MenuItem(menu, SWT.NONE);
            menuItem3.setText("Run...");
            
            menuItem1.addSelectionListener(new SelectionAdapter() {
               public void widgetSelected(SelectionEvent e) {
                  menu.setVisible(true);
               }
            });
         }
         
         // if user clicked on down arrow, show menu
         if (event.detail == SWT.ARROW) {
            System.out.println("... and it's the arrow!");
            if (menu.getVisible()) {
               menu.setVisible(false);
            } else { 
               final ToolItem toolItem = (ToolItem) event.widget;
               final ToolBar  toolBar = toolItem.getParent();
               
               Rectangle toolItemBounds = toolItem.getBounds();
               Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
               menu.setLocation(point.x, point.y + toolItemBounds.height);
               menu.setVisible(true);
            }
         }
      }
   }   
} 

Using an image button in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT Image Button Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
                   
      Button button1 = new Button(buttonGroup, SWT.PUSH);
      button1.setImage(getImage("jpgIcon.gif"));
      button1.addSelectionListener(selectionListener);
      Button button2 = new Button(buttonGroup, SWT.PUSH);
      button2.setImage(getImage("gifIcon.gif"));
      button2.addSelectionListener(selectionListener);
      Button button3 = new Button(buttonGroup, SWT.PUSH);
      button3.setImage(getImage("tiffIcon.gif"));
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
    
   public static Image getImage(String imageName) {
      ImageData source = new ImageData(Main.class.getResourceAsStream(imageName));
      ImageData mask = source.getTransparencyMask();
      Image image = new Image(null, source, mask);
      return image;
   }
}

Images used:


Creating a Shell with only the minimize button enabled in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
 
public class Main { 
   static Display display;
   static Shell shell;
   static Color color;
    
   public static void main(String[] args) {
      display = new Display();
      shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 400, 200);
      shell.setText("SWT Shell Demonstration");
      shell.setLayout(new GridLayout());
 
      final Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
      final Button createShellButton = new Button(buttonGroup, SWT.PUSH);
      createShellButton.setText("Open Shell");
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            final Shell childShell = new Shell(SWT.BORDER | SWT.MIN);
            Button closeButton = new Button(childShell, SWT.PUSH);
            closeButton.setBounds(10, 10, 100, 30);
            closeButton.setText("Close Shell");
            closeButton.addListener(SWT.Selection, new Listener() {
               public void handleEvent(Event event) {
                  childShell.dispose();
               }
            });
          
            childShell.setSize (300, 100);
            childShell.setText ("Title of Shell");
            childShell.open ();                  
         };
      };
       
      createShellButton.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      if (color != null && !color.isDisposed()) {
         color.dispose();
      }
      display.dispose();
   }
}

Creating a wrapped label in SWT

Main.java:

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.*;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("Label Demonstration");
      shell.setLayout(new GridLayout());
            
      Label label = new Label(shell, SWT.BORDER | SWT.WRAP);
      label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
      label.setText("`I quite agree with you,' said the Duchess; `and the moral " +
                    "of that is--Be what you would seem to be--or if you'd like " +
                    "it put more simply--Never imagine yourself not to be " +
                    "otherwise than what it might appear to others that what " +
                    "you were or might have been was not otherwise than what " +
                    "you had been would have appeared to them to be otherwise.'");
 
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Aligning the text on an SWT button

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 400, 200);
      shell.setText("SWT Button Alignment Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
       
      Button button1 = new Button(buttonGroup, SWT.PUSH);
      button1.setText("left");
      sizeControl(button1, 100, 30);   
      button1.setAlignment(SWT.LEFT);   
      button1.addSelectionListener(selectionListener);
       
      Button button2 = new Button(buttonGroup, SWT.PUSH);
      button2.setText("center");
      sizeControl(button2, 100, 30);
      button2.setAlignment(SWT.CENTER);
      button2.addSelectionListener(selectionListener);
 
      Button button3 = new Button(buttonGroup, SWT.PUSH);
      button3.setText("right");
      sizeControl(button3, 100, 30);
      button3.setAlignment(SWT.RIGHT);
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
    
   public static void sizeControl(Control control, int width, int height) {
      GridData gridData = new GridData();   
      gridData.widthHint = width;
      gridData.heightHint = height;
      control.setLayoutData(gridData);
   }
}

Setting the alignment on a Label in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("Label Demonstration");
      shell.setLayout(new GridLayout());
             
      final Label label = new Label(shell, SWT.BORDER | SWT.WRAP);
      label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
      label.setText("`I quite agree with you,' said the Duchess; `and the moral " +
                    "of that is--Be what you would seem to be--or if you'd like " +
                    "it put more simply--Never imagine yourself not to be " +
                    "otherwise than what it might appear to others that what " +
                    "you were or might have been was not otherwise than what " +
                    "you had been would have appeared to them to be otherwise.'");
       
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            if (button.getText().equals("Left")) {
               label.setAlignment(SWT.LEFT);
            }
            else if (button.getText().equals("Right")) {
               label.setAlignment(SWT.RIGHT);
            }
            else {
               label.setAlignment(SWT.CENTER);
            }
         };
      };
             
      Button leftAlign = new Button(buttonGroup, SWT.RADIO);
      leftAlign.setText("Left");
      leftAlign.addSelectionListener(selectionListener);
      Button centerAlign = new Button(buttonGroup, SWT.RADIO);
      centerAlign.setText("Center");
      centerAlign.addSelectionListener(selectionListener);
      Button rightAlign = new Button(buttonGroup, SWT.RADIO);
      rightAlign.setText("Right");
      rightAlign.addSelectionListener(selectionListener);
 
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}