0

im trying to validate a jtextfield from the keypress and would like it to throw an error message in a jLabel. I am trying to do this with in the code below.

private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {   


    }                                   
Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42
  • Add full reproducible code of what you have tried – Beez Dec 11 '19 at 16:06
  • Have you considered [InputVerifier](https://docs.oracle.com/javase/9/docs/api/javax/swing/InputVerifier.html) ? What about [DocumentListener](https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html) ? – Abra Dec 11 '19 at 18:14
  • No, How do i use it in this context? – Kevin Medjiako Dec 11 '19 at 18:59
  • [How to validate a JTextField?](https://stackoverflow.com/questions/2749521/how-to-validate-a-jtextfield) – Abra Dec 12 '19 at 02:29

1 Answers1

0

Can't comment, so i'll answer instead. If you are looking to validate the input of the user as he/she type, you might wanna have a look at javax.swing.text.DocumentFilter.

If you are simply looking to update a JLabel when the user types something, you should add a KeyboardListener to the JTextField. You have to be a bit careful with that tho, as swing doesn't guarantee the order in which listeners are called, meaning that the text may not have updated when the KeyboardListener is invoked.

Tegon McCloud
  • 63
  • 1
  • 6