如何在JEditorPane中更改HTMLDocument的特定元素的颜色? [英] How can I change the color of a particular element of a HTMLDocument in a JEditorPane?

查看:65
本文介绍了如何在JEditorPane中更改HTMLDocument的特定元素的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想实现将鼠标悬停在链接上时更改其颜色的功能.当我将鼠标悬停在链接上时触发的HyperlinkEvent将HTML元素交给我,但是它不允许我在其上设置任何样式属性,而且我不知道如何获取具有可设置属性的元素.

I basically want to implement changing the color of the links when I hover over them. The HyperlinkEvent that is triggered when I mouse over the link hands me the HTML element, but it won't let me set any style attributes on it, and I can't figure out how to get the elements that do have settable attributes.

推荐答案

我想出了我想要使用样式化文档以及HTMLEditorKit提供的一些帮助来完成的工作:

I figured out what I wanted to do using styled documents and bit of help from the HTMLEditorKit:

public class HighlightHyperlinkExample {
    private static Element lastHyperlinkElementEntered;
    private static JEditorPane textPane;


    public static void main(String[] args) {
        textPane = new JEditorPane();
        textPane.setContentType(new HTMLEditorKit().getContentType());
        JScrollPane scrollPane = new JScrollPane(textPane);
        textPane.setText(
                "Sample text with <a href=\"x\">a link</a> and another <a href=\"x\">link</a>.");

        initListeners();

        JFrame frame = new JFrame();
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }


    private static void initListeners() {
        textPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                removeHyperlinkHighlight();
            }
        });
        textPane.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                Point pt = new Point(e.getX(), e.getY());
                int pos = textPane.viewToModel(pt);
                if (pos >= 0) {
                    HTMLDocument hdoc = (HTMLDocument) textPane.getDocument();
                    Element elem = hdoc.getCharacterElement(pos);
                    if (elem != null) {
                        AttributeSet a = elem.getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(elem);
                        } else {
                            removeHyperlinkHighlight();
                        }
                    }
                }
            }
        });
    }

    private static void removeHyperlinkHighlight() {
        changeColor(lastHyperlinkElementEntered, Color.BLUE);
        lastHyperlinkElementEntered = null;
    }

    private static void highlightHyperlink(Element hyperlinkElement) {
        if (hyperlinkElement != lastHyperlinkElementEntered) {
            lastHyperlinkElementEntered = hyperlinkElement;
            changeColor(hyperlinkElement, Color.RED);
        }
    }

    private static void changeColor(Element el, Color color) {
        if (lastHyperlinkElementEntered != null) {
            HTMLDocument doc = (HTMLDocument) textPane.getDocument();
            int start = el.getStartOffset();
            int end = el.getEndOffset();
            StyleContext ss = doc.getStyleSheet();
            Style style = ss.addStyle("HighlightedHyperlink", null);
            style.addAttribute(StyleConstants.Foreground, color);
            doc.setCharacterAttributes(start, end - start, style, false);
        }
    }
}

这篇关于如何在JEditorPane中更改HTMLDocument的特定元素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆