File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes appending message in TextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "appending message in TextArea" Watch "appending message in TextArea" New topic

appending message in TextArea

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 58

Class A contains a text area:

I have an message that needs to be displayed in that text area, but it is generated by code in Class B. I got an error message that I cannot use a non-static variable in a static context, so I created an instance of Class A and its text area within Class B :

A button triggers the cancel() function in Class B. I know that works because the system will print out the message, but the text area will not. What am I missing?

thank you!
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Are you sure you can compile this? What's the event in your method, I can't see you declared it and what is the purpose of that cancel() method on event object?

I suppose that A reference in your B class is not the frame you are actually displaying to the user and the one that contains the button handling the event. So what you're actually doing is just create new A frame, do something with it, but since it's not the one that is displayed you can't see the result of the action. It's a variable local to that method in B class.

Why don't you declare method in class B to return a String (message that's to be appended to your JTextArea)? Invoke that method on object of B class from event handler in class A to get the message as String and append it where needed.

The quieter you are, the more you are able to hear.
I agree. Here's the link:
subject: appending message in TextArea
It's not a secret anymore!