Using the Hyperlink in jEditorPane link, i managed to get the hyperlink in the jEditorPane.
But now i want to add the hover effect to the hyperlink. I tried to apply the CSS using the
MyHTMLEditorKit kit = new MyHTMLEditorKit();
setEditorKitForContentType("text/html", kit);
addHyperlinkListener(createHyperlinkListener());
StyleSheet css= kit.getStyleSheet();
css.addRule("a { color: red;}");
where MyHTMlEditorKit
extends HTMLEditorKit
. Also i tried following CSS, but it didn't worked.
a:hover{color:red;}
Also, i tried onmouseover="this.style.color='red'"
attribute in the hyperlink, but even it didn't worked.
So how can i achieve the onhover
effect for hyperlink in jEditorPane?
Here's my version, but seems not a good solution. So I wanted to see if anyone had a cleaner approach.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class HoverEffectTest {
private static final String SO = "http://stackoverflow.com/";
private final String s = "<a href='%s' color='%s'>%s</a><br>";
private final String s1 = String.format(s + "aaaaaaaaaaaaaa<br>", SO, "blue", SO);
private final String s2 = String.format(s + "cccc", SO, "#0000FF", "bbbbbbbbbbb");
private final JEditorPane editor = new JEditorPane(
"text/html", "<html>" + s1 + s2);
private JComponent makeUI() {
editor.setEditable(false);
//@see: BasicEditorPaneUI#propertyChange(PropertyChangeEvent evt) {
// if ("foreground".equals(name)) {
editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editor.addHyperlinkListener(new HyperlinkListener() {
@Override public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
setElementColor(e.getSourceElement(), "red");
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
setElementColor(e.getSourceElement(), "blue");
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
Toolkit.getDefaultToolkit().beep();
}
//I don't understand why this is necessary...
//??? call BasicTextUI#modelChanged() ???
editor.setForeground(Color.WHITE);
editor.setForeground(Color.BLACK);
}
});
return new JScrollPane(editor);
}
private void setElementColor(Element element, String color) {
AttributeSet attrs = element.getAttributes();
Object o = attrs.getAttribute(HTML.Tag.A);
if (o instanceof MutableAttributeSet) {
MutableAttributeSet a = (MutableAttributeSet) o;
a.addAttribute(HTML.Attribute.COLOR, color);
}
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new HoverEffectTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
try this:
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a:hover{color:red;}");
Document doc = kit.createDefaultDocument();
String htmlString = "<a href='stackoverflow.com'>Go to StackOverflow!</a>";
// your JEditorPane
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.