Results 1 to 14 of 14
  1. #1
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default How can I append text in JTextArea from another class

    i have two classes A and B, i defined a TextArea myTextArea in A and try to append text to it from B but it is not showing anything in the textArea. below i have some parts of the program. Some one help me please.
    Class A

    Class A extends javax.swing.JFrame{
    private JTextArea myTextArea;
    A( ){
    myTextArea = new JtextArea;
    }
    public void setArea(String mess){
    myTextArea.Append(mess);
    }
    }

    Class B
    Class B{
    A newA;
    B( ){
    newA = new A;
    }
    //i have this line below which is supposed to append text in //myTextArea in class A
    newA.setArea(“message”);
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    newA references a different instance of A. You need a reference to the instance of A that is shown on screen.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    how can i reference the instance of A shown on screen

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Either you pass the reference to B or you show the one in B.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    could you please show me a code example

  6. #6
    LittleRave is offline Member
    Join Date
    Dec 2009
    Location
    germany
    Posts
    15
    Rep Power
    0

    Default

    Either write a constructor like this:

    B(A newA) {
    this.newA = newA;
    }

    But this is kind of stupid to call and always put the A class into the constructor.

    Or do it the elegant way with a singleton and write your classes like that:

    Class A extends javax.swing.JFrame{
    private static final A INSTANCE = null;

    public static void A getInstance() {
    if (INSTANCE == null) {
    INSTANCE = new A();
    }
    return A;
    }

    private A() {
    myTextArea = new JtextArea;
    }

    public void setArea(String mess){
    myTextArea.Append(mess);
    }
    }

    Class B
    Class B{
    A newA;
    B( ){
    newA = A.getInstance();
    }
    //i have this line below which is supposed to append text in //myTextArea in class A
    newA.setArea(“message”);
    }

    With this solution you always get the A class you entered the text into no matter where in your program you call the A.getInstance().
    But only use this if you need one object of the A class in the whole program, you CANNOT create a second one. ;)

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Java Code:
    public class C{
     A a = new A();
     B b = null;
     public C(){
       b = new B(a);
       a.setVisible(true);
     }
    }
    
    
    public class B{
     A newA;
     B(A a){
      newA =a;
     }
     public void doSomething(){
      newA.setArea("BlaBla")
     }
    
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    thank you very much. i will just implement it immediately

  10. #10
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    i have my main method in A, so do have to instanciate C in A

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    No, if your main is in A it should look like this:
    Java Code:
    public class A{
       B b = null;
       public static void main(String agrs[]){
        A a = new A();
        b = new B(a);
        a.setVisible(true);
       }
    }
    B stays as it was in my last post.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    chikaman is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    thanks to all of u. you guys are great. i just did it using PhHein's method

  13. #13
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Cool, so did you understand what was going wrong when you first tried?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  14. #14
    LittleRave is offline Member
    Join Date
    Dec 2009
    Location
    germany
    Posts
    15
    Rep Power
    0

    Default

    lol ... It depends on what you're trying to achieve and how you use it.
    I recently wrote a multiplayer game with some of my fellow-students and I needed about 6 classes only for the keyboard input, but since every class was connected to another i had to pass around lots of ugly data in every single constructor, plus I couldn't get a single interface, I had to create the classes hiding behind the interface somewhere outside and pass it inside. The singleton helped with all that and since there's NEVER a second keyboard instance used, it was a lot more elegant than the normal way with passing the objects around.

    But you're right, in this case it would be better to use the normal way with a constructor passing the reference. Especially since its only two classes with the main in one of it. Since our softwareproject is finished I always tend to think way too complex *lol*.

Similar Threads

  1. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 11:17 AM
  2. java DOM: append text to node
    By newbieal in forum New To Java
    Replies: 2
    Last Post: 10-10-2008, 08:18 PM
  3. Need Help showing text in JTextArea
    By GuyFawkes in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 10:19 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •