5

I am trying to check that the text in a JTextField matches a perticular pattern, and if it does / doesn't display a message the user. This is what I have so far:

 public class input extends KeyListener{
// Some code here
final JTextField inputField = new JTextField(35);
// Some more code...
   public void generate(){
      // Some GUI code here...
     inputField.addKeyListener(this);
   }
   public void keyPressed(KeyEvent e) {}
   public void keyReleased(KeyEvent e) {}
   public void keyTyped(KeyEvent e) {
      if(e.getSource() instanceof JTextField && e.getSource().equals(inputField)){
         if(Pattern.matches("../../....", (JTextComponent) e.getSource()).getText())))
             System.out.println("Yh, it works");
         else System.out.println("EPIC FAIL (LOL)");
     }
   }
}

And it does actually work almost perfectly. However, if I paste something using CTRL + V, I have to type two more characters (as opposed to one) before the KeyListener registers that the string is different! So does any one have any idea's why?

Sorry if I have missed out any details - I have tried to make the post as short and concise as possible; so please don't hesitate to ask anything...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andy
  • 3,600
  • 12
  • 53
  • 84

1 Answers1

8

For starters, don't use a KeyListener for this type of problem as it is doomed to fail, and even if you get it to work, it's a kludge at best. Instead I'd use either an ActionListener if I wanted to do my checking after the user is completely done entering information, or a DocumentListener if I want to check input as a user is entering, but am not going to block that entering or change the displayed text, or a Document Filter if I'm going to check the input as the user is entering and block it or change it if it is not appropriate.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • As I am using a JTextField (and don't want to use anything else), I am unaware of any alternatives to the KeyListener because I want to check the input as the user is entering it, but the DocumentListener doesn't seem to be available to attach to a JTextField – Andy Sep 29 '11 at 19:21
  • No DocumentListener attaches to the JTextField's ***Document***. Check out the tutorial on this (Google will show you where it is). – Hovercraft Full Of Eels Sep 29 '11 at 19:24
  • Apologies, I just realised that before your commented! Which method do I need to use out of the DocumentListener though because it appears that there is not just one method to detect the addition, and deletion of a character, so do I need to put the same code in *insertUpdate()*, and *removeUpdate()*? – Andy Sep 29 '11 at 19:30
  • It's entirely up to you and for you to decide -- do you want to react to their adding text and removing text? Experiment with your code and see what happens. – Hovercraft Full Of Eels Sep 29 '11 at 19:34
  • +1 for suggesting any solution but using a KeyListener. Check out the Swing tutorial for more information on using a [DocumentFilter](http://download.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter). – camickr Sep 29 '11 at 19:50
  • 1
    +1 Agree. I used a `DocumentListener` [here](http://stackoverflow.com/revisions/3949518/4), but this appealing [alternative](http://stackoverflow.com/questions/3949382/jspinner-value-change-events/7587253#7587253) was new to me. – trashgod Sep 29 '11 at 23:42
  • @trashgod: Thanks for the nice link - looks interesting. I'm using `DocumentListener` for now but this I'll definitely consider that alternate method in the future!.. – Andy Sep 30 '11 at 16:58
  • @camickr & Hovercraft Full Of Eels: Just out of interest, why is the KeyListener so bad in this situation, and also, when is a KeyListener actually appropriate... – Andy Sep 30 '11 at 17:15
  • A KeyEvent is a low level event. A DocumentEvent is a more abstract high level event. It is generally better to use more abstract events. These are API's that have been added to the JDK to make the handling of events easier. A DocumentFilter was also added to Swing to provide the specific functionality to edit/manipulate the text before it is added to the Document. So the advice here is don't reinvent the wheel. You would use a KeyListener only if the other approaches don't work. – camickr Sep 30 '11 at 17:30