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 7 of 7

Thread: GUI Lockup during run

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default GUI Lockup during run

    I am creating a program that takes several minutes to run and I want to either tell the user about updates via a textbox at the bottom or via a JProgressBar. Unfortunately, the GUI seems to lock up while it is running. Which means, the program appears to have frozen but it just doesnt let you do anything until it finishes. For example, the JButton stays in its selected state until the program finishes and all the GUI items become unresponsive. The JTextArea at the bottom I attempted to use to update does not update its text until the the program finishes. I attempted to give it a JProgressBar, but the JProgressBar pop-up came up when the program started but didnt finish creating the Frame (just made a transparent frame) and when the program finished, it just gave a 100% finished JProgressBar.

    What can be done about this? It will be hard to use a program when the user might get scared it froze.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GUI Lockup during run

    GUI lockups are usually caused by having your code execute on Swing's Event Dispatcher Thread.
    If you have a long task you want to run, you need to have it execute on its own thread and leave the GUI thread free for Swing.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GUI Lockup during run

    How do I do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GUI Lockup during run

    Look for some sample code here or on other forums that use Thread or SwingWorker. Or read the Java Tutorial.
    Basically you create a Thread with a Runnable and put a call to your long running code in the run() method and start the thread and then exit back to Swing.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GUI Lockup during run

    Ok, so based on what I'm reading, in order to create a Thread, i need to send it a runable object, which would be the object that contains all my methods for what I am trying to do. But, due to this programing doing several different types of things and because each type of thing needs direct access to their own GUI objects, each class that contains the methods I need to run also contains their own JPanel and their components for that JPanel. So, by sending Thread the runable object, wont it also run the GUI in that Thread along with the methods? Which means that my problem will still exist.

    Just for information purposes:
    To give an idea of the scope of the program, it currently contains about 4 different processes with their own classes with about 1500 lines of code each (some of which are commented out for faster periodic testing); as well as 5 helper classes and 1 main class.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GUI Lockup during run

    Sounds like your coding got ahead of your design.
    If you need to have Swing update the GUI while in a long running background task, use the SwingUtilities invoke... methods to call the Swing methods. They will keep your thread isolated from Swing's EDT.

    runable object, which would be the object that contains all my methods
    Not quite. You could call the methods of other classes, vs having them be in the Runnable.

  7. The Following User Says Thank You to Norm For This Useful Post:

    aussiemcgr (July 19th, 2010)

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GUI Lockup during run

    Not quite. You could call the methods of other classes, vs having them be in the Runnable.
    Ok, so I found this works when I put it in the ButtonListener class:

    progressBar = new ProgressBar(10);
    Runnable runnable = new Thread(){
    	public void run(){
    		//cleanSource();
    		writeMain();
    		System.out.println("Finished");
    		confirmArea.setText("Finished \n"+confirmArea.getText());
    	}
    };
    Thread thread = new Thread(runnable);
    thread.start();

    I dont know how pretty it is, but it does the job. Thanks for the help.