Results 1 to 12 of 12
- 02-18-2011, 11:50 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Calling another form's label.setText
Hi,
I have 3 java programs: a, b and c
"a" is a form
"b" is where i have all sorts of methods which help "a"
"c" is where I have main()
I use a lot of methods from "b" by making an object of "b" in "a"
usually some method in "b" will do some calculation and return something to "a", but instead of it just returning something is it possible for "b" to do a setText() on a JLabel in "a"?
(Sorry for the confusing explanation, its really quite simple when you see it side by side but a bit hard to write down)
Thanks!
R
- 02-18-2011, 11:55 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
b needs a reference to a, so you have to pass the object of a to the constructor of b
e.g. new b(this) // (if you call this from your a class) or write a setter method to set a to b :)
and you have to write some methods in a e.g. to set the labels text !
- 02-18-2011, 11:59 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
When you construct your b actually pass a reference to the form. The reference will probably be this.
You could give b's class a constructor that takes such a reference as an argument. Then when b does the calculation it will know the form whose label text has to be set.
Or you could pass this as an argument when you call the method that causes b to do the calculation.
(Notice how either approach "exposes" a part of the form's state - the label - to the outside world. It also makes testing b's class a little tricky because you will have to have a fully constructed form in order to test the helper class's calculations.)
- 02-19-2011, 12:12 AM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Couple of questions:
1.
So its not a problem that in "a" I have:
b bb= new b();
and in b I have:
a aa=new a();
?:confused:
---------------
2.
> and you have to write some methods in a e.g. to set the labels text !
Damn! I was hoping for something like this:
a aa=new a();
aa.JLabel.theLabel.setText("blah");
_____________
3. I'm a newbie so please excuse this question but you wrote:
> You could give b's class a constructor that takes such a reference as an argument.
in the constructor I would have to say what type (string, int etc) what would be the type if I were to do what you suggest?
///////////////////
4
> Notice how either approach "exposes" a part of the form's state - the label - to the outside world.
Not really... but I guess I will as I keep learning Java :)
- 02-19-2011, 12:30 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Regarding (3) I am not sure about your question, but the idea of giving the helper class a constructor that takes a form argument is common to both what I and eRaaa were saying.
In code it would look like this (I'll use more descriptive names for your classes and replace the label with a simple int so the whole thing can be run from the command line):
Java Code:class Form { private int label; public void doCalculation() { Helper helper = new Helper(this); helper.provideHelp(); } public void setLabel(int newValue) { label = newValue; } public int getLabel() { return label; } } class Helper { private Form form; // the form being helped // the helper is told what form it is helping in its // constructor Helper(Form form) { this.form = form; } void provideHelp() { form.setLabel(666); } } class Driver { public static void main(String[] args) { Form form = new Form(); System.out.println("The form's label is " + form.getLabel()); form.doCalculation(); System.out.println("The form's label is now " + form.getLabel()); } }
----------
Personally I like the other option you mention: have the helper just help. It is asked to do a caulculation, it does so and returns the result to the form. The form itself does what it wants to ensure that the label is suitably adjusted.
- 02-19-2011, 12:59 AM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Thanks for replying and the example!
I have not worked much with sub classes so I got to read up on the advantages of using subs, but this none the less helped.
Thanks again!
- 02-19-2011, 01:04 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You're welcome. (but nothing has been said anywhere about subclasses...)
- 02-19-2011, 01:26 AM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Oops!
I thought those were subclasses, I now see that they are not, my mistake!
- 02-19-2011, 02:57 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im not sure how all your code looks, and I am not sure how good this code idea is, but you can simply make the method in b that you want to change the label to simply return a string. Then when you call it in a, you can set a string to the return value of b's method, then set the label to this new string
Something like that should work for what you want. And I would hope people here would tell you whether this idea is viable or not.Java Code:class b{ public String doSomething() } class a{ public void anotherMethod(){ String s = doSomething(); label.setText(s); } }
- 02-19-2011, 03:12 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
@sunde - the OP had already considered doing it this way, but was asking whether the other approach was possible.
usually some method in "b" will do some calculation and return something to "a", but instead of it just returning something is it possible for "b" to do a setText() on a JLabel in "a"?Last edited by pbrockway2; 02-19-2011 at 03:17 AM.
- 02-19-2011, 03:19 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Oops, my apologies, I had thought he was simply asking how to set the text of the button from another class... I guess I am too tired to be posting at the moment.
- 02-19-2011, 03:34 AM #12
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
problem with JLabel.setText();
By nonabhai in forum AWT / SwingReplies: 5Last Post: 10-09-2010, 04:44 AM -
setText() problem
By Jozo in forum Java AppletsReplies: 4Last Post: 04-27-2010, 05:29 AM -
setText in JTextArea
By hero in forum AWT / SwingReplies: 1Last Post: 10-12-2009, 09:38 PM -
setText() problem
By jls7168 in forum New To JavaReplies: 2Last Post: 02-20-2009, 10:34 PM -
setText in event
By sniezna.stopa in forum SWT / JFaceReplies: 0Last Post: 06-20-2008, 02:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks