6

I'm trying to remove all text from a JTextPane. I thought you could simply use:

textPane.setText(""); 

This DOES work, but for some reason, there always is an empty line after calling that method. Why is that and how do I prevent that?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Bv202
  • 3,924
  • 13
  • 46
  • 80
  • I've just set up a test case and set the pane to "" and null and it seems to clear everything ok. Are you referring to the line it leaves so you can actually type something in? – pimaster Dec 02 '11 at 22:33

1 Answers1

7

Probably because you are using a KeyListener to listen for the Enter key and then clear the text. Well JTextPane has an Action which adds a newline character when the Enter key is pressed and the is happening after you clear the text pane.

The proper solution is to use Key Bindings and replace the default Action with an action that clears the text pane.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm indeed using a KeyListener to listen for the enter key. I'll have a look at Key Bindings, thanks. But what do you mean with "replace the default Action with an action that clears the text pane."? – Bv202 Dec 02 '11 at 22:35