Syntax error How to add tooltip to JLabel in Java?

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

Updated on: 2019-07-30T22:30:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements