Results 1 to 14 of 14
- 12-10-2009, 02:52 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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”);
}
- 12-10-2009, 02:58 PM #2
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
- 12-10-2009, 03:25 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
how can i reference the instance of A shown on screen
- 12-10-2009, 03:29 PM #4
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
- 12-10-2009, 03:32 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
could you please show me a code example
- 12-10-2009, 03:36 PM #6
Member
- Join Date
- Dec 2009
- Location
- germany
- Posts
- 15
- Rep Power
- 0
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. ;)
- 12-10-2009, 03:36 PM #7
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
- 12-10-2009, 03:40 PM #8
@LittleRave: that is far from elegant!
Being Scott Densmore : Why Singletons are Evil
Singletons are Evil – Renaissance DeveloperMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-10-2009, 03:43 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
thank you very much. i will just implement it immediately
- 12-10-2009, 03:57 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
i have my main method in A, so do have to instanciate C in A
- 12-10-2009, 04:02 PM #11
No, if your main is in A it should look like this:
B stays as it was in my last post.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); } }Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-10-2009, 04:52 PM #12
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
thanks to all of u. you guys are great. i just did it using PhHein's method
- 12-10-2009, 07:01 PM #13
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
- 12-11-2009, 08:43 AM #14
Member
- Join Date
- Dec 2009
- Location
- germany
- Posts
- 15
- Rep Power
- 0
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
-
append variables to a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-02-2009, 10:17 AM -
java DOM: append text to node
By newbieal in forum New To JavaReplies: 2Last Post: 10-10-2008, 07:18 PM -
Need Help showing text in JTextArea
By GuyFawkes in forum AWT / SwingReplies: 3Last Post: 05-05-2008, 09:19 AM -
Append text inputed in the textfield into a TextArea
By romina in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks