File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Text File to ArrayList for JTextArea?" Watch "Text File to ArrayList for JTextArea?" New topic
Author

Text File to ArrayList for JTextArea?

Rico Jackson
Greenhorn

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Text File to ArrayList for JTextArea?
 
Similar Threads
Networked Programs that work across countries
How to activate threads in Swings.
Problem adding JComboBox to JFrame
adding and manipulating elements of a CCombo
How to set Look and Feel ?