Aaron
Aaron

Reputation: 1016

JTextArea Storing Each Line

I'm making an Address Book application, and I have the basic fields (name, age, phone numbers, etc.) and I have a JTextArea for placing miscellaneous comments, since I won't know how much info I need to add. It could be one line or hundreds.

I want to know if there is a way to store this data and place it back into the field when the contacts information is loaded. I could store the entire JTextArea as a String, but would that keep the correct formatting? Or save many Strings into an ArrayList or something similar.

Additionally, is there a method to save blocks of text as opposed to single lines? For example. I might want to store an URL, or I might want to store a 5-line poem the contact wrote. Obviously, storing the URL could take one line, but the poem would take 5 lines and therefore 5 Strings.

What seems like a decant storage solution? If my concept doesn't make sense, please comment and I'll try to make it more clear.

Upvotes: 0

Views: 501

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

Saving it as a single String should fine, it will, normally, preserve newlines as well.

This will of course, depend on where you are storing it

Upvotes: 1

leigero
leigero

Reputation: 3283

It sounds to me like your address book app is asking a user to input simple values as you suggested and I'm assuming it is saving these values into a "Contact" object or something similar.

If this is the case, you can simply add a String comments; to the Contacts class and implement a getter and setter method accordingly.

Just get the data from the JTextField and store it into that Contact's comments variable.

Contact person = new Contact();
person.setComments(textArea.getText());

Something like that. Then you can reference the person.getComments() later on and print that back out to the TextField if thats what you want to do with it.

Upvotes: 0

Related Questions