Java Swing GUI

How to Display Multiple Lines in Tooltip

In this tutorial, we are going to see how to display multiple lines in a tooltip in Java Swing. Let’s first see how to define the text in a tooltip in a component.

JButton button = new JButton("Hover over this button!");
button.setToolTipText("This is a tooltip");

To display the text in multiple lines in a tooltip, use HTML. Here we have used <br> tag for a line break and this would create multiple lines in a tooltip.

JButton button = new JButton("Hover over this button!");
button.setToolTipText("<html>" + "This is a" + "<br>" + "tooltip" + "</html>");


 

Complete Example: Java Program to Display Multiple Lines in Tooltip:
import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame
{
  private void buildeGUI()
  {
    JButton button = new JButton("Hover over this button!");
    button.setToolTipText("" + "This is a" + "
" + "tooltip" + "");
     
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(button);
  }
   
  public static void main(String[] args)
  {
    MyFrame f = new MyFrame() ;
    f.setSize(300, 150) ;
    f.buildeGUI();
    f.setVisible( true ) ;
  }
}

Output:


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *