1、使用Shell的
Tool Tips 实现图像内的
Tool Tips 。
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 ();
}
2、使用Shell实现Tool Tip的功能。
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 ();
//使用Shell实现Tool Tip的功能
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.computeSize (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();
}