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!

Problem changing hyperlink's color when clicked on

801912Apr 16 2008 — edited Apr 16 2008
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();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2008
Added on Apr 16 2008
1 comment
168 views