Skip to content Skip to sidebar Skip to footer

How Can I Add The On Hover Effect On Hyperlink In Jeditorpane Using Swing In Java

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

Solution 1:

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.*;

publicclassHoverEffectTest {
  privatestaticfinalStringSO="http://stackoverflow.com/";
  privatefinalStrings="<a href='%s' color='%s'>%s</a><br>";
  privatefinalStrings1= String.format(s + "aaaaaaaaaaaaaa<br>", SO, "blue", SO);
  privatefinalStrings2= String.format(s + "cccc", SO, "#0000FF", "bbbbbbbbbbb");
  privatefinalJEditorPaneeditor=newJEditorPane(
    "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(newHyperlinkListener() {
      @OverridepublicvoidhyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
          setElementColor(e.getSourceElement(), "red");
        } elseif (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
          setElementColor(e.getSourceElement(), "blue");
        } elseif (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);
      }
    });
    returnnewJScrollPane(editor);
  }
  privatevoidsetElementColor(Element element, String color) {
    AttributeSetattrs= element.getAttributes();
    Objecto= attrs.getAttribute(HTML.Tag.A);
    if (o instanceof MutableAttributeSet) {
      MutableAttributeSeta= (MutableAttributeSet) o;
      a.addAttribute(HTML.Attribute.COLOR, color);
    }
  }
  publicstaticvoidmain(String... args) {
    EventQueue.invokeLater(newRunnable() {
      @Overridepublicvoidrun() {
        createAndShowGUI();
      }
    });
  }
  publicstaticvoidcreateAndShowGUI() {
    JFramef=newJFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(newHoverEffectTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

Solution 2:

try this:

HTMLEditorKitkit=newHTMLEditorKit();
StyleSheetstyleSheet= kit.getStyleSheet();
styleSheet.addRule("a:hover{color:red;}");
Documentdoc= kit.createDefaultDocument();
StringhtmlString="<a href='stackoverflow.com'>Go to StackOverflow!</a>";
// your JEditorPane
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);

Post a Comment for "How Can I Add The On Hover Effect On Hyperlink In Jeditorpane Using Swing In Java"