How can we change the background and foreground color of a JTooltip in Java?


A JToolTip is a subclass of JComponent class and we can create a tooltip for any java component by using setToolTipText() method, it can be used to set up a tooltip for the component. The important methods of a JToolTip class are getAccessibleContext(), getComponent(), paramString() and updateUI(). We can change both the background and foreground color of a JToolTip class by using the put() method of UIManager class and pass the arguments ToolTip.background and ToolTip.foreground.

Example

import java.awt.*;
import javax.swing.*;
public class JTooltipColorTest extends JFrame {
   private JLabel label;
   public JTooltipColorTest() {
      setTitle("JTooltipColor Test");
      setLayout(new FlowLayout());
      label = new JLabel("Welcome to TutorialsPoint");
      label.setToolTipText("Simply Easy Learning");
      UIManager.put("ToolTip.background", Color.white); // to change background color of a JTtoolTip
      UIManager.put("ToolTip.foreground", Color.green); // to change foreground color of a JToolTip
      add(label);
      setSize(400, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JTooltipColorTest();
   }
}

Output

Updated on: 10-Feb-2020

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements