开发者

What is the appropriate event listener for a JTextPane in this scenario?

开发者 https://www.devze.com 2023-04-08 18:06 出处:网络
I have a JTextPane that displays HTML text. HTML text has hyperlinks with tags ... I want to invoke a j开发者_JS百科ava function when the user clicks on a link within the html text displayed on the

I have a JTextPane that displays HTML text. HTML text has hyperlinks with tags ...

I want to invoke a j开发者_JS百科ava function when the user clicks on a link within the html text displayed on the JTextPane.

How can I achieve this? If there is a need to implement an event listener? If so what is the appropriate event listener that is to be handled?


The listener type you are looking for is a HyperlinkListener, some code snippet:

    final JTextPane pane = new JTextPane();
    pane.setEditable(false);
    pane.setContentType("text/html");
    pane.setPage("http://swingx.java.net");
    ToolTipManager.sharedInstance().registerComponent(pane);

    HyperlinkListener l = new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
                try {
                    pane.setPage(e.getURL());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        }

    };
    pane.addHyperlinkListener(l);
0

精彩评论

暂无评论...
验证码 换一张
取 消