• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JToolTipにアイコンを表示
#navi(../)
*JToolTipにアイコンを表示 [#i911af83]
Posted by [[terai]] at 2006-02-13
---
category: swing
folder: ToolTipIcon
title: JToolTipにアイコンを表示
tags: [JToolTip, Icon, JLabel, Html, MatteBorder]
author: aterai
pubdate: 2006-02-13T14:40:55+09:00
description: JToolTipにJLabel、MatteBorder、またはHtmlタグを使用してアイコンを表示する方法をテストします。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVl25jXSI/AAAAAAAAAn4/-g0LJzeMmbc/s800/ToolTipIcon.png
---
* 概要 [#summary]
`JToolTip`に`JLabel`、`MatteBorder`、または`Html`タグを使用してアイコンを表示する方法をテストします。

#contents
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVl25jXSI/AAAAAAAAAn4/-g0LJzeMmbc/s800/ToolTipIcon.png)

**概要 [#qdcfc792]
JToolTipにアイコンを表示します。
* サンプルコード [#sourcecode]
#code(link){{
JLabel l1 = new JLabel("Icon") {
  @Override public JToolTip createToolTip() {
    JLabel iconlabel = new JLabel(icon);
    iconlabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    LookAndFeel.installColorsAndFont(
        iconlabel, "ToolTip.background", "ToolTip.foreground", "ToolTip.font");
    JToolTip tip = new JToolTip() {
      @Override public Dimension getPreferredSize() {
        return getLayout().preferredLayoutSize(this);
      }

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#fe5cdb35]
#code{{
JLabel l1 = new JLabel("JLabelを使ってツールチップにアイコン") {
  public JToolTip createToolTip() {
    JToolTip tip = super.createToolTip();
      @Override public void setTipText(String tipText) {
        String oldValue = iconlabel.getText();
        iconlabel.setText(tipText);
        firePropertyChange("tiptext", oldValue, tipText);
      }
    };
    tip.setComponent(this);
    tip.setLayout(new BorderLayout());
    JLabel iconlabel = new JLabel("テスト", icon, SwingConstants.LEFT);
    iconlabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    tip.add(iconlabel, BorderLayout.CENTER);
    Insets i = tip.getInsets();
    Dimension d = iconlabel.getPreferredSize();
    int w = d.width+i.left+i.right;
    int h = d.height+i.top+i.bottom;
    tip.setPreferredSize(new Dimension(w,h));
    tip.add(iconlabel);
    return tip;
  }
}; l1.setToolTipText("");
}}
#code{{
JLabel l2 = new JLabel("MatteBorderでツールチップにアイコン") {
  public JToolTip createToolTip() {
    JToolTip tip = super.createToolTip();
    Dimension d = tip.getPreferredSize();
    int strw = SwingUtilities.computeStringWidth(
                  tip.getFontMetrics(tip.getFont()), "テスト");
};
l1.setToolTipText("Test1");

JLabel l2 = new JLabel("MatteBorder") {
  @Override public JToolTip createToolTip() {
    JToolTip tip = new JToolTip() {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        Insets i = getInsets();
        d.height = Math.max(d.height, icon.getIconHeight() + i.top + i.bottom);
        return d;
      }
    };
    tip.setComponent(this);
    Border b1 = tip.getBorder();
    Border b2 = BorderFactory.createMatteBorder(
                  0, icon.getIconWidth(), 0, 0, icon);
    Border b3 = BorderFactory.createEmptyBorder(1,1,1,1);
    Border b2 = BorderFactory.createMatteBorder(0, icon.getIconWidth(), 0, 0, icon);
    Border b3 = BorderFactory.createEmptyBorder(1, 1, 1, 1);
    Border b4 = BorderFactory.createCompoundBorder(b3, b2);
    tip.setBorder(BorderFactory.createCompoundBorder(b1, b4));
    Insets i = tip.getInsets();
    int w = strw+i.left+i.right+3; // 3?
    int h = Math.max(d.height, icon.getIconHeight()+i.top+i.bottom);
    tip.setPreferredSize(new Dimension(w,h));
    return tip;
  }
}; l2.setToolTipText("テスト");
};
l2.setToolTipText("Test2");

JLabel l3 = new JLabel("html");
l3.setToolTipText("<html><img src='" + url + "'>test</img></html>");
}}
#code{{
JLabel l3 = new JLabel("htmlタグでツールチップにアイコン");
l3.setToolTipText("<html><img src='"+url+"'>テスト</img></html>");
}}

**解説 [#j592553f]
-上ラベル
--JToolTipにJLabelを追加しています。
-中ラベル
--MatteBorderを使ってアイコンを表示するように、createToolTipメソッドをオーバーライドしています。
-下ラベル
--htmlのimgタグをsetToolTipTextメソッドに使ってアイコンを表示しています。
* 解説 [#explanation]
- 上: `ToolTip icon using JLabel`
-- `JToolTip`にアイコンを設定した`JLabel`を追加
- 中: `ToolTip icon using MatteBorder`
-- アイコンを表示する`MatteBorder`を`JToolTip`に設定するよう`createToolTip`メソッドをオーバーライド
- 下: `ToolTip icon using HTML tags`
-- `<html>`の`img`タグを`setToolTipText`メソッドに使用してアイコンを表示

**参考リンク [#rbfe7317]
-[[XP Style Icons - Windows Application Icon, Software XP Icons>http://www.icongalore.com/]]
--アイコンを利用しています。
* 参考リンク [#reference]
- [https://xp-style-icons.en.softonic.com/ XP Style Icons - Download]
- [https://community.oracle.com/thread/2199222 Swing - Using text and a progress bar inside of a tooltip.]

**コメント [#w823ef7d]
- MatteBorderを使うと1.4と1.5で表示が微妙に異なるようです。 -- [[terai]] &new{2006-02-13 (月) 14:57:57};
- JLabelをJToolTipに貼る方法を追加しました。 -- [[terai]] &new{2006-07-05 (水) 18:40:54};
- 「MatteBorderでツールチップにアイコン」で、MatteBorderとEmptyBorderの内外が反対になっていたのを修正しました。 -- [[terai]] &new{2006-07-05 (水) 19:15:11};
* コメント [#comment]
#comment
- `MatteBorder`を使うと`Java 1.4`と`1.5`で表示が微妙に異なるようです。 -- &user(aterai); &new{2006-02-13 (月) 14:57:57};
- `JLabel`を`JToolTip`内に設定する方法を追加しました。 -- &user(aterai); &new{2006-07-05 (水) 18:40:54};
- 「`MatteBorder`でツールチップにアイコン」で、`MatteBorder`と`EmptyBorder`の内外が反対になっていたのを修正しました。 -- &user(aterai); &new{2006-07-05 (水) 19:15:11};

#comment