Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the color of the tooltip for only one component?

How do I change the color of the tooltip for only one component?

I know you can do the following for changing tooltip colors:

UIManager.put("ToolTip.background", new ColorUIResource(255, 247, 200)); 

But this changes the tooltip background for all component, not just one.

Any easy solutions?

like image 364
Andy Dingfelder Avatar asked Nov 12 '12 20:11

Andy Dingfelder


People also ask

How do I change the color of my tooltip?

Step 2: We will create an element with an icon and its tooltip value. Step 3: Now, we will use jQuery Code to add tooltip to the icon element and add its placement position. Step 4: At last, we will add CSS styles on tooltip element to change the color of the tooltip element.

How do I change the color of a selected item in JComboBox?

We can also set the foreground and background color to JComboBox items by using setForeground() and setBackground() methods of a JComboBox class.

How do I change the tooltip style?

The tooltip is automatically generated by web browsers, it actually can not be removed or changed. To change the tooltip style, we need to add a tooltip text to a different attribute, for example data-title , then use CSS code to create and customise the style. In WordPress, you can add the CSS code to your theme CSS.

How do I change the background color of tooltip in tableau?

Tableau doesn't offer functionality to change the background color of tooltip.


2 Answers

+1 to @MadProgrammer and @Reimeus for their advice and example.

These are both correct.

To add...

There is no default way to do this. You have to extend the ToolTip class, to create your own custom ToolTip with foreground and background color, and then extend JComponents class (JButton, JLabel, etc. are all JComponents ) and override itscreateToolTip() method and sets your custom ToolTip as the the JComponents ToolTip, like this :

Here is an example I made:

enter image description here

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;

/**
 *
 * @author David
 */
public class CustomJToolTipTest {

    private JFrame frame;

    public CustomJToolTipTest() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CustomJToolTipTest();
            }
        });
    }

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);


        JButton button = new JButton("button") {
            //override the JButtons createToolTip method
            @Override
            public JToolTip createToolTip() {
                return (new CustomJToolTip(this));
            }
        };
        button.setToolTipText("I am a button with custom tooltip");

        frame.add(button);

        frame.pack();
        frame.setVisible(true);
    }
}

class CustomJToolTip extends JToolTip {

    public CustomJToolTip(JComponent component) {
        super();
        setComponent(component);
        setBackground(Color.black);
        setForeground(Color.red);
    }
}
like image 50
David Kroukamp Avatar answered Oct 26 '22 20:10

David Kroukamp


You need to provide a custom JTooltip for the component.

Take a look at JComponent#createToolTip

From the Java Docs

Returns the instance of JToolTip that should be used to display the tooltip. Components typically would not override this method, but it can be used to cause different tooltips to be displayed differently.

like image 38
MadProgrammer Avatar answered Oct 26 '22 18:10

MadProgrammer