Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Update GUI before lengthy task

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Update GUI before lengthy task

    I'm trying to change a GUI element before preforming a lengthy function (little icon to show its busy), but I'm not too sure how to do it.

    If I make the change then start the task, GUI doesn't update until the function is finished.
    I can make the GUI changes then start the task in a new thread, but I want to lock all user input too, and disabling every component seems a bit tedious.

    So far, the most promising solution seems to be:
    1) change GUI
    2) set flag indicating long task is running (user input functions check this flag before doing anything)
    3) start thread with task
    4) when done, change GUI back, reset flag

    I'd like to know if there's a better way before continuing.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Update GUI before lengthy task

    A gui should not become un-responsive due to a long task running.

    If you're using a Swing layered design you can loop through all components which have been added to a particular component.

    JPanel somePanel = new JPanel();
    // .. add a bunch of stuff to that somePanel
     
    // disable everything on somePanel
    for(Component c : somePanel.getComponents())
    {
        c.setEnabled(false);
    }

    Similarly, you can automatically re-enable everything using the same technique.

    I would recommend running your task on a separate thread. To simplify your job, have the separate thread do the disabling/enabling of components.

    This solution has the benefit of allowing you to leave certain components active (say for example if you wanted a cancel button) and make your gui more user-friendly (no one likes a non-responsive window). It may very well be easier to design, too.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Update GUI before lengthy task

    I agree with helloworld. But you might also look into using a GlassPane to restrict user input (informatively, not annoyingly).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 5
    Last Post: September 26th, 2011, 12:54 PM
  2. Why my update.jsp is not working?
    By tangara in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: September 8th, 2011, 01:14 AM
  3. Ant Executes and Stops After a Certain Task
    By DanielPros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 10:46 AM
  4. test task for Junior java
    By umami in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 10:31 AM
  5. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM