Swing/RevalidateTabComponent のバックアップの現在との差分(No.6)
- category: swing folder: RevalidateTabComponent title: TabComponentの名前を更新 tags: [JTabbedPane, JPopupMenu, JButton] tags: [JTabbedPane, JPopupMenu, JButton, JLabel] author: aterai pubdate: 2010-08-30T18:25:18+09:00 description: TabComponentを使用するJTabbedPaneで、タブ名称を編集更新します。 image:
概要
概要
TabComponent
を使用するJTabbedPane
で、タブ名称を編集更新します。
Screenshot
Advertisement
サンプルコード
サンプルコード
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();
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();
}
};
#spanadd
#spanend
public TabTitleRenamePopupMenu() {
super();
textField.addAncestorListener(new AncestorListener() {
@Override public void ancestorAdded(AncestorEvent e) {
textField.requestFocusInWindow();
}
#spanadd
#spanend
@Override public void ancestorMoved(AncestorEvent e) {}
#spanadd
#spanend
@Override public void ancestorRemoved(AncestorEvent e) {}
});
add(renameAction);
addSeparator();
add(newTabAction);
add(closeAllAction);
}
#spanadd
#spanend
@Override public void show(Component c, int x, int y) {
JTabbedPane t = (JTabbedPane)c;
renameAction.setEnabled(t.indexAtLocation(x, y)>=0);
JTabbedPane t = (JTabbedPane) c;
renameAction.setEnabled(t.indexAtLocation(x, y) >= 0);
super.show(c, x, y);
}
};
View in GitHub: Java, Kotlin解説
上記のサンプルでは、タブを閉じるJButton
をTabComponent
に追加したJTabbedPane
に、タブ名称を変更するJPopupMenu
を設定しています。
解説
- タブを閉じる
JButton
をTabComponent
に追加したJTabbedPane
にタブ名称を変更するJPopupMenu
を設定 - 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()
することで文字列の長さに応じたサイズへの変更とタブの内部レイアウトの更新を実行
-
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)
- JTabbedPaneのタブにJTextFieldを配置してタイトルを編集
- JTabbedPaneのタブタイトルを変更
- JTabbedPaneにタブを閉じるボタンを追加