-
Notifications
You must be signed in to change notification settings - Fork 5
Tray Tutorial
Jin Liu edited this page Apr 1, 2015
·
4 revisions
Tray和TrayItem是指在任务栏上的能代表程序的图标,和前面提到的TaskBar类似。比如我们常见的QQ,Skype等程序运行之后,在屏幕的右下角会有一个图标。
先看一个示例吧:
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Tray Tutorial");
shell.setSize(300, 200);
RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.spacing = 0;
shell.setLayout(layout);
Image image = new Image(display,
TrayTutorial.class.getResourceAsStream("task.gif"));
CLabel label = new CLabel(shell, SWT.CENTER);
Tray tray = display.getSystemTray();
if (tray == null) {
label.setText("This platform does NOT support the tray.");
} else {
label.setText("Please visit the icon ");
new CLabel(shell, SWT.CENTER).setImage(image);
new CLabel(shell, SWT.CENTER).setText(" on System TaskBar.");
// Create tray item.
TrayItem item = new TrayItem(tray, SWT.NONE);
// Set images.
item.setImage(image);
item.setHighlightImage(image);
// Add tool tip.
final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON);
toolTip.setText("Foxes vs. Dogs");
toolTip.setMessage("A quick brown fox jumps over the lazy dog.");
item.setToolTip(toolTip);
// Add Selection and DefaultSelection Listener.
item.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
shell.moveAbove(null);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
toolTip.setVisible(true);
}
});
// Add Menu Listener.
final Menu menu = new Menu(shell, SWT.POP_UP);
for (int i = 0; i < 5; i++) {
MenuItem menuItem = new MenuItem(menu, SWT.NONE);
menuItem.setImage(image);
menuItem.setText("MenuItem - " + i);
}
new MenuItem(menu, SWT.SEPARATOR);
MenuItem exit = new MenuItem(menu, SWT.NONE);
exit.setText("Exit");
exit.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
shell.dispose();
}
});
item.addMenuDetectListener(new MenuDetectListener() {
@Override
public void menuDetected(MenuDetectEvent e) {
menu.setVisible(true);
}
});
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
image.dispose();
display.dispose();
运行此段代码,会在TaskBar上出现一个图标,双击会出现ToolTip,右键点击会出现菜单。
参考资料:
上一篇:ToolTip Tutorial 下一篇:Link Tutorial
Soyatec Eclipse Plug-in and RCP Tutorials, wrote by Jin Liu (jin.liu@soyatec.com).
Eclipse Plug-in Development Tutorial
-
Standard Widget Toolkit Tutorial
-
Widgets Tutorial
- Label Tutorial
- Button Tutorial
- Text Tutorial
- Combo Tutorial
- List Tutorial
- Scale Tutorial
- Slider Tutorial
- Spinner Tutorial
- DateTime Tutorial
- Table Tutorial
- Tree Tutorial
- ToolBar Tutorial
- CoolBar Tutorial
- ProgressBar Tutorial
- ScrollBar Tutorial
- TaskBar Tutorial
- ExpandBar Tutorial
- Menu Tutorial
- Sash Tutorial
- ToolTip Tutorial
- Tray Tutorial
- Link Tutorial
- Browser Tutorial
- Dialog Tutorial
- Common Properties Tutorial
- Components Tutorial
- Shell Tutorial
- Display Tutorial
- SWT Custom Widgets Tutorial
- Drag and Drop Tutorial
- Layouts Tutorial
- Resource Management Tutorial
-
Widgets Tutorial
-
[JFace Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Tutorial)
- [JFace Viewers Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Viewers-Tutorial)
- [TableViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/TableViewer-Tutorial)
- [TreeViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/TreeViewer-Tutorial)
- [ListViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ListViewer-Tutorial)
- [ComboViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ComboViewer-Tutorial)
- [JFace Dialogs Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Dialogs-Tutorial)
- [JFace DataBinding Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-DataBinding-Tutorial)
- [JFace Viewers Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Viewers-Tutorial)
-
[Plug-in Development Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Plug-in-Development-Tutorial)
- [Views Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Views-Tutorial)
- [Editors Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Editors-Tutorial)
- [Action and Menus Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Action-and-Menus-Tutorial)
- [Preferences Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Preferences-Tutorial)
- [ExtensionPoint Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ExtensionPoint-Tutorial)
- [Publish Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Publish-Tutorial)
- [Feature Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Feature-Project-Tutorial)
- [UpdateSite Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/UpdateSite-Project-Tutorial)
- [Fragment Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Fragment-Project-Tutorial)
-
[Eclipse 3.x RCP Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Eclipse-3.x-RCP-Tutorial)
- [Branding Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Branding-Tutorial)
- [Publish RCP Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Publish-RCP-Tutorial)
-
[Graphical Editing Framework Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/GEF-Tutorial)