Create fake tooltips for items in a SWT table : Tooltips « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » SWT JFace Eclipse » Tooltips 




Create fake tooltips for items in a SWT table
Create fake tooltips for items in a SWT table



/*
 * Tool Tips example snippet: 
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#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 Snippet125 {

  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 = (Labelevent.widget;
        Shell shell = label.getShell();
        switch (event.type) {
        case SWT.MouseDown:
          Event e = new Event();
          e.item = (TableItemlabel.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[] { (TableIteme.item });
          table.notifyListeners(SWT.Selection, e);
        // fall through
        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.TOOL);
            tip.setLayout(new FillLayout());
            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("tooltip " + 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.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}


           
       














Related examples in the same category
1.Create tool tips for a tab folder, tool bar and controlCreate tool tips for a tab folder, tool bar and control
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.