InputContextが使用可能な状態でCaretの色を変更
Total: 17
, Today: 17
, Yesterday: 0
Posted by aterai at
Last-modified:
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
デフォルト色を個別に取得可能
- InputMethodListener (Java Platform SE 8)では現在のインプット・メソッドの使用可・不可の切り替えを取得できない?