Q:

Not Being able to clear the JTextArea(using actionlistener when enter key is pressed. the text area is left with a new line(\n))

I am making a mini project for my college assignment. I am making a Local Chat application to chat over the LAN network. Everything works fine except one little problem. I am using to text areas one is editable and another is not. I am using the enter key to send the text from the editable text area to the non-editable text area. After that I want the editable text area to be cleared. The code that I am using is  
textmsg.setText("");

i have also used 
textmsg.setText(null);

and
textmsg.removeAll();

but the editable text area is left with a new line("\n").
The output is something like this:
this is the image of the output window.
What I want is that when I press the enter key the message gets to the non-editable text area  if there is a text in there. If it is blank then i want nothing to happen.
The exact code that I used in my program is:
private void send(String message) {
    if(message.equals("")) return;
    console(message);
    textmsg.setText("");
}

public void console(String message) {
    txtHistory.append(message+"\n");
}

textmsg is the editable JTextArea. txtHistory is the non-editable text area.
send function is called on both button click and by pressing the enter key as well.
when I use the button it is working fine but not when I use the enter key.

A:

Suggestions:

Use Key Bindings to allow the editable text area to react to enter keypress. This will work with Swing's own bindings
Never add a KeyListener on a JTextComponent as this messes with the functioning of your text component. Key bindings is much safer and cleaner (see the tutorial link above).
Within the Action used in your binding, get the editable text area's text, append it to the other text area, clear the editable one's text by calling .setText("")

Something like:
// get text from editable text area
String text = editableTA.getText();
// append to non-editble text area
nonEditableTA.append(text + "\n");
editableTA.setText("");  // clear editable
editableTA.requestFocusInWindow(); // give it focus

for example
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class RespondToEnter extends JPanel {
    private static final int COLS = 40;
    private JTextArea nonEditableTA = new JTextArea(15, COLS);
    private JTextArea editableTA = new JTextArea(5, COLS);
    private EnterAction enterAction = new EnterAction("Submit");
    // private JButton submitButton = new JButton(enterAction);

    public RespondToEnter() {

        // first set up bindings
        int condition = JComponent.WHEN_FOCUSED; // only want this when textarea has focus
        InputMap inputMap = editableTA.getInputMap(condition); // get input and action maps
        ActionMap actionMap = editableTA.getActionMap();
        // bind to the enter key stroke:
        KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        String key = enterStroke.toString();  // key to bind input to action
        // bind the enter key stroke to the enter action:
        inputMap.put(enterStroke, key);
        actionMap.put(key, enterAction);

        // allow word wrap
        nonEditableTA.setWrapStyleWord(true);
        nonEditableTA.setLineWrap(true);
        editableTA.setWrapStyleWord(true);
        editableTA.setLineWrap(true);

        nonEditableTA.setFocusable(false); // this is enough to prevent editing
        JScrollPane nonEdScrollPane = new JScrollPane(nonEditableTA);
        nonEdScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JScrollPane edScrollPane = new JScrollPane(editableTA);
        edScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        setLayout(new BorderLayout());
        add(nonEdScrollPane, BorderLayout.CENTER);
        add(edScrollPane, BorderLayout.PAGE_END);       
    }

    // action used in our key bindings
    private class EnterAction extends AbstractAction {
        public EnterAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);  // allow an alt-keystroke mnemonic key
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // get text from editable text area
            String text = editableTA.getText();
            // append to non-editble text area
            nonEditableTA.append(text + "\n");
            editableTA.setText("");  // clear editable
            editableTA.requestFocusInWindow(); // give it focus
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        RespondToEnter mainPanel = new RespondToEnter();
        JFrame frame = new JFrame("Respond To Enter Key");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

