Search code examples
javaswingline-breakskeylistenerjeditorpane

Java action on Enter key entered, not display line break


I am doing a chat application and I put a JEditor pane to type the message.

I put a key listener on this JEditor so that when I type the Enter key, it sends the message.

My problem is that it sends the message but it also does a line break in my JEditor and it doesn't look good even if right after after I also do a setText("").

How to avoid the display of the line break ?

Thank you very much

Regards.


Solution

  • Don't use a KeyListener.

    Swing was designed to be used with Key Bindings. The default Action is to add a new line string to the Document. You need to replace the default Enter Action with a custom Action of your own. You would start by extending TextAction.