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();
}*/
}
}
}
}
}