Swing/RevalidateTabComponent のバックアップの現在との差分(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/RevalidateTabComponent へ行く。
- 1 (2010-08-30 (月) 18:25:18)
- 2 (2011-04-22 (金) 17:36:54)
- 3 (2011-11-30 (水) 17:53:55)
- 4 (2012-12-28 (金) 12:29:11)
- 5 (2013-07-24 (水) 21:56:06)
- 6 (2015-02-05 (木) 14:34:11)
- 7 (2016-09-09 (金) 15:04:44)
- 8 (2017-04-04 (火) 14:17:08)
- 9 (2017-10-24 (火) 17:20:37)
- 10 (2019-05-15 (水) 21:10:09)
- 11 (2021-02-10 (水) 11:01:24)
- 12 (2024-04-19 (金) 12:58:27)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:TabComponentの名前を更新 #navi(../) RIGHT:Posted by [[terai]] at 2010-08-30 *TabComponentの名前を更新 [#v3420ee8] TabComponentを使用するJTabbedPaneで、タブ名称を編集更新します。 --- category: swing folder: RevalidateTabComponent title: TabComponentの名前を更新 tags: [JTabbedPane, JPopupMenu, JButton, JLabel] author: aterai pubdate: 2010-08-30T18:25:18+09:00 description: TabComponentを使用するJTabbedPaneで、タブ名称を編集更新します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTR4c_40eI/AAAAAAAAAh4/dLbGOWvSzSc/s800/RevalidateTabComponent.png --- * 概要 [#summary] `TabComponent`を使用する`JTabbedPane`で、タブ名称を編集更新します。 -&jnlp; -&jar; -&zip; #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTR4c_40eI/AAAAAAAAAh4/dLbGOWvSzSc/s800/RevalidateTabComponent.png) #screenshot **サンプルコード [#oacb1cc0] #code{{ * サンプルコード [#sourcecode] #code(link){{ class TabTitleRenamePopupMenu extends JPopupMenu { private final JTextField textField = new JTextField(10); private final Action renameAction = new AbstractAction("rename") { public void actionPerformed(ActionEvent e) { JTabbedPane t = (JTabbedPane)getInvoker(); @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) { 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()) { if (!str.trim().isEmpty()) { t.setTitleAt(idx, str); JComponent c = (JComponent)t.getTabComponentAt(idx); 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(); 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(); JTabbedPane t = (JTabbedPane) getInvoker(); t.removeAll(); } }; public TabTitleRenamePopupMenu() { super(); textField.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent e) { @Override public void ancestorAdded(AncestorEvent e) { textField.requestFocusInWindow(); } public void ancestorMoved(AncestorEvent e) {} public void ancestorRemoved(AncestorEvent e) {} @Override public void ancestorMoved(AncestorEvent e) {} @Override public void ancestorRemoved(AncestorEvent e) {} }); add(renameAction); addSeparator(); add(newTabAction); add(closeAllAction); } public void show(Component c, int x, int y) { JTabbedPane t = (JTabbedPane)c; renameAction.setEnabled(t.indexAtLocation(x, y)>=0); @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を設定しています。 * 解説 [#explanation] - タブを閉じる`JButton`を`TabComponent`に追加した`JTabbedPane`にタブ名称を変更する`JPopupMenu`を設定 - [https://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()`することで文字列の長さに応じたサイズへの変更とタブの内部レイアウトの更新を実行 [[How to Use Tabbed Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)>http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html]]のButtonTabComponentを使っているので、JTabbedPane#setTitleAt(...)と名前を変更したあとで、((JComponent)tabbedPane.getTabComponentAt(idx)).revalidate()として、タブの内部レイアウトを検証し直しています。 * 参考リンク [#reference] - [https://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]] **参考リンク [#k0638175] -[[How to Use Tabbed Panes (The Java Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)>http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html]] -[[JTabbedPaneのタブにJTextFieldを配置してタイトルを編集>Swing/TabTitleEditor]] -[[JTabbedPaneのタブタイトルを変更>Swing/EditTabTitle]] -[[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]] **コメント [#q268aea3] * コメント [#comment] #comment #comment