How to add tooltip to JLabel in Java?


Tooltip is visible whenever you will place the mouse cursor on the label. Use the setToolTipText() method to add tooltip to JLabel −

label.setToolTipText("This is a demo tooltip");

The following is an example to add tooltip to JLabel −

Example

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("Demo Label!");
      label.setFont(new Font("Verdana", Font.PLAIN, 14));
      label.setToolTipText("This is a demo tooltip");
      Border border = BorderFactory.createLineBorder(Color.ORANGE);
      label.setBorder(border);
      frame.add(label);
      frame.setSize(500,300);
      frame.setVisible(true);
   }
}

Output

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements