Results 1 to 12 of 12
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    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 !

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.)

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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 :)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome. (but nothing has been said anywhere about subclasses...)

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Oops!
    I thought those were subclasses, I now see that they are not, my mistake!

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

    Java Code:
    class b{
      public String doSomething()
    }
    
    class a{
      public void anotherMethod(){
        String s = doSomething();
        label.setText(s);
      }
    }
    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.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @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.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  12. #12
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    No worries :)

    I wanted to know this basically so that I can have a thread class and make that have some sort of timer in another class...
    The way you described it is how I usually use my helper classes.

Similar Threads

  1. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 04:44 AM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 05:29 AM
  3. setText in JTextArea
    By hero in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 09:38 PM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 PM
  5. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 02:56 PM

Posting Permissions

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