Question

This is my FrameGUI

public FrameGUI(String title) {
        super(title);
        setLayout(new BorderLayout());

        final JTextArea textArea = new JTextArea();  //  *****
        detailsPanel = new DetailsPanel();
        Container c = getContentPane();
        c.add(textArea, BorderLayout.CENTER);
        c.add(detailsPanel, BorderLayout.NORTH);
    }

i want to update this textArea from other classes. right now im outputting to the console, but i want to append the text area insted of this. i can append this easily in this class by adding a button and an event handler but i want to do it from another class where different processes are happening.

Thanks for the help.

Was it helpful?

Solution

Your question is not Swing or GUI specific, but is part of the more the general Java question of:

How can an object of one class change the state of a field of an object of another class?

One way to do this is with setter methods. Fore example, you can give the class that holds the JTextArea a public method that will give other classes the ability to do this.

For example,

// assuming the class holds a descriptionArea 
// JTextArea field
public void appendToDescriptionArea(String text) {
  descriptionArea.append(text);
}

This way, the JTextArea field can remain private, but still other classes that hold a valid reference to the displayed GUI that holds the field can call this method and update the JTextArea's text. Note that a common mistake is to give the class that wishes to add text a new and completely unique reference to the class that holds the JTextArea, but if you did this, you would be setting the text of a GUI that is not displayed. So be sure that you call this method on the correct visualized instance.


If this answer doesn't help you solve your issue, then consider posting more information about the classes involved including pertinent code and background information. The more specific and useful information that you can get to us, usually the more specific and helpful the answers we can get to you.


Edit
Regarding this error:

"textArea can not be resolved"

And this code:

public FrameGUI(String title) {
  super(title);
  setLayout(new BorderLayout());

  final JTextArea textArea = new JTextArea();  //  *****
  detailsPanel = new DetailsPanel();
  Container c = getContentPane();
  c.add(textArea, BorderLayout.CENTER);
  c.add(detailsPanel, BorderLayout.NORTH);
}

Your problem here is that you are declaring your textArea variable inside of the FrameGUI's constructor, and in doing this, the variable's visibility or "scope" is limited to just this constructor. Outside of the constructor it doesn't exist and can't be used.

A solution is to declare the textArea variable outside of the constructor, to make it a field of the class. e.g.,:

public class FrameGUI extends JFrame { // I'm not a fan of extending JFrames.
    // this variable is now visible throughout the class    
    private JTextArea textArea = new JTextArea(15, 40); // 15 rows, 40 columns

    public FrameGUI(String title) {
      super(title);
      setLayout(new BorderLayout());

      // final JTextArea textArea = new JTextArea();  //  *** commented out
      detailsPanel = new DetailsPanel();

      Container c = getContentPane();
      c.add(new JScrollPane(textArea), BorderLayout.CENTER); // put textarea into a scrollpane
      c.add(detailsPanel, BorderLayout.NORTH);
    }

    public void appendToTextArea(String text) {
      textArea.append(text);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top