Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement tooltips for JTree?

since JTree & TreeModel don't provide tooltips straight out-of-the-box, what do you think, what would be the best way to have item-specific tooltips for JTree?

Edit: (Answering my own question afterwards.)

@Zarkonnen: Thanks for the getTooltipText idea.

I found out another (maybe still a bit nicer) way with overriding DefaultTreeCellRenderer and thought to share it:

public class JTreeWithToolTips {
    private static class OwnRenderer extends DefaultTreeCellRenderer {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean sel, boolean expanded, boolean leaf, int row,
                boolean hasFocus) {
            setToolTipText("foobar" + row);
            return super.getTreeCellRendererComponent(tree, value, sel,
                    expanded, leaf, row, hasFocus);
        }
    }

    public static void main(String[] args) {
        JTree tree = new JTree(new Object[] { "foo", "bar", "foobar" });
        tree.setCellRenderer(new OwnRenderer());
        ToolTipManager.sharedInstance().registerComponent(tree);

        JFrame frame = new JFrame();
        frame.getContentPane().add(tree);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
like image 959
Touko Avatar asked Nov 07 '08 13:11

Touko


People also ask

Which method is used to add tooltip text to almost all?

We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.

Which method is used to set the tooltip text?

Use the setToolTipText method to set up a tool tip for the component.

What is tooltip in Java?

Tooltips are small windows of text that popup when the user leaves the mouse cursor over a component for a second or two. They are used to explain the functionality of the component. Tooltips are an integral part of Swing components.


3 Answers

See getTooltipText on JTree. This should allow you to show tooltips depending on what in the tree is being hovered over. (Do read the docs though, you need to register the JTree with the ToolTipManager.)

like image 118
Zarkonnen Avatar answered Sep 24 '22 15:09

Zarkonnen


Yeah, you can use onMouseMoved and then use a method (I don't remember the name) that tells you in which node you are over. If you get null, obviously then you are not over a node.

like image 27
Agustin Avatar answered Sep 21 '22 15:09

Agustin


When dealing with specific TreeNode subclasses, based on your own answer and comments, I came up with an interface for my TreeNode to implement.

Notice how we check if the value is an intance of Tooltipable in the TreeCellRenderer:

public static interface Tooltipable {
    public String getToolTip();
}

public static class TheNode extends DefaultMutableTreeNode implements Tooltipable {

    private String shortDesc, longDesc;

    public TheNode(String shortDesc, String longDesc) {
        super();
        this.shortDesc = shortDesc;
        this.longDesc = longDesc;
    }

    @Override
    public String getToolTip() {
        return longDesc;
    }

    @Override
    public String toString() {
        return shortDesc;
    }
}

public static class TheModel extends DefaultTreeModel {
    public TheModel() {
        super(new TheNode("Root", "The base of everything"));
        TheNode root = (TheNode)getRoot();
        root.add(new TheNode("Second", "I am a number two"));
        TheNode node = new TheNode("Third", "Another one bites the dust");
        root.add(node);
        node.add(new TheNode("Last", null)); // No tooltip for this one
    }
}

public static class TreeTooltipRenderer extends DefaultTreeCellRenderer {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof Tooltipable)
            setToolTipText(((Tooltipable)value).getToolTip());
        return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree(new TheModel());
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.setCellRenderer(new TreeTooltipRenderer());
    frame.add(new JScrollPane(tree), BorderLayout.CENTER);
    frame.setVisible(true);
}
like image 1
Matthieu Avatar answered Sep 25 '22 15:09

Matthieu