在鼠标悬停时检索 JEditorPane 中超链接的标题属性

Dan*_*iel 1 java swing tooltip jeditorpane mouselistener

我想将工具提示添加到 (non-editable) 中的超链接JEditorPane。我在网上找到了一些提示,但没有一个对我有用。这是我目前的方法:

jEditorPaneIsFollower.addMouseMotionListener(new java.awt.event.MouseMotionListener() {
    @Override
    public void mouseMoved(java.awt.event.MouseEvent evt) {
        int pos = jEditorPaneIsFollower.viewToModel(evt.getPoint());
        if (pos >= 0) {
            HTMLDocument hdoc = (HTMLDocument)jEditorPaneIsFollower.getDocument();
            javax.swing.text.Element e = hdoc.getCharacterElement(pos);
            AttributeSet a = e.getAttributes();
            String href = (String) a.getAttribute(javax.swing.text.html.HTML.Attribute.TITLE);
            if (href != null) {
                jEditorPaneIsFollower.setToolTipText(href);
            } else {
                jEditorPaneIsFollower.setToolTipText(null);
            }
        }
        else {
            jEditorPaneIsFollower.setToolTipText(null);
        }
    }
    @Override
    public void mouseDragged(java.awt.event.MouseEvent e) {
        //
    }
});
Run Code Online (Sandbox Code Playgroud)

我的编辑器窗格的初始化:

jEditorPaneIsFollower.setEditable(false);
jEditorPaneIsFollower.setContentType("text/html");
jEditorPaneIsFollower.setDocument(new HTMLDocument());
jEditorPaneIsFollower.setEditorKit(new HTMLEditorKit());
Run Code Online (Sandbox Code Playgroud)

编辑器窗格的内容如下:

jEditorPaneIsFollower.addMouseMotionListener(new java.awt.event.MouseMotionListener() {
    @Override
    public void mouseMoved(java.awt.event.MouseEvent evt) {
        int pos = jEditorPaneIsFollower.viewToModel(evt.getPoint());
        if (pos >= 0) {
            HTMLDocument hdoc = (HTMLDocument)jEditorPaneIsFollower.getDocument();
            javax.swing.text.Element e = hdoc.getCharacterElement(pos);
            AttributeSet a = e.getAttributes();
            String href = (String) a.getAttribute(javax.swing.text.html.HTML.Attribute.TITLE);
            if (href != null) {
                jEditorPaneIsFollower.setToolTipText(href);
            } else {
                jEditorPaneIsFollower.setToolTipText(null);
            }
        }
        else {
            jEditorPaneIsFollower.setToolTipText(null);
        }
    }
    @Override
    public void mouseDragged(java.awt.event.MouseEvent e) {
        //
    }
});
Run Code Online (Sandbox Code Playgroud)

从调试中,我看到pos当我将鼠标移到编辑器窗格上时总是会发生变化,但是,字符元素e总是null.

所以我的问题是:

  1. 初始化编辑器窗格时是否需要将编辑器工具包和文档类型设置为 HTML?
  2. 将鼠标移到编辑器窗格上时,如何获取正确的元素并访问 title 属性以将其设置为工具提示?

Mad*_*mer 5

您似乎正在处理错误的元素,HTML.Attribute.TITLE既不是锚标记也不是锚标记的属性...

相反,您需要HTML.Tag.A从文档中提取属性Element,然后需要从中提取HTML.Attribute.HREF...

MouseListener您可以覆盖由getToolTipText调用的 components方法,而不是使用,ToolTipManager并允许您自定义返回的值,例如...

JEditorPane editorPane = new JEditorPane() {

    @Override
    public String getToolTipText(MouseEvent evt) {
        String text = null;
        int pos = viewToModel(evt.getPoint());
        if (pos >= 0) {
            HTMLDocument hdoc = (HTMLDocument) getDocument();
            javax.swing.text.Element e = hdoc.getCharacterElement(pos);
            AttributeSet a = e.getAttributes();

            SimpleAttributeSet value = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A);
            if (value != null) {
                String href = (String) value.getAttribute(HTML.Attribute.HREF);
                if (href != null) {
                    text = href;
                }
            }
        }
        return text;
    }

};
Run Code Online (Sandbox Code Playgroud)

使用这将需要手动注册的组件ToolTipManager,因为setToolTipText确实这通常...

ToolTipManager.sharedInstance().registerComponent(editorPane);
Run Code Online (Sandbox Code Playgroud)

注意:如果你想显示alt值,你应该使用HTML.Attribute.TITLE而不是HTML.Attribute.HREF