permaculture playing cards
The moose likes Beginning Java and the fly likes Text File to ArrayList for JTextArea? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Text File to ArrayList for JTextArea?" Watch "Text File to ArrayList for JTextArea?" New topic

Text File to ArrayList for JTextArea?

Rico Jackson

Joined: Apr 07, 2011
Posts: 2
I'm trying to display some text in a JTextArea I have set up in a simple GUI. Easy right? That's what I thought, but I'm clearly not that advanced at Java. The hook is the information I want to display in the JTextArea is in a text file (file.txt).

Right now I have a main class and a class called TextArea that houses the GUI and two methods. In the main class I have set up a simple array with some text in it, and I'm able to get it to display in the JTextArea. Basically the code all works fine, the only thing I can't figure out and need to change is have the information in the array coming from a text file. So if anyone can help me figure this out I'd really appreciate it. And if there is another way to get a textfile to appear in JTextArea without putting it into an ArrayList first I'd love to know.

Here's my code for the Main class

And here's the code for the TextArea Class

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The only important line of code is: textArea.setText(show);
That sets the text the TextArea shows. So if you want to show it the content of a file, you'll need to read the content of the file and pass its content to this method. If you don't know how to read from a file read this (although it plays a big role, ignore the Path class for now. It is part of Java 7).

Edit: And welcome to the Javaranch.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rico Jackson

Joined: Apr 07, 2011
Posts: 2
I've pretty much figured out all of the major issues I was having trouble with, thanks to the help from the people in these forums(thanks!). The info from my text file is displaying in the JTextArea but for some reason I can only get the last line of the file to display. There are 10 lines in the text file that I'm trying to display.

Here's my updated code:

I agree. Here's the link:
subject: Text File to ArrayList for JTextArea?
It's not a secret anymore!