Making a border around 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.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 Border 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 | SWT.BORDER);
      button1.setText("button 1");
      button1.addSelectionListener(selectionListener);
       
      Button button2 = new Button(buttonGroup, SWT.PUSH | SWT.BORDER);
      button2.setText("button 2");
      button2.addSelectionListener(selectionListener);
 
      Button button3 = new Button(buttonGroup, SWT.PUSH | SWT.BORDER);
      button3.setText("button 3"); 
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Creating an always-on-top SWT Shell

Use the style SWT.ON_TOP when you create the Shell.

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.RESIZE | SWT.CLOSE | SWT.ON_TOP);
            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 smooth ProgressBar in SWT

Main.java:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
 
public class Main {
   private DownloadThread downloadThread = null;
   private boolean disposed = false;
    
   public Main() {
      final Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText("SWT Smooth ProgressBar Demonstration");

      shell.setLayout(new FormLayout());
      shell.setBounds(200, 200, 700, 200);
       
      Label downloadLabel = new Label(shell, SWT.NONE);
      FormData data0 = new FormData();
      data0.left = new FormAttachment(0, 0);
      //data0.top = new FormAttachment(localLabel, 15);
      //data2.right = new FormAttachment(100, -100);
      downloadLabel.setLayoutData(data0);
      downloadLabel.setText("FTP file: ");
 
      final Text downloadField = new Text(shell, SWT.SINGLE | SWT.BORDER);
      FormData data1 = new FormData();
      data1.left = new FormAttachment(downloadLabel, 15);
      data1.right = new FormAttachment(100, -100);
      downloadField.setLayoutData(data1);
      downloadField.setText("ftp://ftp.mozilla.org/pub/mozilla/moz-sol/1.2a-02-09-25/mozilla-sparc-sun-solaris2.7.tar.gz");
            
      Label localLabel = new Label(shell, SWT.NONE);
      FormData data2 = new FormData();
      data2.left = new FormAttachment(0, 0);
      data2.top = new FormAttachment(localLabel, 15);
      localLabel.setLayoutData(data2);
      localLabel.setText("Save As: ");
      
      final Text localField = new Text(shell, SWT.SINGLE | SWT.BORDER);
      FormData data3 = new FormData();
      data3.left = new FormAttachment(localLabel, 15);
      data3.top = new FormAttachment(downloadField, 5);
      data3.right = new FormAttachment(100, -400);
      localField.setLayoutData(data3);
      localField.setText("c:\temp\tmp.bin");
      
      final Button downloadButton = new Button(shell, SWT.NONE);
      FormData data4 = new FormData();
      data4.left = new FormAttachment(downloadField, 5);
      data4.right = new FormAttachment(100, -5);
      downloadButton.setLayoutData(data4);
      downloadButton.setText("Download");
      
      Label progressLabel = new Label(shell, SWT.NONE);
      progressLabel.setText("Download progress:");
      FormData data5 = new FormData();
      data5.top = new FormAttachment(localField, 15);
      data5.left = new FormAttachment(10, 15);      
      data5.right = new FormAttachment(90, -15);
      progressLabel.setLayoutData(data5);      
             
      final ProgressBar progressBar = new ProgressBar(shell, SWT.SMOOTH);
      FormData data6 = new FormData();
      data6.top = new FormAttachment(localField, 35);
      data6.left = new FormAttachment(10, 15);      
      data6.right = new FormAttachment(90, -15);
      progressBar.setLayoutData(data6);
 
      shell.open();
 
      final DownloadListener downloadListener = new DownloadListener() {
         public void setMinimum(final int min) {
            display.asyncExec(new Runnable() {
               public void run() {
                  if (!disposed) {
                     progressBar.setMinimum(min);
                  }
               }
            });
         }
         
         public void setMaximum(final int max) {
            display.asyncExec(new Runnable() {
               public void run() {
                  if (!disposed) {
                     progressBar.setMaximum(max);
                  }
               }
            });
         }
         
         public void setSelection(final int selection) {
            display.asyncExec(new Runnable() {
               public void run() {
                  if (!disposed) {
                     progressBar.setSelection(selection);
                  }
               }
            });
         }
         
         public void downloadEnded() {
            if (!disposed) {
               display.asyncExec(new Runnable() {
                  public void run() {
                     System.out.println("disposed = " + disposed);
                     if (!disposed) {                  
                        downloadButton.setText("Download");  
                     }
                  }
               });
            }
         }
      };
      
      downloadButton.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
            try {
               Button button = (Button) event.getSource();
               if (button.getText().equalsIgnoreCase("download")) {
                  if (downloadField.getText().trim().equals("")) return;
                  
                  File localFile = new File(localField.getText());
                  downloadThread = new DownloadThread(downloadListener, 
                                                      localFile,
                                                      new URL(downloadField.getText()));
                  downloadThread.start();
                  downloadButton.setText("Stop");
               }
               else {
                  downloadThread.terminate();
               }
            }
            catch(MalformedURLException e) {
               e.printStackTrace();
            }
         };
      });
 
      shell.addShellListener(new ShellAdapter() {
         public void shellClosed(ShellEvent se) {
            disposed = true;     
            if (downloadThread != null) {         
               downloadThread.terminate();
            }
         }
      });        
      
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      
      display.dispose();
   }
   
   public static void main(String[] args) {
      new Main();
   }
}
 
interface DownloadListener {
   public void setMinimum(int min);
   public void setMaximum(int max);
   public void setSelection(int selection);
   public void downloadEnded();
}
 
class DownloadThread extends Thread
{
   private int length = 0;
   private URL url = null;
   private File localFile = null;
   private DownloadListener listener = null;
   private boolean terminated = false;
   
   public DownloadThread(DownloadListener listener, File localFile, URL url) {
      this.url = url;
      this.localFile = localFile;
      this.listener = listener;
   }
   
   public void terminate() {
      terminated = true;
   }
   
   public void run() {
      try {
         System.out.println(url);
         URLConnection con = url.openConnection();
         System.out.println("Connecting...");
         con.connect();
         System.out.println("Connected!");
         int length = con.getContentLength();
 
         listener.setMinimum(0);
         listener.setMaximum(length);
          
         int block = 4096;
         int count = 0;
         FileOutputStream fos = new FileOutputStream(localFile);
         InputStream is = con.getInputStream();
         byte[] buff = new byte[block];
         int read = 0;
 
         listener.setSelection(0);
         while((read = is.read(buff, 0, block)) != -1) {
            byte[] bytes;
            if(read != buff.length) {
               bytes = new byte[read];
               System.arraycopy(buff, 0, bytes, 0, read);
            } 
            else {
               bytes = buff;
            }
            fos.write(bytes);
            count += read;
           
            if (terminated) {
               break;
            }
 
            listener.setSelection(count);
         }
         fos.flush();
         fos.close();
         
         listener.downloadEnded();
      } catch(Exception e) {
         System.out.println("Error downloading file " + url);
         e.printStackTrace();
      }
   }
}

Disabling 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.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 Enabled/Disabled 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("enabled");
      button1.setEnabled(true);
      button1.addSelectionListener(selectionListener);
      
      Button button2 = new Button(buttonGroup, SWT.PUSH);
      button2.setText("disabled");
      button2.setEnabled(false);
      button2.addSelectionListener(selectionListener);
      
      shell.open();

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

Creating an SWT table

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
 
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 Table Demonstration");
      shell.setLayout(new GridLayout());
 
      Table table;
      String[] columnTitles = { "Wine", "Vintage", "Price" };

      Object[][] tabledata = {
            { "Chateau Meyney, St. Estephe",       new Integer(1994), "$18.75"},
            { "Chateau Montrose, St. Estephe",     new Integer(1975), "$54.25" },
            { "Chateau Gloria, St. Julien",     new Integer(1993), "$22.99" },
            { "Chateau Beychevelle, St. Julien",   new Integer(1970), "$61.63" },
            { "Chateau La Tour de Mons, Margeaux", new Integer(1975), "$57.03" },
            { "Chateau Brane-Cantenac, Margeaux",  new Integer(1978), "$49.92" },
      };
 
      Group tableGroup = new Group(shell, SWT.NULL);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      tableGroup.setLayout(gridLayout);
      tableGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      tableGroup.setText("Table");
 
      table = new Table(tableGroup, SWT.FULL_SELECTION);
      table.setHeaderVisible(true);
       
      for (int i=0; i<columnTitles.length; i++) {
         TableColumn tableColumn = new TableColumn(table, SWT.NULL);
         tableColumn.setText(columnTitles[i]);
      }  
       
      for (int i=0; i<tabledata.length; i++) {
         TableItem tableItem = new TableItem (table, SWT.NULL);
         for (int j=0; j<columnTitles.length; j++) {
            tableItem.setText(j, ""+tabledata[i][j]);
         }
      }
 
      for (int i=0; i<columnTitles.length; i++) {
         TableColumn tableColumn = table.getColumn(i);
         tableColumn.pack();
      }      
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Creating a slider in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
 
public class Main {
   public static void main (String [] args) {
      Display display = new Display ();
      Shell shell = new Shell (display);
    
      shell.setBounds(200, 200, 250, 250);   
       
      Slider slider = new Slider (shell, SWT.VERTICAL);
      slider.setBounds (10, 10, 20, 200);
      final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP);
      styledText.setBounds(40, 10, 190, 200);
       
      slider.addListener (SWT.Selection, new Listener () {
         public void handleEvent (Event event) {
            String evt = "SWT.NONE";
            switch (event.detail) {
               case SWT.ARROW_DOWN: 
                  evt = "SWT.ARROW_DOWN";
                  break;
               case SWT.ARROW_UP: 
                  evt = "SWT.ARROW_UP"; 
                  break;
               case SWT.PAGE_DOWN: 
                  evt = "SWT.PAGE_DOWN"; 
                  break;
               case SWT.PAGE_UP: 
                  evt = "SWT.PAGE_UP"; 
                  break;
               case SWT.HOME: 
                  evt = "SWT.HOME"; 
                  break;
               case SWT.END: 
                  evt = "SWT.END"; 
                  break;
               case SWT.DRAG: 
                  evt = "SWT.DRAG"; 
                  break;
            }
            styledText.append(evt + "n");
            int offset = styledText.getOffsetAtLine(styledText.getLineCount()-1);
            styledText.setCaretOffset(offset);
            styledText.showSelection();
         }
      });
 
      shell.open ();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch ()) display.sleep ();
      }
   }
} 

Making a flat 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.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 Flat 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 | SWT.FLAT);
      button1.setText("orange");
      button1.addSelectionListener(selectionListener);
       
      Button button2 = new Button(buttonGroup, SWT.PUSH | SWT.FLAT);
      button2.setText("pear");
      button2.addSelectionListener(selectionListener);
 
      Button button3 = new Button(buttonGroup, SWT.PUSH | SWT.FLAT);
      button3.setText("apple"); 
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Creating a checked SWT Table

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.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
 
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 Table Demonstration");
      shell.setLayout(new GridLayout());
 
      final Table table;
      String[] columnTitles = { "Wine", "Vintage", "Price" };
 
      Object[][] tabledata = {
            { "Chateau Meyney, St. Estephe",       new Integer(1994), "$18.75"},
            { "Chateau Montrose, St. Estephe",     new Integer(1975), "$54.25" },
            { "Chateau Gloria, St. Julien",        new Integer(1993), "$22.99" },
            { "Chateau Beychevelle, St. Julien",   new Integer(1970), "$61.63" },
            { "Chateau La Tour de Mons, Margeaux", new Integer(1975), "$57.03" },
            { "Chateau Brane-Cantenac, Margeaux",  new Integer(1978), "$49.92" },
      };
 
      Group tableGroup = new Group(shell, SWT.NULL);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      tableGroup.setLayout(gridLayout);
      tableGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      tableGroup.setText("Table");
 
      table = new Table(tableGroup, SWT.FULL_SELECTION | SWT.CHECK);
      table.setHeaderVisible(true);
       
      for (int i=0; i<columnTitles.length; i++) {
         TableColumn tableColumn = new TableColumn(table, SWT.NULL);
         tableColumn.setText(columnTitles[i]);
      }  
       
      for (int i=0; i<tabledata.length; i++) {
         TableItem tableItem = new TableItem(table, SWT.NULL);
         for (int j=0; j<columnTitles.length; j++) {
            tableItem.setText(j, ""+tabledata[i][j]);
         }
      }
 
      for (int i=0; i<columnTitles.length; i++) {
         TableColumn tableColumn = table.getColumn(i);
         tableColumn.pack();
      }      
       
      shell.open();
       
      shell.addShellListener(new ShellAdapter() {
         public void shellClosed(ShellEvent se) {
            for (int i=0; i<table.getItemCount(); i++) {
               TableItem tableItem = table.getItem(i);
               System.out.println(tableItem.getText(0));
               System.out.println("tChecked: " + tableItem.getChecked());
            }                        
         }
      });
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
       
      display.dispose();
      
   }
}

Creating a scale in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main (String [] args) {
      Display display = new Display ();
      Shell shell = new Shell (display);
   
      shell.setBounds(200, 200, 250, 250);   
       
      final Scale scale = new Scale (shell, SWT.VERTICAL);
      scale.setMinimum(100);
      scale.setMaximum(200);
      scale.setBounds(10, 10, 30, 200);
      final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP);
      styledText.setBounds(50, 10, 180, 200);
       
      scale.addListener(SWT.Selection, new Listener () {
         public void handleEvent(Event event) {
            styledText.append(scale.getSelection() + "n");
            int offset = styledText.getOffsetAtLine(styledText.getLineCount()-1);
            styledText.setCaretOffset(offset);
            styledText.showSelection();
         }
      });
 
      shell.open ();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch ()) display.sleep ();
      }
   }
} 

Using a drop down combobox 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.Combo;
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 Combobox Demonstration");
      shell.setLayout(new GridLayout());
 
      String[] comboData = { "one", "two", "three", "four", "five",
                             "six", "seven", "eight", "nine", "ten",
                             "eleven", "twelve", "thirtheen", "fourteen",
                             "fifteen", "sixteen", "seventeen", "eighteen",
                             "nineteen", "twinty" };
 
      Group comboGroup = new Group(shell, SWT.NULL);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      comboGroup.setLayout(gridLayout);
      comboGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      comboGroup.setText("Editable Combo");
       
      Combo combo = new Combo(comboGroup, SWT.DROP_DOWN);
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Combo combo = ((Combo) event.widget);
            System.out.print(combo.getText());
            System.out.println(" selected = " + combo.getSelection());
         };
      };
      combo.addSelectionListener(selectionListener);
      combo.setItems(comboData);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}