简体   繁体   中英

How do I modify an html link attribute when the html is loaded into a jEditPane? JDK6

I understand how to get the attribute with:

public void hyperlinkUpdate(HyperlinkEvent e) {
    e.getSourceElement().getAttributes().getAttribute(HTML.Attribute.COLOR);

How do I change that attribute?

This code changes the style of the element. Hope it will help...

private void editorHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {                                       
    if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
        changeStyle(evt.getSourceElement(), "a:hover");
    } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
        changeStyle(evt.getSourceElement(), "a");
    }
}                                      

private void changeStyle(Element el, String styleName) {
    HTMLDocument doc = (HTMLDocument)editor.getDocument();
    StyleContext ss = doc.getStyleSheet();
    Style style = ss.getStyle(styleName);
    int start = el.getStartOffset();
    int end = el.getEndOffset();
    doc.setCharacterAttributes(start, end - start, style, false);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM