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);
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论