• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TabComponentの名前を更新
#navi(../)
#tags()
#tags(JTabbedPane, JPopupMenu, JButton)
RIGHT:Posted by &author(aterai); at 2010-08-30
*TabComponentの名前を更新 [#v3420ee8]
TabComponentを使用するJTabbedPaneで、タブ名称を編集更新します。
``TabComponent``を使用する``JTabbedPane``で、タブ名称を編集更新します。

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

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTR4c_40eI/AAAAAAAAAh4/dLbGOWvSzSc/s800/RevalidateTabComponent.png)

**サンプルコード [#oacb1cc0]
#code(link){{
class TabTitleRenamePopupMenu extends JPopupMenu {
  private final JTextField textField = new JTextField(10);
  private final Action renameAction = new AbstractAction("rename") {
    @Override public void actionPerformed(ActionEvent e) {
      JTabbedPane t = (JTabbedPane)getInvoker();
      int idx = t.getSelectedIndex();
      String title = t.getTitleAt(idx);
      textField.setText(title);
      int result = JOptionPane.showConfirmDialog(
        t, textField, "Rename", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if(result==JOptionPane.OK_OPTION) {
        String str = textField.getText();
        if(!str.trim().isEmpty()) {
          t.setTitleAt(idx, str);
          JComponent c = (JComponent)t.getTabComponentAt(idx);
          c.revalidate();
        }
      }
    }
  };
  private final Action newTabAction = new AbstractAction("new tab") {
    @Override public void actionPerformed(ActionEvent evt) {
      JTabbedPane t = (JTabbedPane)getInvoker();
      int count = t.getTabCount();
      String title = "Tab " + count;
      t.add(title, new JLabel(title));
      t.setTabComponentAt(count, new ButtonTabComponent(t));
    }
  };
  private final Action closeAllAction = new AbstractAction("close all") {
    @Override public void actionPerformed(ActionEvent evt) {
      JTabbedPane t = (JTabbedPane)getInvoker();
      t.removeAll();
    }
  };
  public TabTitleRenamePopupMenu() {
    super();
    textField.addAncestorListener(new AncestorListener() {
      @Override public void ancestorAdded(AncestorEvent e) {
        textField.requestFocusInWindow();
      }
      @Override public void ancestorMoved(AncestorEvent e) {}
      @Override public void ancestorRemoved(AncestorEvent e) {}
    });
    add(renameAction);
    addSeparator();
    add(newTabAction);
    add(closeAllAction);
  }
  @Override public void show(Component c, int x, int y) {
    JTabbedPane t = (JTabbedPane)c;
    renameAction.setEnabled(t.indexAtLocation(x, y)>=0);
    super.show(c, x, y);
  }
};
}}

**解説 [#z53cbc17]
上記のサンプルでは、タブを閉じるJButtonをTabComponentに追加したJTabbedPaneに、タブ名称を変更するPopupMenuを設定しています。
上記のサンプルでは、タブを閉じる``JButton``を``TabComponent``に追加した``JTabbedPane``に、タブ名称を変更する``JPopupMenu``を設定しています。

[http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html How to Use Tabbed Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)]のButtonTabComponentを使っているので、JTabbedPane#setTitleAt(...)と名前を変更したあとで、( (JComponent)tabbedPane.getTabComponentAt(idx) ).revalidate()として、タブの内部レイアウトを検証し直しています。
[http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html How to Use Tabbed Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)]の``ButtonTabComponent``を使っているので、``JTabbedPane#setTitleAt(...)``と名前を変更したときに、``tabbedPane.getTabComponentAt(idx)``で取得した``JComponent``を``revalidate()``することで、文字列の長さに応じたサイズへの変更と、タブの内部レイアウトの更新を行なっています。

**参考リンク [#k0638175]
-[http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html How to Use Tabbed Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)]
-[[JTabbedPaneのタブにJTextFieldを配置してタイトルを編集>Swing/TabTitleEditor]]
-[[JTabbedPaneのタブタイトルを変更>Swing/EditTabTitle]]
-[[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]]

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