0

I have a text area created using JTextArea editorText = new JTextArea(); and I need to process the keystrokes being entered into the textArea before they can be displayed in the textArea. But it is getting displayed on the textArea even though I use a keyAdapter like so :

private class KeyPressListener extends KeyAdapter 
   {
      public void keyPressed(KeyEvent evt) {
        editorText.append(""+evt.getKeyCode());
      } 
   }

editorText.setKeyListener(keyAdapter); Is there a way for me to process the information entered into the textarea and then display it on the screen?

1 Answers1

4

Don't use a KeyListener for this. If you need to process the input before it is displayed, this would be considered "filtering" the input, and in this situation, you will want to set your JTextArea's Document's DocumentFilter via setDocumentFilter(...).

For example, please have a look at my answer and code to this question.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1, Also look at the section from the Swing tutorial on [Implementing a Document Filter](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter). The Swing tutorial is full of examples. I suggest you bookmark it for easy access that can help you with other Swing basics. – camickr Nov 23 '13 at 22:10
  • Although I marked this problem solved, I have not acheived the desired result. Please see http://stackoverflow.com/questions/20179370/what-should-i-do-when-documentfilter-not-listening-for-changes – Prasana Venkat Ramesh Nov 24 '13 at 18:48