Results 1 to 4 of 4
- 07-06-2010, 08:29 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 23
- Rep Power
- 0
writing to text Area through Class
Hey
i am reading a text file and all the functions that do this are in a class and i want to write this text to a text area. this is my code -
public void FileShow()
{
try {
BufferedReader brU = new BufferedReader(new FileReader("/Desktop/read.txt"));
do {
textAreaUsers.setText(brU.readLine());
} while (brU.read() != -1);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "IOException");
}
}
and the text area is in the jframe
Thanks
- 07-06-2010, 11:37 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
You are setting the text in your JTextArea to the most recently read line from your file; therefore you end up with the last line read. Also what is that brU.read() call doing there? It is snatching away single characters from every line except the first line. Read the API for the JTextArea class and see how you can append text to it. Also change your control flow because as it is now it is incorrect.
kind regards,
Jos
- 07-06-2010, 11:51 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 23
- Rep Power
- 0
here is the code now, i changed it because i figured out it wasn't appending but i still can't get my class to write text to textArea, putting it in the main class works but i want a separate class for it
try {
String lineUser;
BufferedReader brU = new BufferedReader(new FileReader("/Desktop/read.txt"));
while ((lineUser = brU.readLine()) != null) {
textAreaUsers.append(lineUser + System.getProperty("line.separator"));
}
brU.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "IOException");
}
- 07-06-2010, 12:11 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
So (an object of) your other class should at least have a reference to your JTextArea, otherwise it doesn't know where to put the text it has been reading. Maybe you can pass that JTextArea as a parameter to a constructor of your other class or maybe you can pass it as a parameter to the method that actually does the reading. It's all just programming, there's no magic involved.
kind regards,
Jos
Similar Threads
-
under line text area data
By anilkumar_vist in forum New To JavaReplies: 1Last Post: 02-25-2010, 03:37 PM -
Text Area Outline
By Psyclone in forum AWT / SwingReplies: 4Last Post: 02-07-2010, 06:12 AM -
eol in text area....
By Nicholas Jordan in forum NetworkingReplies: 0Last Post: 09-14-2008, 10:59 PM -
Text Area problem
By mcal in forum New To JavaReplies: 0Last Post: 02-11-2008, 09:42 PM -
textfiled and text area to UTF-16?
By Mr tuition in forum AWT / SwingReplies: 0Last Post: 12-04-2007, 12:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks