-1

I have designed a GUI which has a jTextArea. I can append text to this very easily in this class. But I would also like to append text to this from methods of other classes.

For this I make the jTextArea public and create an object of that class in the other class. Now I can write to the jTextArea while executing methods in the other classes.

But it turns out that I need a method of the class I created an object in within my GUI class as well. As I do that, StackOverflowError happens. What should I do?

AllLuckBased
  • 157
  • 1
  • 14

1 Answers1

1

Instead of initializing the BTDBattlesBot object with a new keyword in AutomatedActions class, you can add a parameterized constructor which takes BTDBattlesBot object as parameters and initialize the object in this constructor. Something like below

  public class AutomatedActions {
    ...............
   private final BTDBattlesBot botWindow = null;

   // Below is the parametrized constructor
   public AutomatedActions(BTDBattlesBot botWindow) {
   botWindow = this.botWindow;
  }

 ..........

}

Then in the BTDBattlesBot class, while creating the object of AutomatedActions class, instead of calling the default constructor, call the newly created parametrized constructor and pass the current object of BTDBattlesBot class using this keyword. By doing this you should be able to achieve what you want

class BTDBattlesBot {
......
AutomatedActions action = new AutomatedActions(this);
......
}
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41