How do I set the background of a tooltip in Swing?

I am developing a Swing application in which I need to show a tooltip for JTree nodes. Nodes represent specific tasks and execute in independent threads sequentially. I need to change the tooltip according to the current status of the tasks.

I am overriding the getToolTipText (MouseEvent e) method in a class that extends JTree. This also shows the tooltip, but the tooltip appearance of other components in my application is different than what is shown for Jtree nodes.

See the attached image for your desired look and feel. This tooltip is set to the JTabbedPane. enter image description here

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

I've tried html tags but it doesn't work. I also tried to customize the Jtree node appearance of the tooltip using the following code, but that doesn't work either. The color codes I used below are the same as in the above tip:

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 the desired image above?

+3


source to share


2 answers


You can override the method createToolTip

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();
    }

}

      

+4


source


This worked for me



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

+1


source







All Articles