JEditorPane

     

We create a JEditorPane in the blogger application in the Toolkit section. I refer you there to see in detail how that is used. But there is one question that is often asked when figuring out how to use JEditorPanes. And that's how to handle hyperlink clicks.

You do need to do something in fact. If you use a JEditorPane to display a simple HTML page, which is one of its main purposes, nothing will happen when you click on the links. Unless you create an object of a class that implements the HyperlinkListener interface, as shown in the following example:

 

 try {      String url = "http://www.javagarage.net";      JEditorPane editorPane = new JEditorPane(url);           //don't let user try to edit content!      editorPane.setEditable(false);      editorPane.addHyperlinkListener(new LinkListener()); } catch (IOException e) {         //handle... } class LinkListener implements HyperlinkListener {     public void hyperlinkUpdate(HyperlinkEvent event) {         if (event.getEventType() ==                   HyperlinkEvent.EventType.ACTIVATED) {             JEditorPane pane =                     (JEditorPane)event.getSource();             try {                 // Show the new page in the editor pane.                 pane.setPage(event.getURL());             } catch (IOException e) {                 //handle..             }         }     } } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net