Putting text from JTextField to JLabel

489 views Asked by At

I 'm working on code that takes a string from a JTextField and puts it on a JLabel.

But my problem is always the string shown is missing the last character.

 private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)     {
      String  ch=jTextField1.getText();
      jLabel1.setText(ch);
} 

enter image description here

1

There are 1 answers

1
mastov On BEST ANSWER

It's better to use a DocumentListener to check for changes in the jTextField. Not only will that notify you after the changes of the additional character are incorporated, but you will also be notified about changes unrelated to keyboard events (e.g. pasting text).