• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneのCloseButtonをフォーカスがある場合だけ表示
#navi(../)
*JTabbedPaneのCloseButtonをフォーカスがある場合だけ表示 [#s04e3478]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2008-01-21~
更新日:&lastmod;
---
category: swing
folder: HoverCloseButton
title: JTabbedPaneのCloseButtonをフォーカスがある場合だけ表示
tags: [JTabbedPane, JButton, Focus]
author: aterai
pubdate: 2008-01-21T15:27:41+09:00
description: JTabbedPaneのタブを閉じるボタンを、タブにフォーカスがある場合だけ表示します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTN-acwv2I/AAAAAAAAAbo/gFaIpQr1XGc/s800/HoverCloseButton.png
---
* 概要 [#summary]
`JTabbedPane`のタブを閉じるボタンを、タブにフォーカスがある場合だけ表示します。

#contents
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTN-acwv2I/AAAAAAAAAbo/gFaIpQr1XGc/s800/HoverCloseButton.png)

**概要 [#e5818d53]
JTabbedPaneのタブを閉じるボタンを、タブにフォーカスがある場合だけ表示します。
* サンプルコード [#sourcecode]
#code(link){{
class HoverCloseButtonTabbedPane extends JTabbedPane {
  private transient MouseMotionListener hoverHandler;
  public HoverCloseButtonTabbedPane() {
    super(TOP, SCROLL_TAB_LAYOUT);
  }

#screenshot
  public HoverCloseButtonTabbedPane(int tabPlacement) {
    super(tabPlacement, SCROLL_TAB_LAYOUT);
  }

**サンプルコード [#h7af8be1]
#code{{
public MyJTabbedPane() {
  super();
  setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  addMouseMotionListener(new MouseMotionAdapter() {
    private int prev = -1;
    @Override public void mouseMoved(MouseEvent e) {
      JTabbedPane source = (JTabbedPane)e.getSource();
      int focussed = source.indexAtLocation(e.getX(), e.getY());
      int selected = source.getSelectedIndex();
      if(focussed==prev) return;
      for(int i=0;i<source.getTabCount();i++) {
        TabPanel tab = (TabPanel)source.getTabComponentAt(i);
        tab.setButtonVisible(i==selected || i==focussed);
      }
      prev = focussed;
  @Override public void updateUI() {
    removeMouseMotionListener(hoverHandler);
    super.updateUI();
    if (hoverHandler == null) {
      hoverHandler = new MouseMotionAdapter() {
        private int prev = -1;
        @Override public void mouseMoved(MouseEvent e) {
          JTabbedPane source = (JTabbedPane) e.getComponent();
          int focussed = source.indexAtLocation(e.getX(), e.getY());
          if (focussed == prev) {
            return;
          }
          for (int i = 0; i < source.getTabCount(); i++) {
            TabPanel tab = (TabPanel) source.getTabComponentAt(i);
            tab.setButtonVisible(i == focussed);
          }
          prev = focussed;
        }
      };
    }
  });
    addMouseMotionListener(hoverHandler);
  }

  @Override public void addTab(String title, final Component content) {
    super.addTab(title, content);
    setTabComponentAt(getTabCount() - 1, new TabPanel(this, title, content));
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#bb2c52ca]
上記のサンプルでは、JDK 6 で導入されたタブにコンポーネントを追加する機能を使って、タブ上にマウスカーソルがある場合や、フォーカスがある場合にだけJButtonを表示しています。
* 解説 [#explanation]
上記のサンプルでは、`JDK 6`で導入されたタブにコンポーネントを追加する機能を使って、タブ上にマウスカーソルがある場合だけそのタブを閉じるための`JButton`を表示しています。

**参考リンク [#c695580a]
-[[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]]
-[[JTabbedPaneのタブ文字列をハイライト>Swing/TabTitleHighlight]]
- `JButton`が表示されてもそのタブ幅は常に一定
- `JButton`が表示されて内部のタブタイトル表示幅が短くなった場合は超過したタブ文字列を`...`で省略

**コメント [#e680e90e]
* 参考リンク [#reference]
- [[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]]
- [[JTabbedPaneのタブ文字列をハイライト>Swing/TabTitleHighlight]]

* コメント [#comment]
#comment
- タブが選択されている場合も`JButton`を表示していたが、これを変更してタブ上にマウスカーソルがある場合だけ`JButton`を表示するように変更。 -- &user(aterai); &new{2008-03-19 (水) 21:09:18};

#comment