1

I want to delete the contents of my JTextPane as the user types, I mean

JTextPane.addKeyListener(new java.awt.event.KeyAdapter()
{
   public void keyTyped(java.awt.event.KeyEvent evt)
   {
      searchFieldHandler(evt);
   }
});

private void searchFieldHandler(java.awt.event.KeyEvent evt)
{        
   text = searchField.getText() + String.valueOf(evt.getKeyChar());
   **searchField.setText("");**

   // doing sth with text
}

but it doesn't work(doesn't delete the contents and the last entered char remains) how to do that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Soheil
  • 1,676
  • 7
  • 34
  • 65

1 Answers1

6

Use DocumentListener if you want to do some actions after the user typed into the textPane, and use DocumentFilter if you want to filter the input before it appears on the textPane.

See:

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • 2
    +1 See also [*Implementing a Document Filter*](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter) – trashgod Feb 06 '13 at 09:17