how to detect input is char but not no.

hello.......

i have a text input but i would like user enter no. only but not char/words........so how to write the code

thanks
bhuey_lingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

expertmbCommented:
hi,
try this

    jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {

      public void keyPressed(KeyEvent e) {

     int iKeyCode=e.getKeyCode();
    String sLength = jTextField1.getText();
    if((sLength.length()>12)&&(iKeyCode!=e.VK_BACK_SPACE)&&(iKeyCode!=e.VK_DELETE)){
        jTextField1.setEditable(false);
    }
    else if(iKeyCode == e.VK_ENTER){
      jTextField1.setEditable(true);
    }
    else if(isChar(e.getKeyChar())){
      jTextField1.setEditable(false);
    }
    else{
      jTextField1.setEditable(true);
   }

      }

      public void keyReleased(KeyEvent e) {
    jTextField1.setEditable(true);

      }
    });

  public boolean isChar(char ch){

   char[] oChar ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
   'p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H',
   'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','~',
   '`','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','[','}','}',
   '|','\\',':',';','"','\'','<',',','>','.','?','/'};
   for(int iCount =0;iCount<oChar.length;iCount++){
     if(ch==oChar[iCount]){
       return true;
     }
   }
   return false;
 }


mb...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vivexpCommented:
Hi,

This is what  i tried out to input only nos. Probably u have to modify a little bit.

JTextField jtf;
static String s1;

jtf.addKeyListener(new java.awt.event.KeyAdapter() {

      public void keyPressed(KeyEvent e) {
        jtf_keyPressed(e);
      }

      public void keyReleased(KeyEvent e) {
        jtf_keyReleased(e);
      }
    });


Methods.....
 void jtf_keyPressed(KeyEvent e) {
   System.out.println (""+e.getKeyCode ());
    if(e.getKeyCode ()<=65){
    s1=s1+String.valueOf (e.getKeyChar ());
      jtf.setText (s1);
   }
   else
      jtf.setText (" ");
  }

  void jtf_keyReleased(KeyEvent e) {
  if(e.getKeyCode ()>=65) {
      jtf.setText (" ");
      s1="";
  }
  }

Try out...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.