TIP: Images in Tooltips

Here is a simple tip:

The Swing tooltips support html syntax via use of the <html> tag. This can be exploited to show images in tooltips.

For example, with the following layout:

src
   org
      mypackage
         ImageInTooltip.java
         tooltip.gif
and source code:
package org.mypackage;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageInTooltip {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JLabel label = new JLabel("Label with image in Tooltip!");
		label.setToolTipText(
			"<html><img src=\\"" +
			ImageInTooltip.class.getResource("tooltip.gif") +
			"\\"> Tooltip "
			);
		label.setHorizontalAlignment(JLabel.CENTER);
		frame.setContentPane(label);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(100, 100, 200, 100);
		frame.setVisible(true);
	}
}
give you this:

Comments:

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today