1

Using the method below, I am trapping the VK_ENTER key event to gather the text in a default JTextArea, process it, and then empty the JTextArea. I noticed that the caret did not reset to the upper left but to the first column of the second row instead. No matter what I have tried, I cannot seem to convince the caret to go back to the upper left corner (it's original starting position). You can see my attempts to understand where the caret believes that it is. There is definitely an empty line in place after the Enter key is trapped for the first time.

The reason I use JTextArea is because my text to process may be up to 256 bytes long, so a JTextField is cumbersome.

private void jTextArea2KeyPressed(java.awt.event.KeyEvent evt) {                                      
    if(evt.getKeyCode() == KeyEvent.VK_ENTER){
        try {
            sendCommand("hf", "ab4mw", jTextArea2.getText().trim());
            jTextArea1.append(TX + jTextArea2.getText().trim() + nl);
            jTextArea2.setText("");
            System.out.println("carat: " + jTextArea2.getCaretPosition());
            System.out.println("length: " + jTextArea2.getText().length());
            jTextArea2.setCaretPosition(0);
            //jTextArea2.moveCaretPosition(0);
            System.out.println("line: " + jTextArea2.getLineCount());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Output of the first use of this method: carat: 0 length: 0 line: 1

Output of the second use of this method: carat: 0 length: 0 line: 1

...ad infinitum.

Am I missing something simple here?

guitarpicva
  • 432
  • 3
  • 10

1 Answers1

2

Change your public void keyPressed(KeyEvent e) { method to

public void keyReleased(KeyEvent e) {

Ok, why is this only happening on keyPressed? I'm not quite sure, but my guess is that the JTextArea first processes the jTextArea2.setCaretPosition(0); before it breaks the line (what the enter key naturally does in a TextArea). This is obviously not the case when keyReleased, then it calls jTextArea2.setCaretPosition(0); after it breaks the line, and that is what you want.

Here is an example:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class Example extends JFrame {

    public Example() {

        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(2,0));

        JTextArea jTextArea1 = new JTextArea();
        jTextArea1.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    jTextArea1.setText("");
                    System.out.println("carat: "
                            + jTextArea1.getCaretPosition());
                    System.out.println("length: "
                            + jTextArea1.getText().length());
                    jTextArea1.setCaretPosition(0);
                    // jTextArea2.moveCaretPosition(0);
                    System.out.println("line: " + jTextArea1.getLineCount());
                }
            }
        });
        jTextArea1.setBorder(new LineBorder(Color.BLACK));
        jTextArea1.setText("This TextArea will not work like you want it to");

        JTextArea jTextArea2 = new JTextArea();
        jTextArea2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    jTextArea2.setText("");
                    System.out.println("carat: "
                            + jTextArea2.getCaretPosition());
                    System.out.println("length: "
                            + jTextArea2.getText().length());
                    jTextArea2.setCaretPosition(0);
                    // jTextArea2.moveCaretPosition(0);
                    System.out.println("line: " + jTextArea2.getLineCount());
                }
            }
        });
        jTextArea2.setBorder(new LineBorder(Color.BLACK));
        jTextArea2.setText("This TextArea WILL");

        frame.add(jTextArea1);
        frame.add(jTextArea2);
        frame.setSize(500, 500);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        new Example();
    }
}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • Immediate fix! I think you nailed it with the Pressed logic.Many thanks. It has been a while since using Swing. Timing is everything! – guitarpicva Aug 21 '15 at 23:05
  • @guitarpicva Glad I helped! Please consider accepting the answer (green tick), so everyone knows this solved the problem. It also gives you and me some reputation, but of course you don't have to accept it. – Lukas Rotter Aug 21 '15 at 23:12