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.swing;

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

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

    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(TabbedPaneToolTips::showFrame);
    }

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

        JTabbedPane pane = new JTabbedPane();
        ImageIcon tab1Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-pass-icon.png")));
        ImageIcon tab2Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-fail-icon.png")));
        ImageIcon tab3Icon = new ImageIcon(Objects.requireNonNull(
                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);
    }
}

The result of the code snippet above is:

JTabbedPane Tabs’ Tool Tips Demo

How do I create a multiline tool tips?

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;

public class MultilineToolTip {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(500, 500);
        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);
    }
}
Multiline Tool Tips

Multiline Tool Tips

How do I disable/enable application tool tips?

package org.kodejava.swing;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

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

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

    private void initComponent() {
        setSize(500, 500);
        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(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(e -> {
            // Enable tool tip for the entire application
            ToolTipManager.sharedInstance().setEnabled(true);
        });

        getContentPane().add(enable);
        getContentPane().add(disable);
    }
}
Disable or Enable Tool Tips

Disable or Enable Tool Tips

How do I set a tool tip for Swing components?

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;

public class ToolTipExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tool Tip Demo");
        frame.setSize(500, 500);
        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);
    }
}
Swing's Tool Tip Demo

Swing’s Tool Tip Demo