So my problem is that, i have a text area with 3 rows. Its a part of a chat application which receives text from a user.

I want to send the text and clear the text area as soon as the user presses the enter key. Now the problem is that whenever user presses the enter key the cursor goes to next line(row) and then on clearing the text area it comes to first, so it looks weird to a user that the cursor goes to next line and then comes to first.

So I want to stop the cursor from going into the next line on press of enter key, i had used key events but found of no use, anyone have suggestions for this??

Dani AI

Generated

described the classic chat-area flicker: pressing Enter lets the caret move to the next line, then the area is cleared and the caret jumps back. 's KeyListener/e.consume() fix is valid and explains why the newline can be suppressed. For Swing components the more robust, idiomatic solution is to use Key Bindings (InputMap/ActionMap). Key Bindings run before the component's default insert action, work better with focus traversal and IME, and make it easy to support Enter-to-send while keeping Shift+Enter for a true newline.

Here is a compact Key Binding example (replace sendMessage(...) with the app's send logic):

JTextArea text = new JTextArea(3, 40);
InputMap im = text.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap am = text.getActionMap();

im.put(KeyStroke.getKeyStroke("ENTER"), "send");
im.put(KeyStroke.getKeyStroke("shift ENTER"), "insert-break");

am.put("send", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        String msg = text.getText().trim();
        if (!msg.isEmpty()) {
            sendMessage(msg); // implement sendMessage
            text.setText("");
        }
    }
});

am.put("insert-break", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        text.append(System.lineSeparator());
    }
});

Troubleshooting notes: if a KeyListener must be used, check KeyEvent.VK_ENTER with keyPressed and call e.consume(), but expect edge cases with IME and focus. After clearing, calling setCaretPosition(0) is a last resort; avoiding the initial newline insertion (as above) prevents the visible jump altogether.

Recommended Answers

All 4 Replies

On a key pressed event, test for and remove the newline character.

I had failed to remove the new line character may be, the problem is still the same.
Can you please help me with a code snippet may be I am not doing that properly

public static void main(String[] args) {
        JFrame frame = new JFrame("Hello, World!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 250);
        frame.setLayout(new BorderLayout());
        JTextArea text = new JTextArea();
        text.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
                
            }

            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == e.VK_ENTER) e.consume();
            }

            public void keyReleased(KeyEvent e) {

            }

        });
        JScrollPane scroller = new JScrollPane(text);
        frame.add(scroller, BorderLayout.CENTER);
        frame.setVisible(true);
    }

Oh thanks a lot, i was missing the consume function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.