以下内容源网址:
http://jexp.ru/index.php/Java_Tutorial/SWT/ToolTip#Add_ToolTip_to_Shell_Window
Add ToolTip to Shell Window
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellWindowToolTip { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setToolTipText ("Shell \n toolTip"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Add ToolTip to TabItem
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; public class TabItemToolTip{ public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell (display); TabFolder folder = new TabFolder (shell, SWT.BORDER); folder.setSize (200, 200); TabItem item0 = new TabItem (folder, 0); item0.setToolTipText ("TabItem \n toolTip"); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }
Add ToolTip to ToolItem
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 ToolItemToolTip { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); ToolBar bar = new ToolBar(shell, SWT.BORDER); bar.setBounds(0, 20, 200, 64); ToolItem item1 = new ToolItem(bar, 0); item1.setToolTipText("ToolItem \n toolTip"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Create fake tool tips for items in a table
/******************************************************************************* * Copyright (c) 2000, 2005 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; /* * Tool Tips example snippet: create fake tool tips for items in a table * * 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.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; public class TooltipTableItem { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER); for (int i = 0; i < 20; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText("item " + i); } // Disable native tooltip table.setToolTipText(""); // Implement a "fake" tooltip final Listener labelListener = new Listener() { public void handleEvent(Event event) { Label label = (Label) event.widget; Shell shell = label.getShell(); switch (event.type) { case SWT.MouseDown: Event e = new Event(); e.item = (TableItem) label.getData("_TABLEITEM"); // Assuming table is single select, set the selection as if // the mouse down event went through to the table table.setSelection(new TableItem[] { (TableItem) e.item }); table.notifyListeners(SWT.Selection, e); shell.dispose(); table.setFocus(); break; case SWT.MouseExit: shell.dispose(); break; } } }; Listener tableListener = new Listener() { Shell tip = null; Label label = null; public void handleEvent(Event event) { switch (event.type) { case SWT.Dispose: case SWT.KeyDown: case SWT.MouseMove: { if (tip == null) break; tip.dispose(); tip = null; label = null; break; } case SWT.MouseHover: { TableItem item = table.getItem(new Point(event.x, event.y)); if (item != null) { if (tip != null && !tip.isDisposed()) tip.dispose(); tip = new Shell(shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL); tip.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); FillLayout layout = new FillLayout(); layout.marginWidth = 2; tip.setLayout(layout); label = new Label(tip, SWT.NONE); label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); label.setData("_TABLEITEM", item); label.setText(item.getText()); label.addListener(SWT.MouseExit, labelListener); label.addListener(SWT.MouseDown, labelListener); Point size = tip.ruputeSize(SWT.DEFAULT, SWT.DEFAULT); Rectangle rect = item.getBounds(0); Point pt = table.toDisplay(rect.x, rect.y); tip.setBounds(pt.x, pt.y, size.x, size.y); tip.setVisible(true); } } } } }; table.addListener(SWT.Dispose, tableListener); table.addListener(SWT.KeyDown, tableListener); table.addListener(SWT.MouseMove, tableListener); table.addListener(SWT.MouseHover, tableListener); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Show a tool tip inside a rectangle
/******************************************************************************* * 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; /* * Tool Tips example snippet: show a tool tip inside a rectangle * * 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.Color; import org.eclipse.swt.graphics.GC; 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; public class ToolTipRectangle { public static void main(String[] args) { Display display = new Display(); final Color[] colors = { display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_GREEN), display.getSystemColor(SWT.COLOR_BLUE), }; final Rectangle[] rects = { new Rectangle(10, 10, 30, 30), new Rectangle(20, 45, 25, 35), new Rectangle(80, 80, 10, 10), }; final Shell shell = new Shell(display); Listener mouseListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MouseEnter: case SWT.MouseMove: for (int i = 0; i < rects.length; i++) { if (rects[i].contains(event.x, event.y)) { String text = "ToolTip " + i; if (!(text.equals(shell.getToolTipText()))) { shell.setToolTipText("ToolTip " + i); } return; } } shell.setToolTipText(null); break; } } }; shell.addListener(SWT.MouseMove, mouseListener); shell.addListener(SWT.MouseEnter, mouseListener); shell.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { GC gc = event.gc; for (int i = 0; i < rects.length; i++) { gc.setBackground(colors[i]); gc.fillRectangle(rects[i]); gc.drawRectangle(rects[i]); } } }); shell.setSize(200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
如果觉得文章很好,请鼓励一下我吧:D爱的么么哒!
相关推荐
最近实现的一点ToolTip的小控件。可以随意改变tooltip的大小,颜色和背景颜色。 在vs2008上编译通过,可以直接拿来使用。
在这个windows 32源代码中使用Windows api演示了工具提示ToolTip控件的具体使用步骤和方法,剖析原理,清晰明了,对学习使用MFC ToolTip也有帮助。
为控件添加提示信息 ToolTipCtrl
在Disabled的控件上顯示ToolTip 在WINFORM中,如果一个控件有设置tooltip,那么鼠标移上去会显示出提示文字.但是当此控件的Enabled=false时,这个Tooltip是无效的. 本示例就是用来解决这个问题.
在sle控件及数据窗口字段上实现tooltip提示
这是一个在WPF中修改任何控件的Tooltip的样式的源代码,供大家参考获得自己想要的功能
echarts插件默认不支持axisLabel的tooltip功能,本demo主要展示axisLabel新增tooltip后的显示效果。有不懂,可以联系我。
Extjs4的textfield控件竟然没有tooltip悬浮提示,这应该算是个bug吧。网上查了很多,也没有方案。后来自己通过查询并整理出这个方案,终于实现了。 思想如下: 在textfield渲染时注册mouseover事件,然后在其中调用...
在siverlight中基于Popup浮动窗体控件模拟ToolTip的实现源码例子,
主要介绍了C#中datagridview使用tooltip控件显示单元格内容的方法,实例分析了C#控件的相关使用技巧,需要的朋友可以参考下
tooltip控件 groupBox控件
嵌入.NET控件到托盘图标Tooltip嵌入.NET控件到托盘图标Tooltip
对超长HTML控件内容展现ToolTip信息控件代码,当HTML控件长度固定,而显示内容又超长时,采用动态ToolTip方式显示剩余内容!!
ToolTip实现鼠标划过提示功能,简单实用
可以加到任何控件上的tooltip提示控件.zip
介绍C#使用GDI+重绘工具提示控件ToolTip,实现ToolTip自定义图标、大图标、小图标和透明显示,并可以轻松的更换ToolTip背景和边框颜色
各种风格的网页链接ToolTip显示控件,提供了基于ASP.NET+C#实现的服务器端动态ToolTip显示控件,也提供了纯js脚本的客户端ToolTip显示控件,该控件可以与不同的用户控件帮定。
vb.net中tooltip控件应用
很酷的tooltip信息显示控件源码很酷的tooltip信息显示控件源码
When you add a ToolTip control to a form, it is active by default. Therefore, you do not have to do anything to make it active. If you look at the source file for your form, you will see code that ...