How to add tooltip to JLabel in Java?

Java 8Object Oriented ProgrammingProgramming

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

raja
Published on 09-May-2019 08:52:06
Advertisements