What's the best way to get a value of a TextArea after a key is typed, including this character?
If I do it in the even listener, textarea.getText()
returns the value without the eventual new char.
Basically I see two ways:
postponing processing with something like invokeLater(). I would prefer a solution without threads.
figuring out where to put the char into the text, based on the carret position.
Is there any other, simpler?
Thanks.
Edit: This is what I have:
JTextArea textarea = (JTextArea) evt.getComponent();
String texySource = textarea.getText();
char keyCode = evt.getKeyChar();
//if( Character.isLetterOrDigit( keyCode ) || Character.isSpaceChar( keyCode ) )
if( keyCode >= 0x20 || keyCode == 0x0A || keyCode == 0x0D ){
// TODO: The carret doesn't have to be at the end...
//texySource += Character.toString( evt.getKeyChar() );
String ch = Character.toString( evt.getKeyChar() );
texySource = StringUtils.overlay(texySource, ch,
textarea.getSelectionStart(),
textarea.getSelectionStart() );
}