A Java tooltip tutorial

Java Swing FAQ: How do I create a Java tooltip (also known as balloon or bubble help text)?

Some things in Swing are harder than others, but setting basic balloon "tooltip" help text on Java components (JLabel, JTextField, JComboBox, etc.) is actually very easy. For very basic rollover help text, all you have to do is set the "tool tip" text on the desired components with the setToolTipText method. This tooltip method is available on the JComponent class, and is then inherited by all Java component sub-classes.

In this Java tooltip tutorial we'll first look at a simple tooltip example, and then create a complete Java class that demonstrates the use of a tooltip on a variety of Java/Swing components.

A simple Java/Swing tooltip example

As a quick Java tooltip example, here's a snippet of code that shows how to set the tooltip/balloon help text on a JTextField:

JTextField textfield = new JTextField(10);
textfield.setToolTipText("Enter your username, up to ten characters in length, letter and numbers only.");

Here's how this text is display when I roll over this JTextField on Mac OS X 10.5.7:

As you can see, adding a tooltip to a JTextField is very simple. It's also very easy to add a tooltip to Java buttons and other components, and I'll show this next in a complete Java class.

A complete Java tooltip example

While that short tooltip example hopefully solves the problem, I thought I'd share the following complete Java class to demonstrate how this works with several different Java components. So, here's the complete source code for a Java class which constructs and displays a JFrame and adds tool tip help text to a JLabel JTextField and JButton:

package com.devdaily.tooltip;

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

/**
 * A simple Java class to demonstrate rollover help effects
 * with the use of tool tip (tooltip) help text.
 * 
 * Created by Alvin Alexander, devdaily.com
 */
public class JavaToolTipExample
{
  public static void main(String[] args)
  {
    new JavaToolTipExample();
  }

  public JavaToolTipExample()
  {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // create a label with tooltip help text
    JLabel tooltipLabel = new JLabel("Username");
    tooltipLabel.setToolTipText("Enter your username");
    
    // create a textfield with tooltip help text
    JTextField tooltipTextfield = new JTextField(10);
    tooltipTextfield.setToolTipText("Enter your username over here, that other thing is a label.");
    
    // create a button with tooltip help text
    JButton tooltipButton = new JButton("Click Me");
    tooltipButton.setToolTipText("Click this button to make something happen.");
    
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(label);
    frame.getContentPane().add(textfield);
    frame.getContentPane().add(button);

    // pack and center it
    frame.pack();
    frame.setLocationRelativeTo(null);

    frame.setVisible(true);
  }

}

The image above shows what this Java tooltip example class looks like when it is compiled and run on a Mac OS X 10.5.7 system.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.