title: Which is the best way to validate data in a JTextField?
body: <p>I tried this, but the JTextField keeps showing the last key entered. What I'm I doing wrong?...</p>
  
  <pre><code>private void codigoKeyTyped(java.awt.event.KeyEvent evt) {
      //codigo is the JTextField
      String s = String.valueOf(evt.getKeyChar());
      try{
          Integer.parseInt(s);
      }catch(Exception e){
          codigo.setText("");    
      }
  }
  </code></pre>
  
  <p>Lastly, I want it to delete the last character and leave the integer value in the text field. But I guess that would be simple after solving that. Thanks!</p>
  
  <p>//ANSWER!</p>
  
  <p>Thanks everybody to answer my question. Finally I made this</p>
  
  <pre><code>private void codigoKeyTyped(java.awt.event.KeyEvent evt) {
  
      char c = evt.getKeyChar();
  
      if (! Character.isDigit(c)) {
          evt.consume();            
      }
  }
  </code></pre>