Swing/InputContextCaretColor のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/InputContextCaretColor へ行く。
- 1 (2025-08-25 (月) 04:56:44)
- category: swing folder: InputContextCaretColor title: InputContextが使用可能な状態でCaretの色を変更 tags: [Caret, InputContext, JTextComponent, DefaultCaret] author: aterai pubdate: 2025-08-25T04:55:51+09:00 description: JTextComponentなどで使用中のインプット・メソッドが変換のために使用可能な場合はCaretの色を変更するよう設定します。 image: https://drive.google.com/uc?id=1QlI0riqRCNwR88VEVcdO-beHF0MhWHIO
Summary
JTextComponentなどで使用中のインプット・メソッドが変換のために使用可能な場合はCaretの色を変更するよう設定します。
Screenshot

Advertisement
Source Code Examples
JTextArea textArea = new JTextArea() {
@Override public void updateUI() {
super.updateUI();
Caret caret = new InputContextCaret();
caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
setCaret(caret);
}
};
//...
class InputContextCaret extends DefaultCaret {
private final Color caretFg = UIManager.getColor("TextArea.caretForeground");
@Override public void paint(Graphics g) {
if (isVisible()) {
JTextComponent c = getComponent();
boolean b = c.getInputContext().isCompositionEnabled();
c.setCaretColor(b ? Color.RED : caretFg);
}
super.paint(g);
}
}
View in GitHub: Java, KotlinDescription
DefaultCaret#paint(...)
をオーバーライドして、JTextComponent#getInputContext()
でInputContext
を取得- 取得した
InputContext
で現在のインプット・メソッドが変換のために使用可能かをInputContext#isCompositionEnabled()で判別ON
: 使用可能の場合はCaret
の色をCaret#setCaretColor(Color.RED)
で変更- 入力途中の
Caret
色や下点線の変更方法が見つからない → 調査中
- 入力途中の
OFF
: 使用可能の場合はCaret
の色をCaret#setCaretColor(UIManager.getColor("TextArea.caretForeground"))
で戻すTextField.caretForeground
、PasswordField.caretForeground
、TextPane.caretForeground
、EditorPane.caretForeground
などで他のJTextComponent
のCaret
デフォルト色を個別に取得可能