简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM