• 追加された行はこの色です。
  • 削除された行はこの色です。
---
title: JTabbedPaneのタブを固定する
tags: [JTabbedPane, JPopupMenu, JLabel]
author: aterai
pubdate: 2011-12-05T14:18:41+09:00
description: JTabbedPaneにJPopupMenuを追加して、指定したタブのタイトルと位置を変更し、タブの固定を行います。
---
* 概要 [#j14c1072]
`JTabbedPane`に`JPopupMenu`を追加して、指定したタブのタイトルと位置を変更し、タブの固定を行います。

#download(https://lh4.googleusercontent.com/-QqKPFV0ZzIc/TttWYFUshII/AAAAAAAABFk/6HcCBI_bg-0/s800/PinTabbedPane.png)

* サンプルコード [#k9852cde]
#code(link){{
JCheckBoxMenuItem pinTabMenuItem = new JCheckBoxMenuItem(new AbstractAction("pin tab") {
  @Override public void actionPerformed(ActionEvent e) {
    JTabbedPane t = (JTabbedPane)getInvoker();
    JCheckBoxMenuItem check = (JCheckBoxMenuItem)e.getSource();
    JTabbedPane t = (JTabbedPane) getInvoker();
    JCheckBoxMenuItem check = (JCheckBoxMenuItem) e.getSource();
    int idx       = t.getSelectedIndex();
    Component cmp = t.getComponentAt(idx);
    Component tab = t.getTabComponentAt(idx);
    Icon icon     = t.getIconAt(idx);
    String tip    = t.getToolTipTextAt(idx);
    boolean flg   = t.isEnabledAt(idx);

    int i;
    if(check.isSelected()) {
      for(i=0;i<idx;i++) {
    if (check.isSelected()) {
      for (i = 0; i < idx; i++) {
        String s = t.getTitleAt(i);
        if(s==null || s.length()==0) continue;
        if (s == null || s.length() == 0) {
          continue;
        }
        break;
      }
    }else{
      for(i=t.getTabCount()-1;i>idx;i--) {
    } else {
      for (i = t.getTabCount() - 1; i > idx; i--) {
        String s = t.getTitleAt(i);
        if(s!=null && s.length()>0) continue;
        if (s != null && s.length() > 0) {
          continue;
        }
        break;
      }
    }
    t.remove(idx);
    t.insertTab(check.isSelected()?"":tip, icon, cmp, tip, i);
    t.insertTab(check.isSelected() ? "" : tip, icon, cmp, tip, i);
    t.setTabComponentAt(i, tab);
    t.setEnabledAt(i, flg);
    if(flg) t.setSelectedIndex(i);
    if (flg) {
      t.setSelectedIndex(i);
    }
  }
});
}}

* 解説 [#yfe02b48]
- タブを固定
-- タブタイトルを空にする
-- タブの位置を左側に移動
-- 固定したタブは削除しない
- タブの固定を解除
-- タブタイトルを`TooltipText`から復元する
-- タブの位置を固定されていないタブの右側に移動
- 注:
-- `TabPlacement`: `LEFT`, `RIGHT`は考慮していない

* 参考リンク [#ye2c31f9]
- [http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons]

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