Results 1 to 16 of 16
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default Changing GUI labels from another class

    I'm trying to get this GUI window to start a process and then have that process set the text on a label in the gui just to keep the user aware of what the system is currently doing.

    As a result I've been trying to set the text of a JLabel from another class.

    So far I have a method like this in the gui:
    Java Code:
    public void setLabel(String newValue)
    {
       label.setText(newValue);
    }

    The gui hands itself to the next class like this:

    Java Code:
    Run run = new Run();
    run.start(this, inputString);
    I then try to call the setLabel method from inside the run object:

    Java Code:
    gui.setLabel("Searching...");
    The problem with this setup is that it only seems to update the JLabel once the process is finished running. Why does this happen?
    Last edited by porchrat; 02-03-2011 at 02:57 PM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i just made a test with two frames: the first frame has a button called "Change Label" and the second frame has a method called changeLabel, which is called and changed each time i push the button "Change Label" from the first frame. all works fine. perhaps i can give more help when you post the code of your frame where the label is displayed.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Why don't you use a SwingWorker?
    I assume that Run is a Thread?
    What happens after the call to start() in GUI?

  4. #4
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Why don't you use a SwingWorker?
    I assume that Run is a Thread?
    What happens after the call to start() in GUI?
    Run isn't a Thread no. I'm not exactly clued up on how to use Threads. I maybe should try learning it they seem pretty useful.

    Well that run object actually launches another class (it's static) and all it does is take in a directory path and then run recursively through each child directory looking for files and putting them in a list until it has found them all. The whole process takes about 10 or 15 seconds. Pretty much the first line of code in that static Find class is: gui.setLabel("Finding progress message here");

    I just don't understand why it doesn't change the JLabel at that point instead of waiting for the entire class to run every method to completion before changing. I would've thought it would change the JLabel at that first line.

  5. #5
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    i just made a test with two frames: the first frame has a button called "Change Label" and the second frame has a method called changeLabel, which is called and changed each time i push the button "Change Label" from the first frame. all works fine. perhaps i can give more help when you post the code of your frame where the label is displayed.
    That is similar to how I tested it initially before I tried using it in this piece of code and I agree that that system works. The problem is that under those circumstances you are in fact running the entire method/class to completion anyway and it all happens far too quickly to determine at which point the JLabel changes so it is hardly the same thing.

    Maybe the various components of the GUI are locked because that Frame is what called the process?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by porchrat View Post
    Run isn't a Thread no.
    There's your problem.
    That means it's running in the EDT (Event Dispatch Thread) which is essentially the Swing thread. Until your code in Run finishes nothing else can use that thread, meaning your GUI locks up.

    When you call setText() on the label it calls a repaint() to say it needs to be redisplayed and this gets queued up. It will only actually do the repaint when the EDT becomes available to do the work, which won't be until after your Run code finishes.

    So use a SwingWorker. Here's a good tutorial on it.

    Here's a full tutorial on concurrency in Swing, from which the above page is taken.

  7. #7
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    There's your problem.
    That means it's running in the EDT (Event Dispatch Thread) which is essentially the Swing thread. Until your code in Run finishes nothing else can use that thread, meaning your GUI locks up.

    When you call setText() on the label it calls a repaint() to say it needs to be redisplayed and this gets queued up. It will only actually do the repaint when the EDT becomes available to do the work, which won't be until after your Run code finishes.

    So use a SwingWorker. Here's a good tutorial on it.

    Here's a full tutorial on concurrency in Swing, from which the above page is taken.
    Ah thank you that looks very promising.

    What you're saying makes perfect sense because I have noticed that all the buttons stop responding as well so it logically follows that I wouldn't be able to repaint the labels either.

    You're saying that the flow of control has moved to the new class and thus no changes can be made to the Frame until control passes back to it?

    Thanks again. I'll take a read through what you've posted it looks very promising. :D

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Each thread is a single flow of commands. It can't do multiple things at once, which is why we have multi-threading. So your code is going like this:

    Create GUI
    |
    Run
    |
    setText()
    |
    more Run stuff
    |
    setText()
    |
    finish Run
    |
    Finally act on the repaint() requests from those setText() calls.

    Whereas with a multi thread:
    Java Code:
    EDT Thread     Worker
    Create GUI
    |
    Start Run ->    run()
                         |
    redraw()         <-  setText() 
                         |
                         some other processing
                         |
    redraw()         <-  setText()
    etc etc.
    (Here's hoping that looks right)

  9. #9
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    LOL yes it looks right.

    Either that or I can make another frame in a separate thread and have the EDT Thread repaint that one instead.

    Either way I can see now I'm going to need to separate the objects into separate threads.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    If you do that short Swing Worker tutorial you should have enough ammunition to do it in your code.

  11. #11
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    If you do that short Swing Worker tutorial you should have enough ammunition to do it in your code.
    I think I'm probably going to make a Frame that contains that JLabel in a separate thread instead of having to rewrite large portions of code that I already know work perfectly. It just makes more sense the more I think about it.

    Regardless I would not be able to do it without the tutorial you have linked me to. I'm running through it already. Thank you again for the help and the easy to follow explanations :)

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    It won't matter.
    The repaint() still uses the EDT.
    All Swing stuff uses the EDT. So unless you move your Run code into a new thread then it still won't display.

  13. #13
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    It won't matter.
    The repaint() still uses the EDT.
    All Swing stuff uses the EDT. So unless you move your Run code into a new thread then it still won't display.
    Oh great. :(

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Everyone who works with Swing goes through this stage...:)

    Once you've done your first Worker you'll never look back.

  15. #15
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Everyone who works with Swing goes through this stage...:)

    Once you've done your first Worker you'll never look back.
    LOL thanks for the vote of encouragement.

    I'll get it working. I haven't encountered anything yet that has been able to stump me if I keep hammering at it long enough. At least now I have identified the problem and I have the solution. I just need to implement it. :D

    Anyway I will leave the thread open in case I have any issues and so that I can let those that have followed it know how it turned out.

  16. #16
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    OK I figured it out.

    These SwingWorker extensions are pretty easy to use actually. Just run execute() and put some logic into doInBackground() and you're ready to go. All my logic already has it's own exception handling so I didn't even need to change that. Just made the class extend SwingWorker.

    Thanks for the help. I can see I still have a lot to learn :)

Similar Threads

  1. Printing labels to A4
    By eggy524 in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 04:53 PM
  2. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  3. size och swt labels
    By larsk in forum SWT / JFace
    Replies: 0
    Last Post: 10-04-2009, 12:01 PM
  4. How do I sent font of labels in an array?
    By Exhonour in forum New To Java
    Replies: 9
    Last Post: 01-14-2009, 03:48 PM
  5. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:26 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
  •