/* 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