【问题标题】:How to reach/trigger the target of a hyperlink inside a JEditorPane/JTextPane programmatically如何以编程方式到达/触发 JEditorPane/JTextPane 内的超链接目标
【发布时间】:2016-06-18 01:46:55
【问题描述】:

我有一个封闭源代码的第三方应用程序,我试图从我的 java 程序中控制它的窗口。我设法运行第三方应用程序的 main 方法并使用 AWTEventListener 实例拦截它生成的窗口事件。然后我遍历它生成的窗口组件,以便找到和操作必要的摆动控件。查找组件、单击按钮、激活切换按钮和修改文本字段工作正常,但 JTextPane 中有一个超链接,我无法以编程方式触发,我也没有在网上找到任何关于如何成功完成的信息。 herehere 的建议看起来很有希望,但我无法使用 MouseEvent 触发超链接。我还应该指出,超链接不是指向 URL,而是指向内部函数。当我调用 JTextPane getText() 方法时,我得到:

<html>
  <head>

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

我的问题:有没有一种方法可以直接到达 JTextPane 内的超链接目标(在这种情况下是展开窗口),而不是尝试让 MouseEvent 模拟点击它?

【问题讨论】:

    标签: java html swing jtextpane jeditorpane


    【解决方案1】:

    我不知道,但这个例子可能会对你有所帮助。

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

    【讨论】:

    • 感谢@Sergiy 的回复。您的示例类似于推荐的 here。我能够遍历元素并找到我需要的链接。我的问题是链接没有指向 URL。相反,它指向另一个程序的方法,我无权访问其代码(#action)。这就是为什么我想知道是否可以使用您的示例找到的链接以编程方式从我的 java 程序中触发它。
    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 2011-12-21
    • 2012-05-02
    • 2013-12-02
    • 2017-07-15
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多