Java Tutorial/SWT/CoolItem

Материал из Java эксперт
Версия от 15:20, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Combo to CoolBar

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.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.ruposite;
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;
public class CoolBarCombo {
  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);
    Composite c = new Composite(coolBar, SWT.NONE);
    c.setLayout(new FillLayout());
    Combo combo = new Combo(c, SWT.DROP_DOWN);
    combo.add("Option One");
    combo.add("Option Two");
    combo.add("Option Three");
    item.setControl(c);
    Control control = item.getControl();
    Point pt = control.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    pt = item.ruputeSize(pt.x, pt.y);
    item.setSize(pt);
    coolBar.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





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.ruposite;
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.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    pt = item.ruputeSize(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;
  }
}





To add multiple controls

To add multiple controls, create a composite, add the controls to the composite, and pass the composite to the cool item"s setControl() method.



import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
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;
public class CoolBarComposite {
  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);
    Composite c = new Composite(coolBar, SWT.NONE);
    c.setLayout(new GridLayout(1, false));
    new Button(c, SWT.PUSH).setText("Button One");
    new Button(c, SWT.PUSH).setText("Button Two");
    item.setControl(c);
    Control control = item.getControl();
    Point pt = control.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    pt = item.ruputeSize(pt.x, pt.y);
    item.setSize(pt);
    coolBar.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}