---
category: swing
folder: TextFieldOnReadOnlyTextPane
title: JTextFieldを編集不可のJTextPaneに追加する
tags: [JTextPane, JTextField, JScrollPane, Focus]
author: aterai
pubdate: 2013-02-11T00:11:13+09:00
description: JTextFieldを空欄として編集不可にしたJTextPaneに追加します。
image: https://lh4.googleusercontent.com/-N1aQ1F9Zrn8/UReetdvfWQI/AAAAAAAABdc/9J_2lkAgW0Y/s800/TextFieldOnReadOnlyTextPane.png
---
* Summary [#summary]
`JTextField`を空欄として編集不可にした`JTextPane`に追加します。
#download(https://lh4.googleusercontent.com/-N1aQ1F9Zrn8/UReetdvfWQI/AAAAAAAABdc/9J_2lkAgW0Y/s800/TextFieldOnReadOnlyTextPane.png)
* Source Code Examples [#sourcecode]
#code(link){{
private static void insertQuestion(JTextPane textPane, String str) {
Document doc = textPane.getDocument();
try {
doc.insertString(doc.getLength(), str, null);
int pos = doc.getLength();
System.out.println(pos);
JTextField field = new JTextField(4) {
@Override public Dimension getMaximumSize() {
return getPreferredSize();
}
};
field.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
field.addFocusListener(new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
try {
Rectangle rect = textPane.modelToView(pos);
rect.grow(0, 4);
rect.setSize(field.getSize());
// System.out.println(rect);
// System.out.println(field.getLocation());
textPane.scrollRectToVisible(rect);
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(
ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
});
Dimension d = field.getPreferredSize();
int baseline = field.getBaseline(d.width, d.height);
field.setAlignmentY(baseline / (float) d.height);
// MutableAttributeSet a = new SimpleAttributeSet();
MutableAttributeSet a = textPane.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setLineSpacing(a, 1.5f);
textPane.setParagraphAttributes(a, true);
textPane.insertComponent(field);
doc.insertString(doc.getLength(), "\n", null);
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(
ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、編集不可状態の`JTextPane`内の文字列中に編集可能な`JTextField`を`JTextPane#insertComponent(...)`メソッドを使用して追加しています。
- `JTextPane`
-- 編集不可に設定
-- 行間を`1.5`倍に設定
--- [[JEditorPaneやJTextPaneに行間を設定する>Swing/LineSpacing]]
- `JTextField`
-- `JTextField#getMaximumSize()`をオーバーライドして幅を制限
-- `JTextField`に`MatteBorder`を設定して下線のみの空欄を表示
-- `JTextField#setAlignmentY(...)`でベースラインを揃える
--- [[JTextPaneに追加するコンポーネントのベースラインを揃える>Swing/InsertComponentBaseline]]
-- `JTextField`に`FocusListener`を追加してKBD{Tab}キーなどで`Focus`が移動したら、その`JTextField`までスクロールするように設定
--- [[FocusTraversalPolicyを使用してフォーカスを取得したコンポーネントまでスクロールする>Swing/AutoScrollOnFocus]]
* Reference [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTextPane.html#insertComponent-java.awt.Component- JTextPane#insertComponent(Component) (Java Platform SE 8)]
* Comment [#comment]
#comment
#comment