Sunday 23 February 2014

Java: Putting an image in ToolTipText

Here is how an image can be added to a ToolTipText

public class ToolTipBasic
{
public static void main(String args[])
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGui();
}
});
}

private static void createAndShowGui()
{
JFrame frame = new JFrame("ToolTipText Tutorial");
Container contentPane = frame.getContentPane();
JButton b = new JButton("Button");
setToolTipImageAndBackground("This is tool tip text", b);
contentPane.add(b, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}

private static void setToolTipImageAndBackground(String toolTipText, JButton b)
{
Color backgroundColor = new Color(255, 255, 255);
UIManager.put("ToolTip.background", backgroundColor);
b.setToolTipText("<html><img src=\"" + ToolTipBasic.class.getResource("/images/Key.gif") + "\">" + toolTipText);
}

The program adds a WHITE background color to the tooltip and puts an image to the tool tip.
Here is the output:


No comments:

Post a Comment