Welcome to the Java Programming Forums

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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Regarding working with different objects and passing information

  1. #1
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    My Mood
    Thanked 140 Times in 135 Posts

    Default Regarding working with different objects and passing information

    I received this private message from delta2w today, and suggested that he post it in the general forum, but since he has yet to do so, I've taken the liberty of posting it for him:

    Quote Originally Posted by delta2w
    I read your post today regarding updating a JTextArea from another class. I am having a similar issue but being very recent to JAVA I didn't fully know how to implement your answer and hoped you wouldn't mind explaining it to me in simple terms.

    What I am trying to do is as follows:
    I am working in NetBeans and I have two classes, a 'MyGui' class with a JFrame with a number of JTextAreas, JLists etc and a second 'updater' class. I wish to be able to set/append the text in any of the JTextAreas, JLists etc from the updater and other future classes. Both classes are in the same package and access is set to default for all textareas.

    I have created a setText(String str) method for JTextArea1 in the MyGui class which takes a String param and uses JTextArea1.setText(str); and has a message popup and have called this from the updater class using:
    MyGui gui = new MyGui();
    gui.setText("Test Text");
    but the text does not appear although the message does. I feel that this is likely due the same reason you mentioned and I am possibly creating a new instance of MyGui which is not visible.

    How do I pass a reference to the constructor to enable this to work for all of the JTextAreas and JLists? The current MyGui constructor simply contains initComponents();

    I also read that it might be due to the EventDispatchThread and require swingutilities.invokeLater. I have tried this but get the same results with just the popup but no change in the textarea text.

    I really appreciate any help/advice you could offer.
    With Thanks

  2. #2
    Junior Member
    Join Date
    Oct 2012
    Thanked 1 Time in 1 Post

    Default Re: Regarding working with different objects and passing information

    Thanks. I solved it simply by adding a second constructor to updater class as follows:
    public Updater(MyGui gui) {
    this.gui = gui;
    Then passed a reference to the original MyGui instance when calling a method in the updater class to update the text
    Updater updater = new Updater(MyGui);
    updater.setNewText("new text");
    Works as needed.

  3. The Following User Says Thank You to delta2w For This Useful Post:

    curmudgeon (October 19th, 2012)

Similar Threads

  1. passing java objects to ActiveMQ(JMS)
    By manju in forum Member Introductions
    Replies: 1
    Last Post: April 19th, 2012, 05:55 AM
  2. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 03:19 PM
  3. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 10:04 AM
  4. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 03:40 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM