Search code examples
javastringswingjtextarea

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.


Solution

  • 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