I'm using java, and I'm trying to make a JTextArea that is non-editable but still has the caret in the field. In other words, a Text Area that does not display characters typed by the user, but still has the blinking caret (i.e. focus).
I honestly stumped on this problem. I've tried mucking around with setEditable, but theres no way to keep the caret. I've also tried deleting the character the user enters as soon as they type it, but i can't stop it flashing on the screen.
I think the following will help you:
textArea.getCaret().setVisible(true);
or
textArea.getCaret().setSelectionVisible(true);
As for the answers above
textArea.getCaret().setVisible(true);
does not always work perfectly, if the TextArea or EditorPane loses focus, say you click on a different frame or something, when you come back the cursor will be invisible again.
I have had the same issues, it appears the solution is to add a focus listener and set it visible every time the editor gains focus.
text.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
text.getCaret().setVisible(true); // show the caret anyway
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With