How to set the background of tooltip in Swing?

2.4k views Asked by At

I am developing a Swing application in which I need to show the tooltip for JTree nodes. The nodes represent certain tasks and run in an independent threads sequentially. I need to change the tooltip as per the current status of the tasks.

I overrode getToolTipText(MouseEvent e) method in a class that extends JTree. This shows a tooltip too, but the look & feel of the tooltip of other components in my application is different than what is shown for Jtree nodes.

Please see the attached image of a desired look & feel of the tooltip. This tooltip is set on JTabbedPane. enter image description here

And here is the screenshot of the tooltip that is being displayed on Jtree: enter image description here

I tried html tags but that doesn't work. I also tried setting the look & feel of Jtree node tooltip by using following code, but this doesn't work either. The color codes I used below are same as the ones shown in desired tooltip above:

UIManager.put("ToolTip.background", new ColorUIResource(255, 247, 200)); // The color is #fff7c8.
Border border = BorderFactory.createLineBorder(new Color(76,79,83)); // The color is #4c4f53.
UIManager.put("ToolTip.border", border);

Can someone please tell me how can I set my JTree tooltip as shown in a desired image above?

2

There are 2 answers

4
BluesSolo On BEST ANSWER

You can override the createToolTip method from JTree in your tree class:

@Override
public JToolTip createToolTip() 
{
    JToolTip tooltip = super.createToolTip();
    tooltip.setBorder(BorderFactory.createLineBorder(new Color(76,79,83)));
    tooltip.setBackground(new Color(255, 247, 200));  
    return tooltip;
}

Example:

import java.awt.Color;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;


public class Main 
{

    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("Tooltip Example");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Node root = new Node("Root", "Root Tooltip");
        MyTree tree = new MyTree(root);
        root.add(new Node("Child 1", "Tooltip 1"));
        root.add(new Node("Child 2", "Tooltip 2"));
        tree.setToolTipText(""); // The correct tooltips will be shown on each node.
        frame.add(tree);

        frame.setVisible(true);
    }   

}

class Node extends DefaultMutableTreeNode
{
    String toolTip;

    public Node(String name, String toolTip)
    {
        super(name);
        this.toolTip = toolTip;
    }
    public String getToolTipText() 
    {
        return toolTip;
    }
}

class MyTree extends JTree
{
    MyTree(TreeNode node)
    {
        super(node);
    }

    @Override
    public JToolTip createToolTip() 
    {
        JToolTip tooltip = super.createToolTip();
        tooltip.setBorder(BorderFactory.createLineBorder(new Color(76,79,83)));
        tooltip.setBackground(new Color(255, 247, 200));  
        return tooltip;
    }

    @Override
    public String getToolTipText(MouseEvent event) 
    {
        super.getToolTipText(event);
        if(getRowForLocation(event.getX(), event.getY()) == -1)
            return null;
        TreePath path = getPathForLocation(event.getX(), event.getY());
        return ((Node) path.getLastPathComponent()).getToolTipText();
    }

}
0
ch.Joshi elijah On

This worked for me

UIManager.put("ToolTip.background", Color.white); UIManager.put("ToolTip.border",new LineBorder(Color.BLACK,1));