Results 1 to 16 of 16
  1. #1
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default How to make a text field to accept numbers only?

    I want to make the tex field to accept numbers only

    Can anyone help?

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    you should add a text field listener like a
    Java Code:
    void textInputCorrection(JTextField tf){
                    tf.setInputVerifier(new InputVerifier() {
    
                                public boolean verify(JComponent comp) {
                                    JTextField textField = (JTextField) comp;
                                    String a=null;
                                    try {
                                      a=textField.getText();
                                      Double.parseDouble(a);                                                                                                  } 
                        catch (NumberFormatException e) {
    
                                    Toolkit.getDefaultToolkit().beep();
                                                                    }
                                    return false;
    
                                }
    
                            });
    
    
    
                          }

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You can figure out the idea next but it is the basic thing )))

  4. #4
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Exclamation

    thanks webuser,

    I used the code and called this method "onKeyReleased" event of the text field, but it does not work, it make the beep noise whenver i enter a letter however it then displays the letters. can u tell me where do i have to call this method,

    the other thing I dont udnerstand is in the boolean verify method, why do we have to return false at the end?


    thanks alot

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As Webuser explain in the above code segment, start from the the simple thing. See how he catch the invalid format. Simply try to convert that text on the textfield into a double value. What happen if you found invalid format, may be a character rather a number. You comes with an exception.

    Webuser easily use a try-catch block there. And catch the NumberFormatException, you comes with that once you found anything other than a number.

    On the catch block you can do anything you want. Webuser just comes with a beep notification. Or else you can use a message box as well, depends on your application.

    At the end what Webuser advice to you is use a listener for the text field. In that case what you have to do is, on the listener call the above method.

  6. #6
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    thanks Eranga

    i understand the code but my problem is on the catch block how can I stop the text field from displaying anything other than numbers. Yes we have a beep notification but I need to have some code to stop the letter for example "a" from getting displayed in the text field.

    Can you help me with that?

    much appreciated

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, I'll give a simple hint. Once a user type a key on the text field copied/append into another temporary variable. Then validate it using above code. Id the validation is success only add it into the text field back.

  8. #8
    faisalcmpm is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    I would suggest you to use a Document class and override insertString method.
    In that method u just match the input string with the rgular expression for intiger.
    If matches call super.insertString
    else call beep

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Working with regular expressions is the most powerful solution we have, true. But on our thread starter I don't think it's an easy way to do this. I think first what we should do is, help him to find the solution is simple way, which he can learn so easily. Learning RegEx is not an easy task. :) Any way, thumbs-up for your suggestion lol. :)

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    I used the code and called this method "onKeyReleased" event of the text
    You should better use keyPressed listener instead )
    There are many ways as to control text fiels input. You can modify
    Java Code:
    void textInputCorrection(JTextField tf)
    method to
    Java Code:
    void textInputCorrection(JTextField tf,String text)
    the second param is to get the current textfield value as a basic correct numeric value.
    So you can invoke the method as...
    Java Code:
    void jTextField1_mousePressed(MouseEvent e) {
        textInputCorrection(jTextField1,jTextField1.getText());
      }

  11. #11
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    thanks everyone alot, it's working..

    cheers.

  12. #12
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    guys i just noticed another problem, here is the code I used:
    void textInputCorrection( final JTextField tf){
    tf.setInputVerifier(new InputVerifier() {

    public boolean verify(JComponent comp) {
    JTextField textField = (JTextField) comp;
    String a=null;
    a=textField.getText();

    try {

    Integer.parseInt(a);

    return true;
    }
    catch (NumberFormatException e) {

    Toolkit.getDefaultToolkit().beep();
    String s[];
    s = a.split("");
    String text ="";
    for(int i=0;i<s.length-1;i++){

    text = text + s[i];

    }
    tf.setText(text);


    }

    return false;
    }

    });



    }

    basically what i am doing is checking that if the "Verify" method return false(meaning a non integer has been entered) then I go through the text and remove the last element of it(that is that very non-integer element).

    The problem is here that I can not enter more than 9 numbers, as soon as I enter the 10 number it treat is as I have entered a non-integer.

    what do u think is the cause of the problem?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  14. #14
    faisalcmpm is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    an int variable can hold a max value of 2147483647 (total 10 digit).
    If u have entered the 10th digit such that the value is more than 2147483647 then offcourse you will get java.lang.NumberFormatException:

  15. #15
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    yes here is the code for numeric values for text field
    //java script validation for easy numeric only text field
    function isNumberKey(evt)
    {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
    return true;

    }

    //source code method calling area

    bye using this simple javascript u can do u r text field as numeric values only

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. launching Jframe from text field
    By crunchymonkey in forum AWT / Swing
    Replies: 4
    Last Post: 10-15-2008, 03:50 AM
  2. need to add text field on existing dialog
    By SHAshank1 in forum SWT / JFace
    Replies: 0
    Last Post: 07-23-2008, 04:15 PM
  3. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 04:19 AM
  4. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-06-2008, 12:01 AM
  5. Waste Space & Text Field
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2007, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •