I have the following code which is meant to update a JTextArea every time a key is pressed and the focus is within the JTextField and append the content of JTextField on a new line within the JTextArea.
My problem is that every time I press a key, the JTextArea update is always one key behind where I want it.
Example: I type "cat" into the JTextField, and only "ca" appears in the JTextArea, instead of the full String "cat" which is what I want.
Any advice appreciated, thanks for taking the time to read.
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Testing extends JFrame {
JTextField text;
JTextArea textArea;
public static void main(String[] args) {
Testing gui = new Testing();
gui.go();
}
public void go() {
this.setLayout(new BorderLayout());
text = new JTextField();
text.addKeyListener(new TestKeyListener());
textArea = new JTextArea();
this.add(text, BorderLayout.NORTH);
this.add(textArea, BorderLayout.CENTER);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}
private class TestKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent evt) {
textArea.append(text.getText() + "\n");
}
}
}
That happens because keyPressed
even happens before the typed character is saved into the JTextField document. So when you are trying to retrieve text you "seem" to be one step behind the actual value, but its not true - JTextField simply doesn't have the new value at the moment.
I know two possible ways to listen to JTextField document changes to do the stuff you are trying to achieve, each of them works for this specific case:
public class TestingTextArea extends JFrame
{
JTextField text;
JTextArea textArea;
public static void main ( final String[] args )
{
final TestingTextArea gui = new TestingTextArea ();
gui.go ();
}
public void go ()
{
this.setLayout ( new BorderLayout () );
text = new JTextField ();
// Add DocumentListener
text.getDocument ().addDocumentListener ( new DocumentChangeListener () );
// Or add UndoableEditListener
text.getDocument ().addUndoableEditListener ( new UndoableEditAdapter () );
textArea = new JTextArea ();
this.add ( text, BorderLayout.NORTH );
this.add ( textArea, BorderLayout.CENTER );
this.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
this.setSize ( 300, 300 );
this.setVisible ( true );
}
public class DocumentChangeAdapter implements DocumentListener
{
@Override
public void insertUpdate ( final DocumentEvent e )
{
documentChanged ( e );
}
@Override
public void removeUpdate ( final DocumentEvent e )
{
documentChanged ( e );
}
@Override
public void changedUpdate ( final DocumentEvent e )
{
documentChanged ( e );
}
public void documentChanged ( final DocumentEvent e )
{
textArea.append ( text.getText () + "\n" );
}
}
public class UndoableEditAdapter implements UndoableEditListener
{
@Override
public void undoableEditHappened ( final UndoableEditEvent e )
{
textArea.append ( text.getText () + "\n" );
}
}
}