• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneのサムネイルをJToolTipsで表示
#navi(../)
*JTabbedPaneのサムネイルをJToolTipsで表示 [#nb660cdb]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-07-31~
更新日:&lastmod;

#contents

**概要 [#g4897f6f]
ツールチップを使って、JTabbedPaneのサムネイルを表示します。

#screenshot

**サンプルコード [#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);
     return tip;
   }
   public String getToolTipText(MouseEvent e) {
     int index = indexAtLocation(e.getX(), e.getY());
     String str = (current!=index)?null:super.getToolTipText(e);
     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);
   }
 }

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

**解説 [#neef6f6d]
マウスカーソルをタブタイトルの上に持っていくと、タブ内部のコンポーネントを縮小してJToolTipに貼り付けています。
マウスカーソルがタブタイトル上にきた場合、そのタブ内部のコンポーネントを縮小して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/]]

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