---
category: swing
folder: ExpandableTextField
title: JTextField内のテキストをJPopupMenuに配置したJTextAreaで複数行編集する
title-en: Edit text in a JTextField on multiple lines in a JTextArea placed in a JPopupMenu
tags: [JTextField, JTextArea, JPopupMenu]
author: aterai
pubdate: 2026-02-23T08:16:23+09:00
description: JTextField内の空白区切りの一行テキストをJPopupMenuに配置したJTextAreaにコピーして複数行での編集を可能にします。
summary-jp: JTextField内の空白区切りの一行テキストをJPopupMenuに配置したJTextAreaにコピーして複数行での編集を可能にします。
summary-en: Copy single-line text separated by spaces from a JTextField into a JTextArea placed in a JPopupMenu to enable multi-line editing.
image: https://drive.google.com/uc?id=1e3McMWr2f0NNyvaXkGEjoHAJDjVFZYrB
---
* Summary [#summary]
JTextField内の空白区切りの一行テキストをJPopupMenuに配置したJTextAreaにコピーして複数行での編集を可能にします。
`JTextField`内の空白区切りの一行テキストを`JPopupMenu`に配置した`JTextArea`にコピーして複数行での編集を可能にします。
// #en{{Copy single-line text separated by spaces from a JTextField into a JTextArea placed in a JPopupMenu to enable multi-line editing.}}
#download(https://drive.google.com/uc?id=1e3McMWr2f0NNyvaXkGEjoHAJDjVFZYrB)
* Source Code Examples [#sourcecode]
#code(link){{
class ExpandableTextField extends OverlayLayoutPanel {
private final JTextField textField = new JTextField(20);
private final JTextArea textArea = new JTextArea();
private final JPopupMenu popup = new JPopupMenu() {
private transient PopupMenuListener listener;
@Override public void updateUI() {
removePopupMenuListener(listener);
super.updateUI();
JPanel footer = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
footer.add(makeResizeLabel(this));
setLayout(new BorderLayout());
add(new JScrollPane(textArea));
add(footer, BorderLayout.SOUTH);
listener = new ExpandPopupMenuListener();
addPopupMenuListener(listener);
}
};
protected ExpandableTextField() {
super();
ExpandArrowIcon icon = new ExpandArrowIcon();
JButton expandBtn = makeExpandButton(icon);
expandBtn.addActionListener(e -> popup.show(textField, 0, 0));
add(expandBtn);
textField.setMargin(new Insets(0, 0, 0, icon.getIconWidth()));
textField.setAlignmentX(RIGHT_ALIGNMENT);
textField.setAlignmentY(CENTER_ALIGNMENT);
add(textField);
}
@Override public final Component add(Component comp) {
return super.add(comp);
}
private final class ExpandPopupMenuListener implements PopupMenuListener {
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
textArea.setText(textField.getText().replace(" ", "\n") + "\n");
textArea.requestFocusInWindow();
}
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
textField.setText(textArea.getText().replace("\n", " ").trim());
}
@Override public void popupMenuCanceled(PopupMenuEvent e) {
// not need
}
}
private static JLabel makeResizeLabel(JPopupMenu popup) {
JLabel resizeLabel = new JLabel("?");
resizeLabel.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
MouseAdapter resizer = new Resizer(popup);
resizeLabel.addMouseListener(resizer);
resizeLabel.addMouseMotionListener(resizer);
return resizeLabel;
}
private static JButton makeExpandButton(ExpandArrowIcon icon) {
JButton expandBtn = new JButton(icon);
expandBtn.setMargin(new Insets(0, 0, 0, 0));
expandBtn.setContentAreaFilled(false);
expandBtn.setFocusable(false);
expandBtn.setBorderPainted(false);
expandBtn.setAlignmentX(RIGHT_ALIGNMENT);
expandBtn.setAlignmentY(CENTER_ALIGNMENT);
return expandBtn;
}
public void setText(String text) {
textField.setText(text);
}
}
}}
* Description [#description]
- `JPanel`に`OverlayLayout`を設定し、`JTextField`と拡大用`JButton`を重ねて配置
-- [[JPasswordFieldでパスワードを可視化する>Swing/ShowHidePasswordField]]
- 拡大用`JButton`がクリックされたら`JTextArea`を配置した`JPopupMenu`を`JTextField`上に表示
-- `PopupMenuListener`を実装して`JPopupMenu`に設定:
--- `PopupMenuListener#popupMenuWillBecomeVisible(...)`をオーバーライドし、`JTextField`内の一行テキストの空白文字を改行文字に置換して`JTextArea`にコピー
--- `PopupMenuListener#popupMenuWillBecomeInvisible(...)`をオーバーライドし、`JTextArea`内の複数行テキストの改行文字に空白文字に置換して`JTextField`にコピー
- `JPopupMenu`にフッターを追加してリサイズ可能に設定
-- [[JComboBoxのドロップダウンリストの高さをマウスドラッグで変更する>Swing/DropDownHeightResizing]]
* Reference [#reference]
- [https://www.jetbrains.com/help/idea/program-arguments-and-environment-variables.html#program_arguments Program arguments, VM options, environment variables | IntelliJ IDEA Documentation]
- [[JTextAreaにフォーカスが当たったときその高さを自動拡張する>Swing/ExpandingTextArea]]
- [[JPasswordFieldでパスワードを可視化する>Swing/ShowHidePasswordField]]
- [[JComboBoxのドロップダウンリストの高さをマウスドラッグで変更する>Swing/DropDownHeightResizing]]
* Comment [#comment]
#comment
#comment