How do I assign tool tips for JTabbedPane tabs?

To set a tool tips for JTabbedPane‘s tabs you need to pass the tool tips when adding a new tab to the JTabbedPane. The addTab() method accept the following parameters: the tab’s title, image icon, a component that is going to be the content of the tabs and a string of tool tips information.

When you hover your mouse to the JTabbedPane‘s tabs the tool tips will be shown on the screen. Here is an example how you can can add tool tips the JTabbedPane‘s tabs.

package org.kodejava.example.swing;

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

public class TabbedPaneToolTips extends JPanel {
    public TabbedPaneToolTips() {
        initializeUI();
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTabbedPane pane = new JTabbedPane();
        ImageIcon tab1Icon = new ImageIcon(
            this.getClass().getResource("/images/test-pass-icon.png"));
        ImageIcon tab2Icon = new ImageIcon(
            this.getClass().getResource("/images/test-fail-icon.png"));
        ImageIcon tab3Icon = new ImageIcon(
            this.getClass().getResource("/images/test-error-icon.png"));

        JPanel content1 = new JPanel();
        JPanel content2 = new JPanel();
        JPanel content3 = new JPanel();

        // Add tabs to the JTabbedPane. The last parameter in the
        // add tab method is the tool tips for the tabs.
        pane.addTab("Success", tab1Icon, content1,
            "Success Test Cases");
        pane.addTab("Fail", tab2Icon, content2,
            "Fail Test Cases");
        pane.addTab("Error", tab3Icon, content3,
            "Error Test Cases");

        this.add(pane, BorderLayout.CENTER);
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneToolTips();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TabbedPaneToolTips.showFrame();
            }
        });
    }
}

The result of the code snippet above is:

JTabbedPane Tabs’ Tool Tips Demo

How do I create a multiline tool tips?

package org.kodejava.example.swing;

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

public class MultilineToolTip {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Hover on me!");

        // Setting tool tip for our Swing JLabel component using an html
        // formatted string so that we can create a multi lines tool tip.
        label.setToolTipText(
            "<html>Lorem Ipsum is simply dummy text of the printing and<br/>" +
                "typesetting industry. Lorem Ipsum has been the industry's <br/>" +
                "standard dummy text ever since the 1500s, when an unknown<br/>" +
                "printer took a galley of type and scrambled it to make a<br/>" +
                "type specimen book.</html>");

        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().add(label);
        frame.setVisible(true);
    }
}

How do I disable / enable application tool tips?

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DisableToolTip extends JFrame {
    public DisableToolTip() throws HeadlessException {
        initComponent();
    }

    private void initComponent() {
        setSize(300, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

        JButton disable = new JButton("DISABLE");
        disable.setToolTipText("Application tool tip will be disabled.");
        disable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Disable tool tip for the entire application
                ToolTipManager.sharedInstance().setEnabled(false);
            }
        });

        JButton enable = new JButton("ENABLE");
        enable.setToolTipText("Application tool tip will be enabled.");
        enable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Enable tool tip for the entire application
                ToolTipManager.sharedInstance().setEnabled(true);
            }
        });

        getContentPane().add(enable);
        getContentPane().add(disable);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DisableToolTip().setVisible(true);
            }
        });
    }
}

How do I set a tool tip for swing component?

package org.kodejava.example.swing;

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

public class ToolTipExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Hover on me!");
        // Setting tool tip for our Swing JLabel component
        label.setToolTipText("My JLabel Tool Tip");

        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().add(label);
        frame.setVisible(true);
    }
}