Swing/TabTitleEditor のバックアップソース(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TabTitleEditor へ行く。
- 1 (2008-09-08 (月) 14:02:08)
- 2 (2009-09-05 (土) 16:35:09)
- 3 (2010-08-10 (火) 16:50:47)
- 4 (2013-01-17 (木) 15:10:29)
- 5 (2013-07-26 (金) 01:06:53)
- 6 (2013-08-20 (火) 14:26:25)
- 7 (2014-11-28 (金) 16:27:12)
- 8 (2016-02-16 (火) 19:34:42)
- 9 (2017-07-06 (木) 13:59:18)
- 10 (2018-02-24 (土) 19:51:30)
- 11 (2018-07-06 (金) 16:34:05)
- 12 (2020-06-30 (火) 11:09:11)
- 13 (2021-12-03 (金) 06:52:51)
--- category: swing folder: TabTitleEditor title: JTabbedPaneのタブにJTextFieldを配置してタイトルを編集 tags: [JTabbedPane, JTextField, MouseListener, ChangeListener, InputMap, ActionMap] author: aterai pubdate: 2008-09-08T14:02:08+09:00 description: JTabbedPaneで選択されたタブにJTextFieldを配置し、そのタイトルを編集します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTU43AZWdI/AAAAAAAAAmw/6klnGPa4D9o/s800/TabTitleEditor.png hreflang: href: https://java-swing-tips.blogspot.com/2008/09/double-click-on-each-tab-and-change-its.html lang: en --- * 概要 [#summary] `JTabbedPane`で選択されたタブに`JTextField`を配置し、そのタイトルを編集します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTU43AZWdI/AAAAAAAAAmw/6klnGPa4D9o/s800/TabTitleEditor.png) * サンプルコード [#sourcecode] #code(link){{ private Component tabComponent = null; private int editing_idx = -1; private int len = -1; private Dimension dim; private void startEditing() { //System.out.println("start"); editing_idx = tabbedPane.getSelectedIndex(); tabComponent = tabbedPane.getTabComponentAt(editing_idx); tabbedPane.setTabComponentAt(editing_idx, editor); editor.setVisible(true); editor.setText(tabbedPane.getTitleAt(editing_idx)); editor.selectAll(); editor.requestFocusInWindow(); len = editor.getText().length(); dim = editor.getPreferredSize(); editor.setMinimumSize(dim); } private void cancelEditing() { //System.out.println("cancel"); if (editing_idx >= 0) { tabbedPane.setTabComponentAt(editing_idx, tabComponent); editor.setVisible(false); editing_idx = -1; len = -1; tabComponent = null; editor.setPreferredSize(null); } } private void renameTabTitle() { //System.out.println("rename"); String title = editor.getText().trim(); if (editing_idx >= 0 && !title.isEmpty()) { tabbedPane.setTitleAt(editing_idx, title); } cancelEditing(); } }} * 解説 [#explanation] `JTabbedPane`のタブタイトルを直接編集します。 `JDK 6`で導入された、`JTabbedPane#setTabComponentAt`メソッドを使用してタブに`JTextField`を追加しています。 操作方法などは、以下のように、`GlassPane`を使用している[[JTabbedPaneのタブタイトルを変更>Swing/EditTabTitle]]と同じですが、こちらは文字が入力されるたびに(`JTabbedPane#revalidate`しているので)タブの幅が広がります。 - 操作方法 -- マウスでタブをダブルクリック、またはタブを選択してKBD{Enter}キーで編集開始 -- 編集中に入力欄以外をクリック、またはKBD{Enter}キーでタイトル文字列が確定 -- 編集中KBD{Tab}キーを押しても無視 -- KBD{Esc}キーで編集をキャンセル -- 文字列が空の状態で確定された場合、編集をキャンセル * 参考リンク [#reference] - [[JTabbedPaneのタブタイトルを変更>Swing/EditTabTitle]] -- こちらは、`GlassPane`に`JTextField`を配置してタブタイトルを編集しているため`JDK 6`以前でも動作可 * コメント [#comment] #comment - `setTabComponentAt(...)`メソッドで閉じるボタンなどと併用していた場合、編集後にそのボタンなどが消える不具合をメールで指摘してもらったので、修正。 -- &user(aterai); &new{2010-08-10 (火) 16:47:33}; #comment