title: Java Swing: How to get TextArea value including the char just typed?
body: <p>What's the best way to get a value of a TextArea after a key is typed, including this character?</p>
  
  <p>If I do it in the even listener, <code>textarea.getText()</code> returns the value without the eventual new char.</p>
  
  <p>Basically I see two ways:</p>
  
  <ol>
  <li><p>postponing processing with something like invokeLater(). I would prefer a solution without threads.</p></li>
  <li><p>figuring out where to put the char into the text, based on the carret position.</p></li>
  </ol>
  
  <p>Is there any other, simpler?</p>
  
  <p>Thanks.</p>
  
  <p><strong><em>Edit:</strong> This is what I have:</em></p>
  
  <pre><code>JTextArea textarea = (JTextArea) evt.getComponent();
  String texySource = textarea.getText();
  char keyCode = evt.getKeyChar();
  //if( Character.isLetterOrDigit( keyCode ) || Character.isSpaceChar( keyCode )  )
  if( keyCode &gt;= 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()    );
  }
  </code></pre>