Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextPane clear text

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?

like image 312
Bv202 Avatar asked Dec 02 '11 22:12

Bv202


1 Answers

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.

like image 147
camickr Avatar answered Oct 01 '22 02:10

camickr