This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JTextArea append() not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTextArea append() not working?" Watch "JTextArea append() not working?" New topic
Author

JTextArea append() not working?

Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
So I'm planning to make a level editor for a game and to do that I must be able to read and save files. Now I'm making a program that opens a textfile and shows the text in a JTextArea but the append() method doesn't work where it's supposed to. It seems it stops working after I opened a dialog with filechooser. So what's wrong?

Here's the code:

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Each time you call createContentPane(), a new JTextArea is created and the member variable "log" set to point to it. The original one is still on the screen, but "log" isn't referring to it anymore. You're then appending to another JTextArea that isn't visible!

I don't understand why you're calling this method repeatedly, when you've got a member variable to refer to everything else; just store the return value the very first time you call it into a member, and use that member everywhere else.


[Jess in Action][AskingGoodQuestions]
Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
Sweet it's working now, thx alot

Now I've been trying to make it able to save the text. I wrote some code but it doesn't seem to work. It saves a file but it's empty. If I load a file and save it without changing anything it works.

Here is the code:


I also tried using PrintWriters methods print(string) and write(string) but they didn't work either.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18710
    
    8

You didn't close the writer after you finished using it. Since writing to disk files happens in chunks of several K bytes at a time (because that's how the operating system deals with files), you're losing the last chunk. Which is probably the whole thing. Close the writer in a finally block like this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextArea append() not working?