
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we change the background and foreground color of a JTooltip in Java?
In this article, we will learn to change the background and foreground color of a JTooltip. We will be using UIManager.put(), we customize the tooltip for a JLabel by setting a white background and green text, which will improve the appearance of the default tooltip.
What is a JToolTip?
A JToolTip is a subclass of the JComponent class, and we can create a tooltip for any Java component by using the 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().
How to change the background and foreground color of a JTooltip?
We can change both the background and foreground color of a JToolTip class by using the put() method of the UIManager class and passing the arguments ToolTip.background and ToolTip.foreground.
put() Method
The put() is a method of the UIManager class and is used to store an object in the developer defaults. It takes two parameters as input in the form of a key and a value.
- key: An object specifying the retrieval key.
- value: The object to store the value of the key.
Syntax
The following is the syntax for the put() method declaration:
public static Object put(Object key, Object value)
UIManager Class
UIManager determines what things look and feel like right now, the various looks and feels available to use, PropertyChangeListeners that are notified when the look and feel changes, the default look and feel, and easy ways to get various default values.
setToolTipText() Method
The setToolTipText() is a method of the JComponent Class and is used to set the tooltip text for the component. Takes a string as the input, and if the argument is null, then this method turns off the tooltip for the component.
Changing the background and foreground color of a JTooltip
To change the background and foreground color of a JTooltip, first, we will create a "JTooltipColorTest" class that extends the JFrame, declare a private JLabel named "label", and then initialize the constructor for the class. After that, set the title of the frame as "JTooltipColor Test" and for layout management, we use the FlowLayout.
Creates a JLabel with the text "Welcome to TutorialsPoint" and sets the tooltip text to "Simply Easy Learning" using the setToolTipText() method.
label = new JLabel("Welcome to TutorialsPoint"); label.setToolTipText("Simply Easy Learning");
Uses the UIManager class to change tooltip colors, using the ToolTip.background to change the background color to white, and using the ToolTip.foreground to change the foreground color to green.
UIManager.put("ToolTip.background", Color.white); UIManager.put("ToolTip.foreground", Color.green);
Adds the JLabel to the JFrame using the add() method of the JFrame:
add(label)
Example
Below is an example of changing the background and foreground color of a JTooltip in Java:
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(); } }