Multi-line ToolTipText

peter_budo 2 Tallied Votes 504 Views Share

Sometimes one line of tool tip is not enough. You may want to try and play around with JToolTip, but if you in hurry this "little hack" can be handy.
With little of HTML plus CSS code you can work out various formats (font, colour, alignment, size etc.).

Kevingon commented: Excellent tip :D +1
import javax.swing.*;
import java.awt.*;

public class ModifyToolTip {

  public static void main(String args[]) {
    Runnable runner = new Runnable() {
      public void run() {
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		
		JButton butt = new JButton("Hello, World");
        butt.setPreferredSize(new Dimension(120,55));
        butt.setToolTipText("<html><center><h1>Hello,</h1>"
        	+"<h2 style='color: red; font-style: italic;'>World</h2>"
        	+"<b>Modified<br/>ToolTipText</b></center>"
        	+"<br/>so simple.");
        panel.add(butt);
        frame.add(panel);        

        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    };
    EventQueue.invokeLater(runner);
  }
}
Kevingon 0 Newbie Poster

Thanks, this was exactly what I was looking for

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The ability to use (limited) HTML comes from JLabel, which is used to render tooltips. Many other Swing components (eg JList) also use JLabels to display a line of content, so this technique can be used to format text in many Swing components.

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

Thanks, this was exactly what I was looking for

I posted this 3 years ago, when I was finishing university so sorry for luck of info on it. Nevertheless I'm happy that someone found it useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.