• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JEditorPaneのHTMLタグにToolTipTextを設定する
#navi(../)
RIGHT:Posted by [[aterai]] at 2011-08-08
*JEditorPaneのHTMLタグにToolTipTextを設定する [#r24f4ad3]
JEditorPaneでdivやspanタグのtitle属性をToolTipで表示できるように設定します。
---
category: swing
folder: ElementAttribute
title: JEditorPaneのHTMLタグにToolTipTextを設定する
tags: [JEditorPane, JTextPane, HTMLDocument, JToolTip, HyperlinkListener]
author: aterai
pubdate: 2011-08-08T15:04:19+09:00
description: JEditorPaneでdivやspanタグのtitle属性をToolTipで表示できるように設定します。
image: https://lh4.googleusercontent.com/-3HQ42PjgBfs/Tj97O_2VS6I/AAAAAAAABAc/EnrOPXrJxfE/s800/ElementAttribute.png
---
* 概要 [#summary]
`JEditorPane`で`div`や`span`タグの`title`属性を`ToolTip`で表示できるように設定します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/-3HQ42PjgBfs/Tj97O_2VS6I/AAAAAAAABAc/EnrOPXrJxfE/s800/ElementAttribute.png)

//#screenshot
#ref(https://lh4.googleusercontent.com/-3HQ42PjgBfs/Tj97O_2VS6I/AAAAAAAABAc/EnrOPXrJxfE/s800/ElementAttribute.png)

**サンプルコード [#c3b2443d]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
JTextPane editor1 = new JTextPane() {
  private transient Position.Bias[] bias = new Position.Bias[1];
  @Override public String getToolTipText(MouseEvent e) {
    String title = super.getToolTipText(e);
    JEditorPane editor = (JEditorPane) e.getSource();
    if(!editor.isEditable()) {
    if (!editor.isEditable()) {
      Point pt = new Point(e.getX(), e.getY());
      int pos = editor.getUI().viewToModel(editor, pt, bias);
      if(bias[0] == Position.Bias.Backward && pos > 0) {
      if (bias[0] == Position.Bias.Backward && pos > 0) {
        pos--;
      }
      if(pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)) {
        HTMLDocument hdoc = (HTMLDocument)editor.getDocument();
      if (pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)) {
        HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
        Element elem = hdoc.getCharacterElement(pos);
        if(elem != null) {
        if (elem != null) {
          AttributeSet a = elem.getAttributes();
          AttributeSet span = (AttributeSet)a.getAttribute(HTML.Tag.SPAN);
          if(span != null) {
            title = (String)span.getAttribute(HTML.Attribute.TITLE);
          AttributeSet span = (AttributeSet) a.getAttribute(HTML.Tag.SPAN);
          if (span != null) {
            title = (String) span.getAttribute(HTML.Attribute.TITLE);
          }
        }
      }
    }
    return title;
  }
};
editor1.setEditorKit(new HTMLEditorKit());
}}

**解説 [#g114332d]
上記のサンプルでは、JEditorPaneでHTMLEditorKitを使った場合のToolTip表示についてテストしています。imgタグのalt属性は自動的にToolTip表示され、リンクはHyperlinkListenerを追加することでToolTipを変更することができます。
* 解説 [#explanation]
上記のサンプルでは、`JEditorPane`で`HTMLEditorKit`を使った場合の`ToolTip`表示についてテストしています。`img`タグの`alt`属性は自動的に`ToolTip`表示され、リンクは`HyperlinkListener`を追加することで`ToolTip`を変更できます。

- 上: spanタグのtitle属性をToolTipで表示
-- HTMLEditorKitのLinkControllerクラスを参考にJEditorPane#getToolTipText(MouseEvent)をオーバーライド
- 下: dviタグのtitle属性をToolTipで表示
-- ImageView#getToolTipText(...)を参考
-- HyperlinkListenerを追加
#code{{
private final String htmlText =
  "<html><body>" +
  "<span style='background:#88ff88;' title='tooltip: span[@title]'>span</span><br />" +
  "<div title='tooltip: div[@title]'>div tag: div div div div</div>" +
  "<div style='padding: 2 24;'><img src='"+ image +"' alt='16x16 favicon' />&nbsp;" +
  "<a href='https://ateraimemo.com/'>Java Swing Tips</a></div>" +
  "</body></html>";
}}

- 上: `span`タグの`title`属性を`ToolTip`で表示
-- `HTMLEditorKit`の`LinkController`クラスを参考に`JEditorPane#getToolTipText(MouseEvent)`メソッドをオーバーライド
- 下: `div`タグの`title`属性を`ToolTip`で表示
-- `ImageView#getToolTipText(...)`を参考
-- `HyperlinkListener`を追加
#code{{
class TooltipEditorKit extends HTMLEditorKit {
  public ViewFactory getViewFactory() {
  @Override public ViewFactory getViewFactory() {
    return new HTMLFactory() {
      public View create(Element elem) {
      @Override public View create(Element elem) {
        AttributeSet attrs = elem.getAttributes();
        Object elementName = attrs.getAttribute(
            AbstractDocument.ElementNameAttribute);
        Object o = (elementName != null)
          ? null : attrs.getAttribute(StyleConstants.NameAttribute);
        if(o instanceof HTML.Tag) {
        if (o instanceof HTML.Tag) {
          HTML.Tag kind = (HTML.Tag) o;
          if(kind == HTML.Tag.DIV) {
          if (kind == HTML.Tag.DIV) {
            return new BlockView(elem, View.Y_AXIS) {
              @Override public String getToolTipText(
                  float x, float y, Shape allocation) {
                String s = super.getToolTipText(x, y, allocation);
                if(s==null) {
                  s = (String)getElement().getAttributes().getAttribute(
                if (s == null) {
                  s = (String) getElement().getAttributes().getAttribute(
                      HTML.Attribute.TITLE);
                }
                return s;
              }
            };
          }
        }
        return super.create(elem);
      }
    };
  }
}
}}

**参考リンク [#p5154cf7]
* 参考リンク [#reference]
- [[JEditorPaneにリンクを追加>Swing/HyperlinkListener]]
- [https://bugs.openjdk.org/browse/JDK-8218674 &#91;JDK-8218674&#93; HTML Tooltip with "img src=" on component doesn't show - Java Bug System]

**コメント [#h336329b]
* コメント [#comment]
#comment
#comment