Results 1 to 8 of 8
  1. #1
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default How to make JTextField input only allowed numbers ?

    Okay, I'm a beginner for Java things.
    I have a homework to make login program.
    The problem is, there's a Label named "Telephone Number" and what confuses me is how to make the JTextField only accept number inputs?
    Thanks

  2. #2
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    u might be having a button called "login" in ur GUI.

    Attach actionlistener to that button by using

    "buttonName.addActionListener()

    and in the actionPerformed method write the simple code to check whether the text box has a number or not .. if not generate pop-up to show error msg.

    so simple...

    still have confusion post or efforts..(codes)

  3. #3
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    Hmm, I've already using the actionlistener.
    But what I'm asking is, I just want the field to accept only inputs from numbers (String like alphabet can't). How to do that? cause now if I input with alphabet the field still accept it.

  4. #4
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    you want to check it at the very time when u enter data in textfield OR

    u want to check it wen u click on login button.?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BluXit View Post
    Okay, I'm a beginner for Java things.
    I have a homework to make login program.
    The problem is, there's a Label named "Telephone Number" and what confuses me is how to make the JTextField only accept number inputs?
    Thanks
    Read the API documentation for the JFormattedTextField (a subclass of JTextField).

    kind regards,

    Jos

  6. #6
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    @programmer_007
    Check when I click Login Button.

    @JosAH
    Where can I find API Documentation?

    Thanks to both of you.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BluXit View Post
    Where can I find API Documentation?
    Here it is,bookmark it and never program without it.

    kind regards,

    Jos

  8. #8
    BluXit is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 02:15 AM
  2. Convert Input from JTextField to int?
    By jls7168 in forum New To Java
    Replies: 7
    Last Post: 02-20-2009, 02:29 AM
  3. JTextField add numbers advice
    By thelinuxguy in forum Advanced Java
    Replies: 9
    Last Post: 02-20-2009, 02:03 AM
  4. Constraining Input in JTextField
    By kataya in forum AWT / Swing
    Replies: 1
    Last Post: 06-26-2008, 06:25 PM
  5. Replies: 2
    Last Post: 03-30-2008, 08:38 PM

Posting Permissions

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