Getting Started with Tool Tips

by John Zukowski

The Swing components support the ability to display brief popup messages when the user rests the mouse cursor over them. While the class used to display popup messages is JToolTip, you never create instances directly. Instead, you call the setToolTipText(String text) JComponent, which triggers the text to be displayed at the appropriate time.

To demonstrate, here's a simple program with two buttons that displays different text over each:

import javax.swing.\*;
import java.awt.\*;

public class ToolTipSample {

  public static void main(String args[]) {
    Runnable runner = new Runnable() {
      public void run() {
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button1 = new JButton("Hello, World");
        button1.setToolTipText("Hello, World");
        frame.add(button1, BorderLayout.NORTH);

        JButton button2 = new JButton("Goodbye, World");
        button2.setToolTipText("Goodbye, World");
        frame.add(button2, BorderLayout.SOUTH);

        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    };
    EventQueue.invokeLater(runner);
  }
}

Tooltip text is normally one line long. However, if the text string begins with <html> then the contents can be any HTML 3.2 formatted text.

import javax.swing.\*;
import java.awt.\*;

public class ToolTipSample {

  public static void main(String args[]) {
    Runnable runner = new Runnable() {
      public void run() {
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button1 = new JButton("Hello, World");
        button1.setToolTipText("<HTML><H1>Hello, World</H1>");
        frame.add(button1, BorderLayout.NORTH);

        JButton button2 = new JButton("Goodbye, World");
        button2.setToolTipText("<html><i>Goodbye," + 
                                 

"<strong>World</strong></i>");
        frame.add(button2, BorderLayout.SOUTH);

        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    };
    EventQueue.invokeLater(runner);
  }
}

There are various ways to customize the popup tooltip appearance. When the tooltip is defined for a specific component, prior to it being displayed, that component's createToolTip method is called. By overriding this method, you can modify settings like colors and fonts:

By overriding the contains (int x, int y) method of the component, you can also display position-sensitive tool tip text. Combining these two options, gives you the following button component:

JButton button1 = new JButton("Hello, World") {
  public JToolTip createToolTip() {
    JToolTip tip = super.createToolTip();
    tip.setBackground(Color.YELLOW);
    tip.setForeground(Color.RED);
    return tip;
  }
  public boolean contains(int x, int y) {
    if (x < 50) {
      setToolTipText("Got Green Eggs?");
    }  else {
      setToolTipText("Got Ham?");
    }
    return super.contains(x, y);
  }
};

Although the JToolTip is something of a passive object, in the sense that the JComponent creates and shows the JToolTip on its own, there are many more configurable aspects of its usage. However, these configurable aspects are the responsibility of the class that manages tooltips, and not the JToolTip itself. The class that manages tooltip usage is aptly named ToolTipManager.

With the Singleton design pattern, no constructor for ToolTipManager exists. Instead, you have access to the current manager through the static sharedInstance() method of ToolTipManager.

Once you have accessed the shared instance of ToolTipManager, you can customize when and if the tooltip text appears. There are five configurable properties:

  • dismissDelay
  • enabled
  • initialDelay
  • lightWeightPopupEnabled
  • reshowDelay

Initially, tooltips are enabled, but you can disable them with ToolTipManager.sharedInstance().setEnabled(false). This allows you to always associate tooltips with components, while letting the end user enable/disable them when desired.

There are three timing-oriented properties: initialDelay, dismissDelay, and reshowDelay. They all measure time in milliseconds. The initialDelay property is the number of milliseconds the user must rest the mouse inside the component before the appropriate tooltip text appears. The dismissDelay specifies the length of time the text appears while the mouse remains motionless; if the user moves the mouse, it also causes the text to disappear. The reshowDelay determines how long a user must remain outside a component before reentry would cause the popup text to reappear.

The remaining, property, lightWeightPopupEnabled is used to determine the popup window type to hold the tooltip text. If the property is true and the popup text fits entirely within the bounds of the top-level window, the text appears within a Swing JPanel. If this property is false and the popup text fits entirely within the bounds of the top-level window, the text appears within an AWT Panel. If part of the text wouldn't appear within the top-level window no matter what the property setting is, the popup text would appear within a Window.

Through careful use of the tooltips, your programs can be made more user-friendly. Don't abuse customizations though. While it is nice to be able to be creative and have custom behaviors and timings, once you move outside the norms of expectations, a user experience can suffer greatly.

\*\*\*\*\*\*\*\*

Java Mobile & Embedded Developer Days At a Glance

This conference is devoted solely to the technologies of mobile and embedded Java platforms and will be a unique opportunity for application developers of intermediate and advanced skill levels, platform developers, and technical experts at tool vendors, OEMs and carriers to get introduced to the community, to join in and collaborate. Attendees will enjoy a broad range of technical sessions, lightning talks, poster sessions, panels, hands-on labs, and participatory sessions. There will be plenty of opportunities to meet your peers and the experts from Sun and the industry, to network, and to discover how to apply these technologies to your own projects. For more information: https://developerdays.dev.java.net

Comments:

text to speech
text to speech using java and sql

Posted by kaleeswari on January 16, 2008 at 06:58 PM PST #

Post a Comment:
Comments are closed for this entry.
About

Java Technology Fundamentals are now covered as part of the Core Java Technology Tech Tips. Please go there for more tech tips.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today