title: Restrict Input of JTextField to Double Numbers?
body: <p>In java , i am trying to make simple currency converter, but for that i need a text field which can restrict input to numbers only and more importantly double numbers. I tried using <code>JFormatedTextField</code> but <strong>it only format the input after you have done your input and click elsewhere</strong> but i need to restrict TextField to <strong>consume()</strong> each invalid character while doing input.</p>
  
  <p><strong>Possible Attempts:</strong></p>
  
  <p>Using <code>JFormatedTextField</code>:</p>
  
  <pre><code>JFormatedTextField textField = new JFormatedTextField(new DoubleFormat());
  textField.setBounds(190, 49, 146, 33);
  frame.getContentPane().add(textField);
  textField.setColumns(10);
  </code></pre>
  
  <p>Using <code>KeyTyped Event</code>:</p>
  
  <pre><code>char c = arg0.getKeyChar();
  if(!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c== KeyEvent.VK_DELETE)){
      arg0.consume();
  }
  </code></pre>
  
  <p>Using <code>KeyTyped Event</code> with regex:</p>
  
  <pre><code>if(!((textField.getText().toString+arg0.getKeyChar()).matches("[0-9]*(.[0-9]*)?"))){
     arg0.consume();
  }
  </code></pre>
  
  <p>Second and third attempt were close but then second attempt failed on point values and third attempt always read first character on textField no matter what it is, So any suggestions ? i am not very fond of JAVA GUI so kindly be patient.</p>