4

I am looking to be able to verify that a JTextField contains only 'numbers' and no (+ , -) before sending the content of this JTextField in a Sql query (to avoid a SqlException).

I want :

  • if I enter a letter, a JLabel is displayed and the color of JTextField changes in red.

    label_errer.setVisible (true);

  • if I delete the letter, the JLabel will disappear and the color of JTextField is normal.

    label_errer.setVisible (false);

the following code works if I click "enter":

            textField_app = new JTextField(3);
    textField_app.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            JTextField source = (JTextField) e.getSource();
            String textFieldContent = source.getText();

            Color bgColor = Color.RED;

            boolean isNumeric=false;
            try { 

                Integer.parseInt(textFieldContent);
                 isNumeric = true;
                bgColor = Color.WHITE;
                label_errA.setVisible(false);
            } catch (Exception e2) {
                 // ---> isNumeric=false
            }

            source.setBackground(bgColor);


            if(isNumeric==false){
                label_errA.setEnabled(true);
                label_errA.setVisible(true);


            }

        }
    });

this solution works, but is there another?

        textField_app = new JTextField(3);
    KeyListener keyListener = new KeyListener() {
          public void keyPressed(KeyEvent keyEvent) {
            printIt("Pressed", keyEvent);
          }

          public void keyReleased(KeyEvent keyEvent) {
            printIt("Released", keyEvent);
          }

          public void keyTyped(KeyEvent keyEvent) {
            printIt("Typed", keyEvent);
          }

          private void printIt(String title, KeyEvent keyEvent) {
            int keyCode = keyEvent.getKeyCode();
            String keyText = KeyEvent.getKeyText(keyCode).toString();


            if(keyCode==(getKeyBinding(keyText))){ 
            textField_app.setBackground(new Color(220, 20, 60));
            label_errA.setEnabled(true);
            label_errA.setVisible(true);
            }
            else  {
                 Color bgColor =Color.WHITE;
                 textField_app.setBackground(bgColor);

                label_errA.setEnabled(false);
                label_errA.setVisible(false);
            }

          }
        };


public int getKeyBinding(String k){
  if(k.equals("A")){
   return KeyEvent.VK_A;
  } else if(k.equals("B")){
   return KeyEvent.VK_B;
  } else if(k.equals("C")){
   return KeyEvent.VK_C;
  } else if(k.equals("D")){
   return KeyEvent.VK_D;
  } else if(k.equals("E")){
   return KeyEvent.VK_E;
  } else if(k.equals("F")){
   return KeyEvent.VK_F;
  } else if(k.equals("G")){
   return KeyEvent.VK_G;
  } else if(k.equals("H")){
   return KeyEvent.VK_H;
  } else if(k.equals("I")){
   return KeyEvent.VK_I;
  } else if(k.equals("J")){
   return KeyEvent.VK_J;
  } else if(k.equals("K")){
   return KeyEvent.VK_K;
  } else if(k.equals("L")){
   return KeyEvent.VK_L;
  } else if(k.equals("M")){
   return KeyEvent.VK_M;
  } else if(k.equals("N")){
   return KeyEvent.VK_N;
  } else if(k.equals("O")){
   return KeyEvent.VK_O;
  } else if(k.equals("P")){
   return KeyEvent.VK_P;
  } else if(k.equals("Q")){
   return KeyEvent.VK_Q;
  } else if(k.equals("R")){
   return KeyEvent.VK_R;
  } else if(k.equals("S")){
   return KeyEvent.VK_S;
  } else if(k.equals("T")){
   return KeyEvent.VK_T;
  } else if(k.equals("U")){
   return KeyEvent.VK_U;
  } else if(k.equals("V")){
   return KeyEvent.VK_V;
  } else if(k.equals("W")){
   return KeyEvent.VK_W;
  } else if(k.equals("X")){
   return KeyEvent.VK_X;
  } else if(k.equals("Y")){
   return KeyEvent.VK_Y;
  } else if(k.equals("Z")){
   return KeyEvent.VK_Z;
  } 
else{
   return 0;
  }
 }
Dev M
  • 1,519
  • 3
  • 29
  • 50
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). You might use a `JSpinner` in place of the `JTextField` for this use-case. – Andrew Thompson May 17 '12 at 23:47
  • @MBenT : Have a look at [this](http://stackoverflow.com/questions/9477354/how-to-allow-introducing-only-digits-in-jtextfield/9478124#9478124) example and [this](http://stackoverflow.com/questions/5662651/how-to-implement-in-java-jtextfield-class-to-allow-entering-only-digits) ... – nIcE cOw May 18 '12 at 15:26

4 Answers4

3

You are looking for a JFormattedTextField. You have all the information here to get yourself started.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
3

What you should probably do to detect every key press is to use a DocumentListener. But since your goal is validation, take a look at using a DocumentFilter instead. It's a little more complicated, but it's a cleaner way to do it, and you won't get any concurrent modification exceptions.

You can create a DocumentFilter, and then every key press will need to pass the filter's inspection (a rough way of putting it, but fairly accurate). If the filter deems it OK, it puts it through. You can also add any actions of your own, such as turning the field red, as you mentioned.

wchargin
  • 15,589
  • 12
  • 71
  • 110
1

I think you are looking for the java.awt.event.KeyListener instead of ActionListener. Use the KeyTyped() function.

user845279
  • 2,794
  • 1
  • 20
  • 38
0

I wrote a library that I can give you a link to if you want, which gives you Textfields that can only take numbers and nothing else.

Yes, you can look at the source too, no problem.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • http://dl.dropbox.com/u/2978946/JNumberField.zip here you go. If you find it useful, please approve this as your answer to the question. It helps :) – OmniOwl May 17 '12 at 22:23
  • `int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode).toString(); if(keyText.equals("A")){ label_errA.setEnabled(true); label_errA.setVisible(true); } else { label_errA.setEnabled(false); label_errA.setVisible(false); }` how to compare multiple character? contains(CharSequence s) – Dev M May 18 '12 at 11:30