Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can i check for empty JTextfield and get one line at a time from a JTextArea?

 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTextField filename = new JTextField(800,20);


......


 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.trim() never returns null. Neither does JTextField.getText(). Therefore, the check against null can be removed.

Never ever use == for String comparison. Strings are objects, so you are comparing object references. Use equals.
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably make a StringUtils class that has a isNullOrEmpty(String str) { } method. If you're going to be doing a lot of String validation you'll use it a lot. Then you could replace lines 5 through 9 with:

 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob - Thanks for the tip on not using == and using equals instead.

Jarred - Thanks for the idea but i am not sure i need to import another class. It's good to know in case i might need it.

But can someone help me with the textarea problem? How would you separate one line at a time? Should i use indexOf but doesn't that use only characters and not code for '\n'.

I appreciate any tips to help me learn and i can help others if i can.
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTextArea class does have a getLineCount() method. So in your original post, you declare "content" to be a JTextArea variable in the first code fragment, but in the second code fragment you must have declared "content" as a different variable of a different type.
 
salvin francis
Bartender
Posts: 1305
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a job for String tokenizer !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic