2

I have a JTextField that works perfectly fine when someone enters a number instead of a letter. My only problem is that that the number then does not dissappear. The user can't enter any other numbers but that last number pressed stays always in the filed! Why?

    searchF.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                char ch = e.getKeyChar();
                if(Character.isDigit(ch)){
                    searchF.setText(" ");
                    JOptionPane.showMessageDialog(null, "Please Enter Only Names or Surnames. Letters Only Allowed");
                    searchF.setText(" ");
                   }
               }
 });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lambros
  • 151
  • 2
  • 14

3 Answers3

4

I have a JTextField that works perfectly fine when someone enters a number instead of a letter. My only problem is that that the number then does not dissappear.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    LOL :-) Who are you ? If you are __mKorbel__, than who is [mKorbel](http://stackoverflow.com/users/714968/mkorbel), though the way you posted this answer, resembles very much like his's :-) – nIcE cOw Oct 07 '13 at 12:42
  • @nIcE cOw once time I'm by using [OpenID](http://meta.stackexchange.com/questions/190442/myopenid-no-longer-supported-add-alternative-login-method-to-your-account) for login to SO...., aaaaand today I decided to add more loggin, rest of my always, (and sure) enless success everywhere you can to see .... aaach – mKorbel Oct 07 '13 at 12:55
  • 1
    @nIcE cOw but its exciting to be newbee here, I'm going crazy from rulles here, how easy my live (here) was up to today .... – mKorbel Oct 07 '13 at 12:57
  • 1
    On the other hand, you aren't (for very a brief time, with your rate of gaining reputation), bothered by the endless close vote queue. The rules won't bother you long. – kiheru Oct 07 '13 at 13:04
  • @kiheru :-) and there are two possibilities a) I have to built my rep again..., (already asked my success) mods will merge my two accounts .... – mKorbel Oct 07 '13 at 13:10
  • @mKorbel : Thankyou for letting us know, I didn't knew that fact, that they are about to shut down OpenID login thingy. I just created a new login for myself too :-) – nIcE cOw Oct 07 '13 at 13:45
3

You'll get the event before it's handled by the TextField. What you can do is consume the event, that way the TextField won't receive it.

(A better approach might be a DocumentFilter, you can still copy-paste numbers into the TextField with the KeyListener.)

Alowaniak
  • 571
  • 3
  • 10
3

This is another way of coming at the issue, Consume the key event if it is a number, that way the user doesn't lose their input but you still get the message.

  if(Character.isDigit(ch)){
                JOptionPane.showMessageDialog(null, "Please Enter Only Names or Surnames. Letters Only Allowed");
             e.consume();
               }
Levenal
  • 3,796
  • 3
  • 24
  • 29
  • 1
    @Lambros, this is NOT the proper solution. Try pasting some text into the text field and see what happens. – camickr Oct 07 '13 at 15:22
  • text works fine! but numbers don't.. that's what I wanted! :) – Lambros Oct 08 '13 at 21:16
  • 1
    @Lambros he meant try pasting (via clipboard) some text (which contains numbers) into it. Since you're catching key-presses and check them for being a digit you can press `ctrl+v` which won't get caught by your checking for a digit. The text you're pasting will then be put into the textfield while it can contain numbers. – Alowaniak Oct 09 '13 at 05:11
  • 2
    @Lambros by using a `DocumentFilter`; @nIcE cOw commented on your question showing a [related example](http://stackoverflow.com/questions/9477354/how-to-allow-introducing-only-digits-in-jtextfield/9478124#9478124) – Alowaniak Oct 09 '13 at 08:24