The problem is that keyPressed
is being called before the TextBox is updated.
Instead of
tf.addKeyListener(this);
Try using this:
tf.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
printIt();
}
public void removeUpdate(DocumentEvent e) {
printIt();
}
public void insertUpdate(DocumentEvent e) {
printIt();
}
public void printIt() {
System.out.println(tf.getText());
}
You'll need to import javax.swing.event.DocumentEvent
and javax.swing.event.DocumentListener
.