Java Tutorial/SWT/ToolItem

Материал из Java эксперт
Перейти к: навигация, поиск

Add check buttons to ToolBar

import org.eclipse.swt.SWT;
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 ToolItemCheckBox{
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar bar = new ToolBar(shell, SWT.BORDER|SWT.VERTICAL);
    for (int i = 0; i < 4; i++) {
      ToolItem item = new ToolItem(bar, SWT.CHECK);
      item.setText("Item " + i);
    }
    bar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Add image to ToolItem

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 ToolItemImage {
  public static void main(String[] args) {
    Display display = new Display();
    Image image = new Image(display, "yourFile.gif");
    Shell shell = new Shell(display);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
    for (int i = 0; i < 12; i++) {
      ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);
      item.setImage(image);
    }
    toolBar.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    image.dispose();
    display.dispose();
  }
}





Add radio buttons to ToolBar

import org.eclipse.swt.SWT;
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 ToolItemRadioButton {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar bar = new ToolBar(shell, SWT.BORDER|SWT.VERTICAL);
    for (int i = 0; i < 4; i++) {
      ToolItem item = new ToolItem(bar, SWT.RADIO);
      item.setText("Item " + i);
    }
    bar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Add SelectionListener to ToolItem

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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolItemSelectionListenerAdding {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    shell.setLayout(gridLayout);
    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
    itemBack.setText("Back");
    ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
    itemForward.setText("Forward");
    ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
    itemStop.setText("Stop");
    ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
    itemRefresh.setText("Refresh");
    ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
    itemGo.setText("Go");
    GridData data = new GridData();
    data.horizontalSpan = 3;
    toolbar.setLayoutData(data);
    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        ToolItem item = (ToolItem) event.widget;
        String string = item.getText();
        if (string.equals("Back"))
          System.out.println("Back");
        else if (string.equals("Forward"))
          System.out.println("Forward");
        else if (string.equals("Stop"))
          System.out.println("Stop");
        else if (string.equals("Refresh"))
          System.out.println("Refresh");
        else if (string.equals("Go"))
          System.out.println("Go");
      }
    };
    itemBack.addListener(SWT.Selection, listener);
    itemForward.addListener(SWT.Selection, listener);
    itemStop.addListener(SWT.Selection, listener);
    itemRefresh.addListener(SWT.Selection, listener);
    itemGo.addListener(SWT.Selection, listener);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Add ToolItem to ToolBar

import org.eclipse.swt.SWT;
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 ToolBarToolItemAdd {
  public static void main(String[] args) {
    Shell shell = new Shell();
    ToolBar bar = new ToolBar(shell, SWT.BORDER);
    for (int i = 0; i < 8; i++) {
      ToolItem item = new ToolItem(bar, SWT.PUSH);
      item.setText("Item " + i);
    }
    bar.pack();
    shell.open();
    Display display = shell.getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Constants for Creating Tool Items

ConstantDescriptionSWT.CHECKCreates a stateful push button ("toggle" button).SWT.DROP_DOWNCreates a dropdown.SWT.PUSHCreates a traditional push button.SWT.RADIOCreates a grouped stateful push button (only one in the group may be selected at a time).SWT.SEPARATORCreates a separator.


Create a push button

import org.eclipse.swt.SWT;
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 ToolItemButtonPush{
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar bar = new ToolBar(shell, SWT.BORDER|SWT.VERTICAL);
    for (int i = 0; i < 4; i++) {
      ToolItem item = new ToolItem(bar, SWT.PUSH);
      item.setText("Item " + i);
    }
    bar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Creating Radio Groups

To create more than one group of radio buttons in the same toolbar, separate each group using a separator.



import org.eclipse.swt.SWT;
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 ToolItemRadioGroups {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar toolBar = new ToolBar(shell, SWT.BORDER | SWT.VERTICAL);
    ToolItem item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("One");
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Two");
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Three");
    new ToolItem(toolBar, SWT.SEPARATOR); // Signals end of group
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("One");
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Two");
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Three");
    toolBar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Two dropdowns

import org.eclipse.swt.SWT;
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 ToolItemDropDown {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar bar = new ToolBar(shell, SWT.BORDER|SWT.VERTICAL);
    for (int i = 0; i < 4; i++) {
      ToolItem item = new ToolItem(bar, SWT.DROP_DOWN);
      item.setText("asdf");
    }
    bar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Update a status line when the pointer enters a ToolItem

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * ToolBar example snippet: update a status line when the pointer enters a ToolItem
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolItemStatusLine {
  static String statusText = "";
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);
    final ToolBar bar = new ToolBar(shell, SWT.BORDER);
    bar.setBounds(10, 10, 150, 50);
    final Label statusLine = new Label(shell, SWT.BORDER);
    statusLine.setBounds(10, 90, 150, 30);
    new ToolItem(bar, SWT.NONE).setText("item 1");
    new ToolItem(bar, SWT.NONE).setText("item 2");
    new ToolItem(bar, SWT.NONE).setText("item 3");
    bar.addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent e) {
        ToolItem item = bar.getItem(new Point(e.x, e.y));
        String name = "";
        if (item != null) {
          name = item.getText();
        }
        if (!statusText.equals(name)) {
          statusLine.setText(name);
          statusText = name;
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Working with Dropdowns

To provide Combo-like functionality to a dropdown tool item:



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.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
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 ToolItemDropDownMenu {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar toolBar = new ToolBar(shell, SWT.BORDER | SWT.VERTICAL);
    ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);
    item.setText("One");
    DropdownSelectionListener listenerOne = new DropdownSelectionListener(item);
    listenerOne.add("Option One for One");
    listenerOne.add("Option Two for One");
    listenerOne.add("Option Three for One");
    item.addSelectionListener(listenerOne);
    toolBar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
class DropdownSelectionListener extends SelectionAdapter {
  private ToolItem dropdown;
  private Menu menu;
  public DropdownSelectionListener(ToolItem dropdown) {
    this.dropdown = dropdown;
    menu = new Menu(dropdown.getParent().getShell());
  }
  public void add(String item) {
    MenuItem menuItem = new MenuItem(menu, SWT.NONE);
    menuItem.setText(item);
    menuItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        MenuItem selected = (MenuItem) event.widget;
        dropdown.setText(selected.getText());
      }
    });
  }
  public void widgetSelected(SelectionEvent event) {
    if (event.detail == SWT.ARROW) {
      ToolItem item = (ToolItem) event.widget;
      Rectangle rect = item.getBounds();
      Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
      menu.setLocation(pt.x, pt.y + rect.height);
      menu.setVisible(true);
    } else {
      System.out.println(dropdown.getText() + " Pressed");
    }
  }
}