title: Adding textfield validation to multiple textfields
body: <p>I'm trying to add validation to 4 JTextFields using only numeric characters (0-9). The code I have for one JTextField is:</p>
  
  <pre><code>txtf_xCoord.addKeyListener(new KeyAdapter()
      {
          @Override
          public void keyTyped(KeyEvent keyEvent)
          {
              if (txtf_xCoord.getText().length() &lt; 3 &amp;&amp; keyEvent.getKeyChar() &gt;= '0' &amp;&amp; keyEvent.getKeyChar() &lt;= '9')
              {
                  // Optional
                  super.keyTyped(keyEvent);
              }
              else
              {
                  // Discard the event
                  keyEvent.consume();
              }
          }
      });
  </code></pre>
  
  <p>Is there a more efficient way of adding this validation to the rest of the JTextFields without copy and pasting the code for each?</p>