Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Junky

Thread: Checking to see if a JTextField is empty.

  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Checking to see if a JTextField is empty.

    Ok I'm trying to check whether or not a JTextField is empty, mainly because I'm trying to make it so if you don't enter any information in the TextField it ignores it from my formula.

    However after scouring google and anything I can think of in the API I have come up with nothing. I also tried
    Java Code:
    lower.getText() != ""
    and
    Java Code:
    lower.getText() != null
    Is there a way to check and see if anything is entered into a JTextField?

    Edit: I'm an idiot. I just used .equals to solve that problem. I thought of that as soon as I hit post. :facepalm:
    Last edited by Dark; 06-23-2011 at 07:06 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    getText().trim().length() == 0
    getText().trim().equals("")

    use the equals method for String/Object equality

    A Textfield will not return null, as the API docs state

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

    Default

    Java Code:
    // Number 3:
    getText().trim().isEmpty();

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    getText() with an empty JTextField will return an empty string, so getText().equals(""); works. I got it to work and now my project is done.

    Curious though, why are you suggesting trim()?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Curious though, why are you suggesting trim()?
    It gets rid of leading/trailing spaces.

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

    Default

    Quote Originally Posted by Dark View Post
    getText() with an empty JTextField will return an empty string, so getText().equals(""); works. I got it to work and now my project is done.

    Curious though, why are you suggesting trim()?
    Because an idiotic user might've fallen asleep with his nose on the space bar and when he woke up he accidentally pressed enter ...

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Jos speaks from experience.
    Dark likes this.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well my project accepts spaces as a legal character, so trimming it would limit the functionality. I see what you mean though, if I had a name field then trimming it would be a good idea.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Fubarable View Post
    Java Code:
    // Number 3:
    getText().trim().isEmpty();
    Since 1.6, unfortunately, our project still uses 1.5

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

    Default

    Quote Originally Posted by Junky View Post
    Jos speaks from experience.
    Am not! I never fall asleep in front of my keyboard; I always fall asleep (read: I do deep thinking there) on my old couch. I hate to sit in front of a computer; it's like sitting in front of a washing machine, that's stupid too.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    If I must fall asleep at the keyboard, I prefer to rest my nose on the zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzz

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Is the z key more comfortable?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default

    Quote Originally Posted by Dark View Post
    Is the z key more comfortable?
    No it's not; as a matter of fact typing with your nose is nearly impossible; example:

    oim gtytopibgh thiosd wweigttth myuy nbioose

    kind regards,

    Jos (I wonder if I can type with my ... *beep* ;-)
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well I understood what you were trying to type, maybe with a little practice you'll become a pro.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default

    Quote Originally Posted by Dark View Post
    Well I understood what you were trying to type, maybe with a little practice you'll become a pro.
    I prefer the piano over the keyboard ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    And really, Jos, any discussion about the weight of your caboose is OT here. Let's stick to the topic, shall weeeeeeeeeeeeeeeee

    db

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

    Default

    Quote Originally Posted by DarrylBurke View Post
    And really, Jos, any discussion about the weight of your caboose is OT here. Let's stick to the topic, shall weeeeeeeeeeeeeeeee
    There's nothing wrong with my caboose; as a matter of fact its weight is fine and I have the fundamental right to show it whereever and whenever I want; so there. I thought that the sub-title of this forum was "Java programmers and their cabooses" so I don't understand your remark about being off-topic either.

    kindest regards,

    Jos (and his caboose)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Empty FileInputStream..
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 08:21 PM
  2. Why is this stakc empty?
    By jigglywiggly in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 06:22 AM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM
  5. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 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
  •