Dev M
Dev M

Reputation: 1709

How to listen to the input of a characters in a JTextField?

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 :

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;
  }
 }

Upvotes: 4

Views: 3057

Answers (4)

wchargin
wchargin

Reputation: 16047

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.

Upvotes: 3

Guillaume Polet
Guillaume Polet

Reputation: 47607

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

Upvotes: 3

user845279
user845279

Reputation: 2804

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

Upvotes: 1

OmniOwl
OmniOwl

Reputation: 5709

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.

Upvotes: 0

Related Questions