The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Problem with append text to main class JTextArea from another class through thread.

I saw similiar posts but not solved, so i'm asking for help one's again.

This is part of my applet.

I have problem with append text from second class named TextOuterClassPut."tta.textArea.append("text from second class")" from class TextOuterClassPut is not working.
it should append this string to JTextArea in main frame.

"textArea.append" from class TextOuterClassPut is called by method "otherClass.test()"

public class TextOuterClassPut {
public TestTextArea tta = new TestTextArea();;
public TextOuterClassPut() {}
public void test()
{
// !!! this line below is not working properly !!!
tta.textArea.append( "text from second class" );
}
}

Re: Problem with append text to main class JTextArea from another class.

i'm sory i corrected main post. I hope its more clear now.
Generaly problem is with "tta.textArea.append("text from second class"); - it is not working properly.
it should be in JTextArea textArea in main frame and it is not.

Re: Problem with append text to main class JTextArea from another class.

You have several problems that I see, but most importantly is that yes, your code is in fact working in that you're appending text to a JTextArea that is held by TestTextArea object, but the problem you're having is that the TestTextArea is not displayed anywhere and nor can it be. It's a TestTextArea that was created for the sole purpose of doing your text manipulation, but since there's no way that it can be the displayed JApplet (you cannot display a JApplet that way), there's no way that you'll see any of the text that you're sending this object.

To solve this, I suggest that you pass a reference from the actual displayed applet into your outer class, perhaps in the outer class's constructor, and then call methods on this object, not some completely unrelated TestTextArea that again is not the one being displayed.

Other problems:

you're calling Swing methods from within a SwingWorker's doInBackground() method, something you never should be doing, nor would you ever have a reason for doing this. The doInBackground() method is for doing long-running non-Swing tasks. If you need to make Swing calls while this is running, use the publish/process method pair, or add a PropertyChangeListener and listen for changes to some bound property.

You're trying to fiddle directly with the field of another class. This breaks the OOP rule of encapsulation. Instead only call public methods on other objects.