Add selection listener to CoolItem : CoolItem « SWT « Java Tutorial






Add selection listener to CoolItem
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class CoolItemSelectionListener {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    CoolBar coolBar = new CoolBar(shell, SWT.BORDER);
    coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final CoolItem item = new CoolItem(coolBar, SWT.DROP_DOWN);
    item.setControl(createToolBar(coolBar));

    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("selected");
      }
    });

    Control control = item.getControl();
    Point pt = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    pt = item.computeSize(pt.x, pt.y);
    item.setSize(pt);

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

  private static Control createToolBar(Composite composite) {
    ToolBar toolBar = new ToolBar(composite, SWT.NONE);
    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("circle");
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("square");
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("star");
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("triangle");
    return toolBar;
  }
}








17.67.CoolItem
17.67.1.Add Combo to CoolBarAdd Combo to CoolBar
17.67.2.To add multiple controlsTo add multiple controls
17.67.3.Add selection listener to CoolItemAdd selection listener to CoolItem