import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.events.ShellListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; public class ComboTest { MyPopup popup; public static void main (String [] args) { Display display = new Display(); Shell shell = new ComboTest().open(display); // Event loop while (shell != null && ! shell.isDisposed()) { if (! display.readAndDispatch()) display.sleep(); } // Cleanup display.dispose(); } public Shell open(Display d){ Shell shell = new Shell(); createPartControl(shell); shell.pack(); shell.open(); return shell; } public void createPartControl(final Shell shell){ GridLayout gl = new GridLayout(2,false); gl.marginHeight = 10; gl.marginWidth = 10; shell.setLayout(gl); final Label l = new Label(shell,SWT.NONE); l.setText("Nothing Selected"); l.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); final Button b = new Button(shell,SWT.ARROW | SWT.DOWN); b.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent arg0) { showPopup(shell,l); } public void widgetDefaultSelected(SelectionEvent arg0) { }} ); } public void showPopup(Shell parentShell,Label l){ if (popup == null){ popup = new MyPopup(parentShell,l); } popup.show(l); } private class MyPopup { Shell shell; List list; Shell parentShell; Label label; public MyPopup(Shell arg0,Label arg1) { shell = new Shell(arg0,SWT.TOOL); parentShell = arg0; parentShell.addShellListener(new ShellListener(){ public void shellActivated(ShellEvent arg0) { } public void shellClosed(ShellEvent arg0) { } public void shellDeactivated(ShellEvent arg0) { if (!shell.isDisposed()) Display.getCurrent().asyncExec(new Runnable(){ public void run() { shell.setVisible(false); }} ); } public void shellDeiconified(ShellEvent arg0) { } public void shellIconified(ShellEvent arg0) { }} ); label = arg1; shell.setLayout(new FillLayout()); list = new List(shell,SWT.NONE); list.addMouseListener(new MouseListener(){ public void mouseDoubleClick(MouseEvent arg0) { } public void mouseDown(MouseEvent arg0) { list.setCapture(false); parentShell.setActive(); if (list.getBounds().contains(arg0.x,arg0.y)){ try { Display.getCurrent().getThread().sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } label.setText(list.getSelection()[0]); } shell.setVisible(false); } public void mouseUp(MouseEvent arg0) { }} ); list.add("Item 1"); list.add("Item 2"); list.add("Item 3"); list.add("Item 4"); shell.setSize(100,100); } public void show(Label l) { shell.setLocation(l.toDisplay(l.getLocation())); shell.setVisible(true); list.setCapture(true); } } }