Results 1 to 17 of 17
- 06-23-2011, 07:01 PM #1
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 triedandJava Code:lower.getText() != ""
Is there a way to check and see if anything is entered into a JTextField?Java Code:lower.getText() != null
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.
- 06-23-2011, 08:27 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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
-
Java Code:// Number 3: getText().trim().isEmpty();
- 06-24-2011, 07:29 AM #4
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.
- 06-24-2011, 07:46 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
It gets rid of leading/trailing spaces.Curious though, why are you suggesting trim()?
- 06-24-2011, 07:48 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 06-24-2011, 08:01 AM #7
Jos speaks from experience.
- 06-24-2011, 08:12 AM #8
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.
- 06-24-2011, 10:12 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 06-24-2011, 10:38 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-24-2011, 10:49 AM #11
If I must fall asleep at the keyboard, I prefer to rest my nose on the zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzz
- 06-24-2011, 10:51 AM #12
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.
- 06-24-2011, 11:02 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-24-2011, 11:26 AM #14
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.
- 06-24-2011, 12:50 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
I prefer the piano over the keyboard ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-24-2011, 12:55 PM #16
And really, Jos, any discussion about the weight of your caboose is OT here. Let's stick to the topic, shall weeeeeeeeeeeeeeeee
db
- 06-24-2011, 01:36 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
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)When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Empty FileInputStream..
By dudejonne in forum New To JavaReplies: 5Last Post: 11-08-2009, 08:21 PM -
Why is this stakc empty?
By jigglywiggly in forum New To JavaReplies: 1Last Post: 10-09-2009, 06:22 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
KeyBindings Empty Key?
By Unome in forum Java AppletsReplies: 1Last Post: 10-24-2008, 07:28 PM -
Empty ResultSet
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:36 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks