• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneのサムネイルをJToolTipsで表示
#navi(../)
*JTabbedPaneのサムネイルをJToolTipsで表示 [#nb660cdb]
Posted by [[terai]] at 2006-07-31
---
category: swing
folder: TabThumbnail
title: JTabbedPaneのサムネイルをJToolTipで表示
tags: [JToolTip, JTabbedPane]
author: aterai
pubdate: 2006-07-31T12:28:44+09:00
description: ツールチップを使って、JTabbedPaneのサムネイルを表示します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTUz8_Yw-I/AAAAAAAAAmo/wLoOmG5I3oc/s800/TabThumbnail.png
---
* 概要 [#summary]
ツールチップを使って、`JTabbedPane`のサムネイルを表示します。

#contents
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTUz8_Yw-I/AAAAAAAAAmo/wLoOmG5I3oc/s800/TabThumbnail.png)

**概要 [#g4897f6f]
ツールチップを使って、JTabbedPaneのサムネイルを表示します。
* サンプルコード [#sourcecode]
#code(link){{
class TabThumbnailTabbedPane extends JTabbedPane {
  private int current = -1;
  private static final double SCALE = .15;
  private Component getTabThumbnail(int index) {
    Component c = getComponentAt(index);
    Icon icon = null;
    if (c instanceof JScrollPane) {
      c = ((JScrollPane) c).getViewport().getView();
      Dimension d = c.getPreferredSize();
      int newW = (int) (d.width  * SCALE);
      int newH = (int) (d.height * SCALE);
      BufferedImage image = new BufferedImage(
          newW, newH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = image.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                          RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2.scale(SCALE, SCALE);
      c.paint(g2);
      g2.dispose();
      icon = new ImageIcon(image);
    } else if (c instanceof JLabel) {
      icon = ((JLabel) c).getIcon();
    }
    return new JLabel(icon);
  }

-&jnlp;
-&jar;
-&zip;
  @Override public JToolTip createToolTip() {
    int index = current;
    if (index < 0) {
      return null;
    }

#screenshot
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder());
    p.add(new JLabel(getTitleAt(index)), BorderLayout.NORTH);
    p.add(getTabThumbnail(index));

**サンプルコード [#o54faaa7]
#code{{
class MyTabbedPane extends JTabbedPane {
  private final JToolTip tip;
  private int current = -1;
  public MyTabbedPane() {
    super();
    tip = super.createToolTip();
  }
  public JToolTip createToolTip() {
    initToolTip(tip, current);
    JToolTip tip = new JToolTip() {
      @Override public Dimension getPreferredSize() {
        Insets i = getInsets();
        Dimension d = p.getPreferredSize();
        return new Dimension(
            d.width + i.left + i.right, d.height + i.top + i.bottom);
      }
    };
    tip.setComponent(this);
    LookAndFeel.installColorsAndFont(
        p, "ToolTip.background", "ToolTip.foreground", "ToolTip.font");
    tip.setLayout(new BorderLayout());
    tip.add(p);
    return tip;
  }
  public String getToolTipText(MouseEvent e) {

  @Override public String getToolTipText(MouseEvent e) {
    int index = indexAtLocation(e.getX(), e.getY());
    String str = (current!=index)?null:super.getToolTipText(e);
    String str = (current == index) ? super.getToolTipText(e) : null;
    current = index;
    return str;
  }
  private final double scale = 0.2d;
  private void initToolTip(JToolTip tip, int index) {
    tip.removeAll();
    JPanel panel = new JPanel(new BorderLayout());
    if(index<0) {
      return;
    }
    String str = getTitleAt(index);
    panel.add(new JLabel(str), BorderLayout.NORTH);
    Component c = getComponentAt(index);
    if(c instanceof JScrollPane) {
      c = ((JScrollPane)c).getViewport().getView();
    }
    final BufferedImage image = new BufferedImage(
       c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    c.paint(g);
    JPanel pnl = new JPanel() {
      public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.scale(scale,scale);
        g2.drawImage(image, 0, 0, this);
      }
    };
    pnl.setOpaque(true);
    panel.add(pnl, BorderLayout.CENTER);
    tip.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    tip.setLayout(new BorderLayout());
    tip.add(panel, BorderLayout.CENTER);
    Dimension d = new Dimension(
      (int)(c.getWidth()*scale), (int)(c.getHeight()*scale));
    tip.setPreferredSize(d);
  }
}
}}

**解説 [#neef6f6d]
マウスカーソルがタブタイトル上にきた場合、そのタブ内部のコンポーネントを縮小してJToolTipに貼り付けています。
* 解説 [#explanation]
- 選択されたタブではなく、マウスカーソルが存在するタブコンテンツの内部コンポーネントを縮小して`BufferedImage`を作成
- 縮小した`BufferedImage`から`ImageIcon`を作成、`JLabel#setIcon(...)`でコンポーネントに変換して`JToolTip`に配置

**参考リンク [#zffef92d]
-[[デジタル出力工房 絵写楽>http://www.bekkoame.ne.jp/~bootan/free2.html]]
-[[2000ピクセル以上のフリー写真素材集>http://sozai-free.com/]]
-[[XP Style Icons - Windows Application Icon, Software XP Icons>http://www.icongalore.com/]]
* 参考リンク [#reference]
- [http://www.bekkoame.ne.jp/~bootan/free2.html デジタル出力工房 絵写楽]
- [http://sozai-free.com/ 2000ピクセル以上のフリー写真素材集]
- [https://xp-style-icons.en.softonic.com/ XP Style Icons - Download]

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