Hi, I have the following code that has a JEditorPane that contains text and hyperlinks. What I want to do now, is to make the hyperlink change its color at the moment it's clicked on. Here are the steps I have in mind:
1) Hyperlink is underlined and has blue color.
2) User clicks on hyperlink.
3) Hyperlink temporarily changes color to red, and turns back to blue again.
So far, I have tried to add the following rule to the StyleSheet, but it hasn't worked.
sheet.addRule("a:active {text-decoration: underline; color: red}");
I was wondering if you have other ideas besides this.
Your help will be greatly appreciated.
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.html.HTMLEditorKit;
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("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, but doesn't look like one.</a>."+
"</span></td>"+
"</tr>"+
"</table>";
setText(text);
frame.getContentPane().add(panel);
frame.setSize(new Dimension(400, 200));
frame.setVisible(true);
}
public static void main (String [] args)
{
MyEditorPaneTest test = new MyEditorPaneTest();
}
}