lolero
lolero

Reputation: 1273

How to reach/trigger the target of a hyperlink inside a JEditorPane/JTextPane programmatically

I have a closed source third party application whose windows I am trying to control from within my java program. I manage to run the main method of the third-party application and intercept the window events it generates using an instance of AWTEventListener. Then I iterate through the components of the windows it generates in order to find and manipulate the necessary swing controls. Finding the components, clicking on buttons, activating toggle buttons, and modifying text fields works fine but there is a hyperlink inside a JTextPane that I have not been able to trigger programmatically, nor have I found any information online on how to do it successfully. The suggestions here and here looked promising but I was not able to trigger the hyperlink with the MouseEvent. I should also point out that the hyperlink is not to a URL but to an internal function. When I invoke the JTextPane getText() method, I get:

<html>
  <head>

  </head>
  <body>
    <a href="#action">Expand Window</a>
  </body>
</html>

My question: Is there is a way to directly reach the target of a hyperlink inside a JTextPane (in this case, to expand the window) as opposed to trying to have a MouseEvent simulate a click on it?

Upvotes: 2

Views: 556

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

I don't know but probably this example will help you.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.LinkedHashSet;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;


public class TextPaneTest {


    public static void main(String[] args) {
        final JFrame frm = new JFrame("Editor pane test");

        final JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setText("<html>Here is the text with a <a href=\"http://google.com\">link</a></html>");
        frm.add(new JScrollPane(pane));
        final JButton btn = new JButton(new AbstractAction("Find link") {

            @Override
            public void actionPerformed(ActionEvent e) {
                final HTMLDocument doc = (HTMLDocument) pane.getDocument();
                final Collection<String> links = new LinkedHashSet<String>();
                // probably exists a better way to iterate over elements
                for (int i = 0; i < doc.getLength(); i++) {
                    final Element el = doc.getCharacterElement(i);
                    final AttributeSet a = el.getAttributes();
                    final AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
                    if (anchor != null) {
                        links.add((String)anchor.getAttribute(HTML.Attribute.HREF));
                    }
                }
                System.out.println("Links found: " + links);
            }
        });
        frm.add(btn, BorderLayout.EAST);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

}

Upvotes: 2

Related Questions