TITLE:JTabbedPaneのTabAreaをスクロール
#navi(../)
#tags(JTabbedPane, JViewport, JSlidter)
RIGHT:Posted by &author(aterai); at 2009-05-18
*JTabbedPaneのTabAreaをスクロール [#wacc0838]
``JTabbedPane``の``TabArea``を``JSlidter``を使ってスクロールします。

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

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTSn6mtDdI/AAAAAAAAAjE/ja_v92IXLsU/s800/ScrollTabToVisible.png)

**サンプルコード [#d4ebe884]
#code(link){{
private static void scrollTabAt(JTabbedPane tp, int index) {
  JViewport vp = null;
  for(Component c:tp.getComponents()) {
    if("TabbedPane.scrollableViewport".equals(c.getName())) {
      vp = (JViewport)c;
      break;
    }
  }
  if(vp==null) return;
  final JViewport viewport = vp;
  for(int i=0;i<tp.getTabCount();i++)
    tp.setForegroundAt(i, i==index?Color.RED:Color.BLACK);
  Dimension d = tp.getSize();
  Rectangle r = tp.getBoundsAt(index);
  int gw = (d.width-r.width)/2;
  r.grow(gw, 0);
  viewport.scrollRectToVisible(r);
}
}}

**解説 [#u1995ea4]
``JTabbedPane#setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT)``とした``JTabbedPane``の``TabArea``は、名前が``TabbedPane.scrollableViewport``な``JViewport``に配置されています。

上記のサンプルでは、この``JViewport``を取得して、``JViewport#scrollRectToVisible(Rectangle)``メソッドを使用し、矢印ボタンをクリックせずに``TabArea``のスクロールを行っています。

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