Example usage for javax.swing.tree TreeCellRenderer getTreeCellRendererComponent

List of usage examples for javax.swing.tree TreeCellRenderer getTreeCellRendererComponent

Introduction

In this page you can find the example usage for javax.swing.tree TreeCellRenderer getTreeCellRendererComponent.

Prototype

Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus);

Source Link

Document

Sets the value of the current tree cell to value.

Usage

From source file:Main.java

private Element getHyperlinkElement(MouseEvent event) {
    Point p = event.getPoint();//from w w w. ja va2  s . c o m
    int selRow = tree.getRowForLocation(p.x, p.y);
    TreeCellRenderer r = tree.getCellRenderer();
    if (selRow == -1 || r == null) {
        return 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 == false) {
        return null;
    }
    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;
}