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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.