• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*ToolTipにアイコンを表示 [#i911af83]
*JToolTipにアイコンを表示 [#i911af83]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-02-13~
更新日:&lastmod;

#contents

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

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;
       JToolTip tip = super.createToolTip();
       Border b1 = tip.getBorder();
       Border b2 = BorderFactory.createMatteBorder(0, 16, 0, 0, icon);
       Border b3 = BorderFactory.createEmptyBorder(1, 1, 1, 1);
       Border b4 = BorderFactory.createCompoundBorder(b2, b3);
       tip.setBorder(BorderFactory.createCompoundBorder(b1, b4));
       Insets i = tip.getInsets();
       int w = tip.getPreferredSize().width+i.left+i.right+16;
       int h = i.top+i.bottom+16;
       tip.setPreferredSize(new Dimension(w,h));
       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]
- MatteBorderを使うと1.4と1.5で表示が微妙に異なるようです。 -- [[terai]] &new{2006-02-13 (月) 14:57:57};

#comment