Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2009, 03:52 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
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”);
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-10-2009, 03:58 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-10-2009, 04:25 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
Default
how can i reference the instance of A shown on screen
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-10-2009, 04:29 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-10-2009, 04:32 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
Default
could you please show me a code example
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-10-2009, 04:36 PM
Member
 
Join Date: Dec 2009
Location: germany
Posts: 15
Rep Power: 0
LittleRave is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-10-2009, 04:36 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-10-2009, 04:40 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
Default
@LittleRave: that is far from elegant!

Being Scott Densmore : Why Singletons are Evil
Singletons are Evil – Renaissance Developer
__________________
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-10-2009, 04:43 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
Default
thank you very much. i will just implement it immediately
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-10-2009, 04:57 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
Default
i have my main method in A, so do have to instanciate C in A
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-10-2009, 05:02 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
Default
No, if your main is in A it should look like this:
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-10-2009, 05:52 PM
Member
 
Join Date: Dec 2009
Posts: 8
Rep Power: 0
chikaman is on a distinguished road
Default
thanks to all of u. you guys are great. i just did it using PhHein's method
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-10-2009, 08:01 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
PhHein is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-11-2009, 09:43 AM
Member
 
Join Date: Dec 2009
Location: germany
Posts: 15
Rep Power: 0
LittleRave is on a distinguished road
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*.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
append variables to a text file ddatta8 New To Java 2 01-02-2009 11:17 AM
java DOM: append text to node newbieal New To Java 2 10-10-2008 08:18 PM
Need Help showing text in JTextArea GuyFawkes AWT / Swing 3 05-05-2008 10:19 AM
Append text inputed in the textfield into a TextArea romina AWT / Swing 1 08-07-2007 06:35 AM


All times are GMT +2. The time now is 05:16 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org