Sunday, 23 February 2014

Java: ToolTipText

This post is a tutorial for ToolTipText. Following topics are covered

  • How to put a ToolTipText to a JButton
  • How to change the backgroud color of the ToolTipText

Here is the program that demonstrates the behavior

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");
setToolTipTextBackground("This is tool tip text", b);
contentPane.add(b, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}

private static void setToolTipTextBackground(String toolTipText, JButton b)
{
Color backgroundColor = new Color(255, 255, 255);
// Set tooltiptext background color using created Color
UIManager.put("ToolTip.background", backgroundColor);
b.setToolTipText(toolTipText);
}
}

I have removed the imports to make the program look shorter. And here is the output from the program:


No comments:

Post a Comment