开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜