wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes How can i check for empty JTextfield and get one line at a time from a JTextArea? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can i check for empty JTextfield and get one line at a time from a JTextArea?" Watch "How can i check for empty JTextfield and get one line at a time from a JTextArea?" New topic
Author

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

Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
I have a JTextField filename = new JTextField(800,20);


......


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jarred Olson
Ranch Hand

Joined: Jul 31, 2009
Posts: 37
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

Joined: Aug 18, 2009
Posts: 43
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

This sounds like a job for String tokenizer !!


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i check for empty JTextfield and get one line at a time from a JTextArea?
 
Similar Threads
Static variables and inheritance
ImageIcon not working
how to accept strings and values in invoice textfields
save text file problem help !
Input/Output Question...