Results 1 to 5 of 5
  1. #1
    nimishalex is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default Jtextfield Validation

    Hi all

    In my Swing application i am using Jtextfields.If Jtextfield have empty string("") i need to set it to 0 always .Is there any easy way to set this without if else comparison.

    Thanks

    Nimish

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I would suggest,
    Java Code:
    textField.setText("0");
    Why you dont want to use if else condition? Can you use other conditional statement?

    If you want to set text to JTextfields that has empty string then you will need to use conditional statement.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Have you thought about what you would want to happen when the JTextField (NOT Jtextfield) displays 0 and the user types a character?

    To answer your question, use an appropriately coded DocumentFilter. Override remove(...) and replace(...) to call the super implementation, check whether the JTextField's text isEmpty() and conditionally call insert(...) to insert "0"

    db

  4. #4
    nimishalex is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks to all for the quick reply

    I am newbie in Java Programming. Its fine to use conditional Statement. I thought some other way to implement this without the conditional statement.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

Similar Threads

  1. Which one is better for navigation and validation
    By anilkumar_vist in forum New To Java
    Replies: 1
    Last Post: 10-18-2010, 05:21 AM
  2. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  3. VAlidation
    By chetna1982 in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 07:08 AM
  4. Need Help On Entry Validation
    By obdi in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 06:39 AM
  5. form validation
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 03-07-2008, 07:04 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
  •