File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTextArea append() not working?" Watch "JTextArea append() not working?" New topic

JTextArea append() not working?

Jon Mattson

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

Joined: Jul 08, 2003
Posts: 24199

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

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

Joined: Oct 14, 2005
Posts: 19973

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:
subject: JTextArea append() not working?
It's not a secret anymore!