Swing/ElementAttribute のバックアップの現在との差分(No.4)
- 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:
概要
概要
JEditorPane
でdiv
やspan
タグのtitle
属性をToolTip
で表示できるように設定します。
Screenshot
Advertisement
サンプルコード
サンプルコード
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());
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JEditorPane
でHTMLEditorKit
を使った場合のToolTip
表示についてテストしています。img
タグのalt
属性は自動的にToolTip
表示され、リンクはHyperlinkListener
を追加することでToolTip
を変更することができます。
解説
上記のサンプルでは、JEditorPane
でHTMLEditorKit
を使った場合のToolTip
表示についてテストしています。img
タグのalt
属性は自動的にToolTip
表示され、リンクはHyperlinkListener
を追加することでToolTip
を変更できます。
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' /> " +
"<a href='http://terai.xrea.jp/'>Java Swing Tips</a></div>" +
"<a href='https://ateraimemo.com/'>Java Swing Tips</a></div>" +
"</body></html>";
- 上:
span
タグのtitle
属性をToolTip
で表示-
HTMLEditorKit
のLinkController
クラスを参考にJEditorPane#getToolTipText(MouseEvent)
をオーバーライド
-
- 下:
dvi
タグのtitle
属性をToolTip
で表示-
HTMLEditorKit
のLinkController
クラスを参考にJEditorPane#getToolTipText(MouseEvent)
メソッドをオーバーライド
-
- 下:
div
タグのtitle
属性をToolTip
で表示ImageView#getToolTipText(...)
を参考HyperlinkListener
を追加
class TooltipEditorKit extends HTMLEditorKit {
@Override public ViewFactory getViewFactory() {
return new HTMLFactory() {
@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);
}
};
}
}
参考リンク
参考リンク
- JEditorPaneにリンクを追加
- [JDK-8218674] HTML Tooltip with "img src=" on component doesn't show - Java Bug System