Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:27:59+00:00 2026-06-12T04:27:59+00:00

I’m creating a JTree and adding some nodes doing something like this: DefaultMutableTreeNode top

  • 0

I’m creating a JTree and adding some nodes doing something like this:

DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
tree = new JTree(top);
...
DefaultMutableTreeNode node = new DefaultMutableTreeNode("<html><a href=\"    \">"+code+"</a>  "+description+"</html>");
top.add(node);
...

Now I added a “addMouseMotionListener” to the node which is ok. My problem is that the mouse cursor changes whenever I hover over any part of the node. What I really want is to only change mouse cursor when hovering over the HTML hyperlink text part of the node

 "<a href=\"\">"+code+"</a>"

and NOT the description.

So is there a way to make the mouse cursor change only when hovering in certain parts of the node?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:28:01+00:00Added an answer on June 12, 2026 at 4:28 am

    OK, it took me a while, but what I described in my comment seems to work. There may be other/better ways and I would love to read about it, but so far this is all I have found.

    The idea is that:

    1. I use a JEditorPane as a TreeCellRenderer
    2. I listen for mouse move (and also, as a bonus for mouse click)
    3. For each event I recreate the renderer for the hovered cell
    4. I translate the event to the component coordinates
    5. I use viewToModel to find if I am hovering an anchor element
    6. I change the cursor accordingly.

    Here is the code and as a bonus, you also get working hyperlinks!

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Desktop;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.Element;
    import javax.swing.text.html.HTML;
    import javax.swing.text.html.HTMLDocument;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeCellRenderer;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreeNode;
    import javax.swing.tree.TreePath;
    
    public class TestTreeHyperlinks {
    
        private final class HyperlinkMouseListener extends MouseAdapter {
            private final JTree tree;
    
            private HyperlinkMouseListener(JTree tree) {
                this.tree = tree;
            }
    
            @Override
            public void mouseExited(MouseEvent e) {
                tree.setCursor(Cursor.getDefaultCursor());
            }
    
            @Override
            public void mouseClicked(MouseEvent e) {
                Element h = getHyperlinkElement(e);
                if (h != null) {
                    Object attribute = h.getAttributes().getAttribute(HTML.Tag.A);
                    if (attribute instanceof AttributeSet) {
                        AttributeSet set = (AttributeSet) attribute;
                        String href = (String) set.getAttribute(HTML.Attribute.HREF);
                        if (href != null) {
                            try {
                                Desktop.getDesktop().browse(new URI(href));
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            } catch (URISyntaxException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
            }
    
            @Override
            public void mouseMoved(MouseEvent event) {
                boolean isHyperlink = isHyperlink(event);
                if (isHyperlink) {
                    tree.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                } else {
                    tree.setCursor(Cursor.getDefaultCursor());
                }
    
            }
    
            private boolean isHyperlink(MouseEvent event) {
                return getHyperlinkElement(event) != null;
            }
    
            private Element getHyperlinkElement(MouseEvent event) {
                Point p = event.getPoint();
                int selRow = tree.getRowForLocation(p.x, p.y);
                TreeCellRenderer r = tree.getCellRenderer();
                if (selRow != -1 && r != null) {
                    TreePath path = tree.getPathForRow(selRow);
                    Object lastPath = path.getLastPathComponent();
                    Component rComponent = r.getTreeCellRendererComponent(tree, lastPath, tree.isRowSelected(selRow), tree.isExpanded(selRow),
                            tree.getModel().isLeaf(lastPath), selRow, true);
                    if (rComponent instanceof JEditorPane) {
                        Rectangle pathBounds = tree.getPathBounds(path);
                        JEditorPane editor = (JEditorPane) rComponent;
                        editor.setBounds(tree.getRowBounds(selRow));
                        p.translate(-pathBounds.x, -pathBounds.y);
                        int pos = editor.getUI().viewToModel(editor, p);
                        if (pos >= 0 && editor.getDocument() instanceof HTMLDocument) {
                            HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
                            Element elem = hdoc.getCharacterElement(pos);
                            if (elem.getAttributes().getAttribute(HTML.Tag.A) != null) {
                                return elem;
                            }
                        }
                    }
                }
                return null;
            }
        }
    
        @SuppressWarnings("serial")
        public class MyTreeCellRenderer extends DefaultTreeCellRenderer implements TreeCellRenderer {
    
            private JEditorPane editorPane;
    
            public MyTreeCellRenderer() {
                editorPane = new JEditorPane("text/html", null);
            }
    
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row,
                    boolean hasFocus) {
                Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                if (c instanceof JLabel) {
                    JLabel label = (JLabel) c;
                    editorPane.setText(label.getText());
                    editorPane.setToolTipText(label.getToolTipText());
                    editorPane.setOpaque(label.isOpaque());
                    editorPane.setBackground(label.getBackground());
                    editorPane.setBorder(label.getBorder());
                }
                return editorPane;
            }
        }
    
        protected void initUI() {
            final JTree tree = new JTree(getTreeModel());
            tree.setCellRenderer(new MyTreeCellRenderer());
            HyperlinkMouseListener listener = new HyperlinkMouseListener(tree);
            tree.addMouseListener(listener);
            tree.addMouseMotionListener(listener);
            JFrame f = new JFrame(TestTreeHyperlinks.class.getSimpleName());
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(tree), BorderLayout.CENTER);
            f.pack();
            f.setSize(f.getWidth() + 100, f.getHeight() + 100);
            f.setVisible(true);
    
        }
    
        private TreeModel getTreeModel() {
            return new DefaultTreeModel(
                    getNodes(new DefaultMutableTreeNode("<html>Root <a href=\"http://www.google.be\">Google</a></html>"), 5));
        }
    
        private TreeNode getNodes(DefaultMutableTreeNode parent, int i) {
            if (i > 0) {
                for (int j = 0; j < 5; j++) {
                    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
                            "<html>Node "
                                    + (j + 1)
                                    + " <a href=\"http://www.stackoverflow.com\">a link to stackoverflow</a></html> and some more text not in an hyperlink");
                    getNodes(newChild, i - 1);
                    parent.add(newChild);
                }
            }
            return parent;
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestTreeHyperlinks().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.