Swing/ExpandableTextField のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ExpandableTextField へ行く。
- 1 (2026-02-23 (月) 08:24:26)
- 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
JTextField内の空白区切りの一行テキストをJPopupMenuに配置したJTextAreaにコピーして複数行での編集を可能にします。
Screenshot

Advertisement
Source Code Examples
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);
}
}
View in GitHub: Java, KotlinDescription
JPanelにOverlayLayoutを設定し、JTextFieldと拡大用JButtonを重ねて配置- 拡大用
JButtonがクリックされたらJTextAreaを配置したJPopupMenuをJTextField上に表示PopupMenuListenerを実装してJPopupMenuに設定:PopupMenuListener#popupMenuWillBecomeVisible(...)をオーバーライドし、JTextField内の一行テキストの空白文字を改行文字に置換してJTextAreaにコピーPopupMenuListener#popupMenuWillBecomeInvisible(...)をオーバーライドし、JTextArea内の複数行テキストの改行文字に空白文字に置換してJTextFieldにコピー
JPopupMenuにフッターを追加してリサイズ可能に設定
Reference
- Program arguments, VM options, environment variables | IntelliJ IDEA Documentation
- JTextAreaにフォーカスが当たったときその高さを自動拡張する
- JPasswordFieldでパスワードを可視化する
- JComboBoxのドロップダウンリストの高さをマウスドラッグで変更する