#navi(../)
*ToolTipにアイコンを表示 [#i911af83]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-02-13~
更新日:&lastmod;

#contents

**概要 [#qdcfc792]
ToolTipにアイコンを表示します。

http://terai.xrea.jp/swing/tooltipicon/screenshot.png

**サンプルコード [#fe5cdb35]
 JLabel l1 = new JLabel("BorderFactoryでツールチップアイコン") {
   public JToolTip createToolTip() {
     JToolTip tip = new JToolTip();
     Border b1 = tip.getBorder();
     Border b2 = BorderFactory.createMatteBorder(1, 16, 1, 0, icon);
     tip.setBorder(BorderFactory.createCompoundBorder(b1, b2));
     tip.setComponent(this);
     String str = getToolTipText();
     Insets insets = tip.getInsets();
     FontMetrics fm = getFontMetrics(getFont());
     int w = SwingUtilities.computeStringWidth(fm, str);
     int h = fm.getHeight()+insets.top+insets.bottom;
     Dimension d = new Dimension(w+insets.left+insets.right, h);
     tip.setPreferredSize(d);
     return tip;
   }
 };
 l1.setToolTipText("テスト");
 JLabel l2 = new JLabel("htmlタグでツールチップアイコン");
 l2.setToolTipText("<html><img src='"+url+"'>テスト</img></html>");

-[[サンプルを起動>http://terai.xrea.jp/swing/tooltipicon/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/tooltipicon/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/tooltipicon/src.zip]]

**解説 [#j592553f]
-上ラベル
--MatteBorderを使ってアイコンを表示するように、createToolTipメソッドをオーバライドしています。
-下ラベル
--htmlのimgタグをsetToolTipTextメソッドに使ってアイコンを表示しています。

//**参考リンク
**コメント [#w823ef7d]
#comment