Using the Hyperlink in jEditorPane link, i managed to get the hyperlink in the jEditorPane.
在jEditorPane链接中使用超链接,我设法获得jEditorPane中的超链接。
But now i want to add the hover effect to the hyperlink. I tried to apply the CSS using the
但现在我想将悬停效果添加到超链接。我尝试使用CSS来应用CSS
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.
其中MyHTMlEditorKit扩展了HTMLEditorKit。我也试过跟随CSS,但它没有奏效。
a:hover{color:red;}
Also, i tried onmouseover="this.style.color='red'"
attribute in the hyperlink, but even it didn't worked.
另外,我在超链接中尝试了onmouseover =“this.style.color ='red'”属性,但即使它没有奏效。
So how can i achieve the onhover
effect for hyperlink in jEditorPane?
那么如何在jEditorPane中实现超链接的onhover效果呢?
2 个解决方案
#1
1
try this:
尝试这个:
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a:hover{color:red;}");
Document doc = kit.createDefaultDocument();
String htmlString = "<a href='*.com'>Go to *!</a>";
// your JEditorPane
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);
#2
2
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://*.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);
}
}
#1
1
try this:
尝试这个:
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a:hover{color:red;}");
Document doc = kit.createDefaultDocument();
String htmlString = "<a href='*.com'>Go to *!</a>";
// your JEditorPane
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);
#2
2
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://*.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);
}
}