0

/* I m trying to make a TEXTBOX using Java/eclipse. textbox only accept numeric values via keybord and that's have using swing + sing "single time" at "first position" - sing "single time" at "first position" . period "single time" at "any position" and delete and backspace work perfectely */

import javax.swing.*;  
import java.awt.event.KeyEvent;  
import java.awt.event.KeyListener;  

public class NumericBox extends JApplet{ 
    //making textfield
    static JTextField tf = new JTextField();  
    //main start
    public static void main(String arh[]){  

        NumericBox g = new NumericBox();  
        Handle h = g.new Handle();  
        JFrame f = new JFrame();  //frame
        JPanel p = new JPanel();  //pannel

        tf.setColumns(5);  
        tf.addKeyListener(h);  

        p.add(tf);    
        f.add(p);  
        f.setSize(200,100);  
        f.setVisible(true);  

    }//end of main method  
   // to use only numaric key implementation

    private class Handle implements KeyListener{  

        public void keyTyped(KeyEvent e) {
            char vChar = e.getKeyChar();
            if (!(Character.isDigit(vChar)
                  || (vChar == KeyEvent.VK_BACK_SPACE)
                  || (vChar == KeyEvent.VK_CLEAR)
                  || (vChar == KeyEvent.VK_PERIOD)
                  || (vChar == KeyEvent.VK_PLUS)
                  || (vChar == KeyEvent.VK_MINUS))) {
                getToolkit().beep();
                e.consume();
            }

       /*    if(vChar == KeyEvent.VK_PERIOD){
                            if(i==1)
                                e.consume();
                            else
                                i=1;
                        }
         */ 
        }

        public void keyPressed(KeyEvent e) {}  //keyPressed  events
        public void keyReleased(KeyEvent e) {}  //keyReleased events

    }//end of inner class  

}//end of outer class  
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
abhinav
  • 21
  • 2
  • Trying to do that by filtering key events is a fragile approach (think about pasting to the field, for instance). Take a look at the answers [here](http://stackoverflow.com/questions/5662651/how-to-implement-in-java-jtextfield-class-to-allow-entering-only-digits), for example. – kiheru Jan 07 '15 at 16:41
  • + sing "single time" at "first position" - sing "single time" at "first position" . period "single time" at "any position" not working only in my code +,-,. are repated and +,- are on diff-diff pos that is wrong – abhinav Jan 07 '15 at 16:56
  • See http://stackoverflow.com/a/13424140/1076463 – Robin Jan 07 '15 at 18:00
  • Use a DocumentFilter, and test for `Double.parseDouble(...)` within it. See [link](http://stackoverflow.com/questions/12793030/jtextfield-limiting-character-amount-input-and-accepting-numeric-only) for details. – Hovercraft Full Of Eels Jan 07 '15 at 22:27

2 Answers2

0

Validating your text would be handled best via a regular expression. As for catching the value changes, go for the underlying document provided by Swing, following the example here: https://stackoverflow.com/a/3953219/1843508

Community
  • 1
  • 1
Ofer Lando
  • 814
  • 7
  • 12
0

You can either use JFormattedTextField or as @Ofer Lando said, using regex(regular expression)

Example,

private boolean containsNumber(String sentence)
{
    Pattern patt = Pattern.compile(".*?[0-9].*");
    Matcher m = patt.matcher(sentence);
    return m.matches();
}
Voqus
  • 159
  • 1
  • 11