Use images in tooltips in Java

Description

The following code shows how to use images in tooltips.

Example


   // w  w w  .  ja v  a  2s .  c  o  m


import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

  public static void main(String[] args) {

    JFrame frame = new JFrame();
    JLabel label = new JLabel("Label with image in Tooltip!");
    label.setToolTipText("<html><img src=\"" + Main.class.getResource("tooltip.gif")
        + "\"> Tooltip ");
    label.setHorizontalAlignment(JLabel.CENTER);
    frame.setContentPane(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 200, 100);
    frame.setVisible(true);

  }
}

The code above generates the following result.

Use images in tooltips in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer