【问题标题】:Mark already used hyperlinks in HTMLEditorKit标记 HTMLEditorKit 中已使用的超链接
【发布时间】:2020-11-02 18:00:51
【问题描述】:

我正在使用 Swing HTMLEditorKit 生成一个 HTML 表格。一列显示可选择的超链接。

就像在搜索引擎中一样,我想标记已经调用的链接(粗体或着色)。

添加此行为的正确位置在哪里?

编辑:

吉尔伯特,谢谢你的提示

似乎不尊重链接状态。第一个 addRule 行不改变颜色,但保持默认的蓝色字体。第二个注释掉的行有效。

    ...
    HTMLEditorKit kit = new HTMLEditorKit();
    StyleSheet css = kit.getStyleSheet();
    if (css.getStyleSheets() == null) {
        StyleSheet css2 = new StyleSheet();
        css2.addRule("a:link {color: #DDDDDD } a:visited {color: #DDDDDD } a:hover {color: #DDDDDD } a:active {color: #DDDDDD } "); 
//          css2.addRule("a {color: #DDDDDD }"); 
        css2.addStyleSheet(css);
        kit.setStyleSheet(css2);
    }       

【问题讨论】:

  • 在 HTML 中,您可以为 a:visited 添加

标签: java html swing hyperlink htmleditorkit


【解决方案1】:

This one 给了我解决方案。

稍微修改一下对我有用

    jEditorPane.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                HyperlinkEvent.EventType type = e.getEventType();
                
                if (type == HyperlinkEvent.EventType.ACTIVATED) {
                    
                    // mark activated
                    if (e.getSourceElement() != null) {
                        AttributeSet a = e.getSourceElement().getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(e.getSourceElement());
                        }
                    }

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 2010-10-22
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多