Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Handle HyperLinks in a JEditorPane

801912Apr 14 2008 — edited Apr 15 2008
Hello,

I wrote the following code that puts some text inside a JEditorPane. A part of the text is normal text, and another part of it is hyperlinks. I've tried to use stylesheets, as you can see in the code, to show hyperlinks in a different way, meaning underlining them, changing the color, etc... But I haven't been successful. Do you have any ideas? Am I missing something here? or doing it the wrong way?
Please help out.
Thanks.

import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import javax.swing.text.html.StyleSheet;

public class MyEditorPaneTest extends JEditorPane {
	
	public MyEditorPaneTest()
	{
	    JFrame frame = new JFrame("MyEditorPaneTest");
	    JPanel panel = new JPanel();
	    panel.add(this);
	    HTMLEditorKit editorKit = new HTMLEditorKit();
	    
	    StyleSheet sheet = new StyleSheet();
	    sheet.addRule("a.link {text-decoration: underline; color: #FF0000}");
        sheet.addRule("a:visited {color: #00FF00}");
        sheet.addRule("a:hover {color: #FF00FF}");
        sheet.addRule("a:active {color: #0000FF}");
        sheet.addRule("td{font-family: Helvetica; font-size: 12pt;}");
        editorKit.setStyleSheet(sheet);
        setEditorKit(editorKit);
        setContentType("text/html");
	    setEditable(false);
	    setPreferredSize(new Dimension(400,200));
	    String text = "<!-- HTML MSG --><table xmlns:fo="+"http://www.w3.org/1999/XSL/Format"+"width=\"578\" border=\"0\">"+
        "<tr>"+
        "<td width=\"8\"> </td><td> This is a text. This is a text. This is a text. This is a text.<p></p>"+
        "<hr size=\"1\" noshade=\"true\">"+
        "<span class=\"f1\">This is a text.<a href=\"http://www.yahoo.com\" target=\"_new\">This is a link.</a>."+
        "</span></td>"+
        "</tr>"+
        "</table>";
	    setText(text);
	    HyperLinkListener listener = new HyperLinkListener();
	    addHyperlinkListener(listener);
	    frame.getContentPane().add(panel);
	    frame.setSize(new Dimension(400, 200));
	    frame.setVisible(true);
	}
	
	public static void main (String [] args)
	{
		MyEditorPaneTest test = new MyEditorPaneTest();
	}
	
	private class HyperLinkListener implements HyperlinkListener 
	{
        public void hyperlinkUpdate(HyperlinkEvent e) 
        {
	        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
	        {
		        JEditorPane pane = (JEditorPane) e.getSource();
		        if (e instanceof HTMLFrameHyperlinkEvent)
		        {
		            HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
		            HTMLDocument doc = (HTMLDocument)pane.getDocument();
		            doc.processHTMLFrameHyperlinkEvent(evt);
		        } 
		        else 
		        {
		            try 
		            {
			            pane.setPage(e.getURL());
		            } 
		            catch (Throwable t) 
		            {
			            t.printStackTrace();
		            }
		        }
	        }
	        else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED)
	        {
	        	JEditorPane pane = (JEditorPane) e.getSource();
		        if (e instanceof HTMLFrameHyperlinkEvent)
		        {
		        	System.err.println("inside if: "+e);
		            HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
		            HTMLDocument doc = (HTMLDocument)pane.getDocument();
		            doc.processHTMLFrameHyperlinkEvent(evt);
		        } 
		        else 
		        {
		            /*try 
		            {
			            pane.setPage(e.getURL());
		            } 
		            catch (Throwable t) 
		            {
			            t.printStackTrace();
		            }*/
		        }
	        }
	     }
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2008
Added on Apr 14 2008
1 comment
317 views