1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Ricky
  • 77
  • 1
  • 4
  • 14

1 Answers1

1

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);
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • ive tried adding the above code to my file, but now im getting "textArea can not be resolved" – Ricky Mar 15 '14 at 13:05
  • @Kal: then you are going to have to post relevant code in your original question, including how and where you declare the JTextArea, the method that you use to let other classes have the ability to append text, how you're getting the reference to class that holds the JTextArea in your second class, and how you try to call the above method. Also post any and all full error and exception messages. This additional information will help us to better understand your problem. Luck! – Hovercraft Full Of Eels Mar 15 '14 at 13:07
  • ok, i have updated with more of my code, hopefully my question makes sense now. thanks for your help btw. – Ricky Mar 15 '14 at 13:14
  • @Kal: you're welcome, and best of luck with your Java studies! – Hovercraft Full Of Eels Mar 15 '14 at 13:36
  • 1
    @Kal: See also this [answer](http://stackoverflow.com/a/3245805/230513) regarding correct synchronization. – trashgod Mar 15 '14 at 14:46