954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Multi-line ToolTipText

By peter_budo on Dec 26th, 2008 10:44 pm

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.).

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);
  }
}

Thanks, this was exactly what I was looking for

Kevingon
Newbie Poster
11 posts since Sep 2011
Reputation Points: 19
Solved Threads: 1
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: