File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem managing a loop with Swing app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem managing a loop with Swing app" Watch "Problem managing a loop with Swing app" New topic

Problem managing a loop with Swing app

Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Okay, new to Swing. I am moving a command line application to Swing and want to manage a processing loop (stopping and starting it.) On startup the application kicks the process off and I will stop it or restart it. The stop was easy from what I can tell but I can't get the process to restart (processTestLoop.)

Here is the code sample that I am testing (sorry for the length.) Any thoughts?


pete stein

Joined: Feb 23, 2007
Posts: 1561
You've got a Swing concurrency issue, plain and simple. Bottom line is, when you call Thread.sleep(...) on the main Swing thread, the EDT, you prevent Swing from painting or interacting with the user, so that while the loops still runs, your program appears to grind to a halt. The solution -- use a background thread. For instance you could use a simple background thread like so:

but you should also look into trying to use a SwingWorker object in this situation too. You can read more about all of this here:
Concurrency in Swing
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

I also really suggest you rename your TestLoop field. When I browsed through your Main class code I thought the startLoop was a static method.

How To Ask Questions How To Answer Questions
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Pete, Rob,

Thanks for your pointers. The testLoop field in the actual program is different, I was trying to build a test framework to make sure what track I was on. Should I make the boolean field and the stop/start methods synchronized? Other than those I do not share information.

I am reading up on SwingWorkers now. Just trying to avoid rewriting the command line program and seeing if I can fit it in. Though I may be better off rewriting portions of the logic to fit better.

I agree. Here's the link:
subject: Problem managing a loop with Swing app
It's not a secret anymore!