Java Tutorial/SWT/ToolBar
Содержание
Add a combo box in a tool bar
<source lang="java">
/*******************************************************************************
* 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 *******************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.rubo; 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 ToolBarCombo {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); ToolBar bar = new ToolBar(shell, SWT.BORDER); for (int i = 0; i < 4; i++) { ToolItem item = new ToolItem(bar, 0); item.setText("Item " + i); } ToolItem sep = new ToolItem(bar, SWT.SEPARATOR); int start = bar.getItemCount(); for (int i = start; i < start + 4; i++) { ToolItem item = new ToolItem(bar, 0); item.setText("Item " + i); } Combo combo = new Combo(bar, SWT.READ_ONLY); for (int i = 0; i < 4; i++) { combo.add("Item " + i); } combo.pack(); sep.setWidth(combo.getSize().x); sep.setControl(combo); bar.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Add Buttons to ToolBar
<source lang="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.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 ToolBarButtonsAdding {
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(); }
}</source>
A toolbar can be horizontal or vertical.
<source lang="java">
ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL); ToolBar toolBar = new ToolBar(shell, SWT.VERTICAL);</source>
<source lang="java">
/*******************************************************************************
* 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: place a drop down menu in a tool bar * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; 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 ToolBarDropDown {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); final ToolBar toolBar = new ToolBar(shell, SWT.NONE); final Menu menu = new Menu(shell, SWT.POP_UP); for (int i = 0; i < 8; i++) { MenuItem item = new MenuItem(menu, SWT.PUSH); item.setText("Item " + i); } final ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN); item.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { if (event.detail == SWT.ARROW) { Rectangle rect = item.getBounds(); Point pt = new Point(rect.x, rect.y + rect.height); pt = toolBar.toDisplay(pt); menu.setLocation(pt.x, pt.y); menu.setVisible(true); } } }); toolBar.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } menu.dispose(); display.dispose(); }
}</source>
ToolBar Constants
ConstantDescriptionSWT.FLATMakes the toolbar items flat; only the button under the mouse pointer appears raised.SWT.WRAPWraps the toolbar buttons; this style has no effect on Windows.SWT.RIGHTRight aligns the toolbar.SWT.HORIZONTALDraws a horizontal toolbar.SWT.VERTICALDraws a vertical toolbar.SWT.SHADOW_OUTDraw a shadow around the toolbar.
ToolBar: create tool bar with normal, hot and disabled images.
<source lang="java">
/*******************************************************************************
* 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 *******************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; 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 ToolBarNormalHotDisabledImage {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Image image = new Image(display, 20, 20); Color color = display.getSystemColor(SWT.COLOR_BLUE); GC gc = new GC(image); gc.setBackground(color); gc.fillRectangle(image.getBounds()); gc.dispose(); Image disabledImage = new Image(display, 20, 20); color = display.getSystemColor(SWT.COLOR_GREEN); gc = new GC(disabledImage); gc.setBackground(color); gc.fillRectangle(disabledImage.getBounds()); gc.dispose(); Image hotImage = new Image(display, 20, 20); color = display.getSystemColor(SWT.COLOR_RED); gc = new GC(hotImage); gc.setBackground(color); gc.fillRectangle(hotImage.getBounds()); gc.dispose(); ToolBar bar = new ToolBar(shell, SWT.BORDER | SWT.FLAT); bar.setSize(200, 32); for (int i = 0; i < 12; i++) { ToolItem item = new ToolItem(bar, 0); item.setImage(image); item.setDisabledImage(disabledImage); item.setHotImage(hotImage); if (i % 3 == 0) item.setEnabled(false); } shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } image.dispose(); disabledImage.dispose(); hotImage.dispose(); display.dispose(); }
}</source>
ToolBar: wrap on resize
<source lang="java">
/*******************************************************************************
* 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 *******************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; 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 ToolBarWrapResize {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); final ToolBar toolBar = new ToolBar(shell, SWT.WRAP); for (int i = 0; i < 12; i++) { ToolItem item = new ToolItem(toolBar, SWT.PUSH); item.setText("Item " + i); } shell.addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { Rectangle rect = shell.getClientArea(); Point size = toolBar.ruputeSize(rect.width, SWT.DEFAULT); toolBar.setSize(size); } }); toolBar.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>