File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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?

Thanks
Mike



pete stein
Bartender

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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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.

Thanks
Mike
 
Consider Paul's rocket mass heater.
 
subject: Problem managing a loop with Swing app
 
Similar Threads
Jpcap captured packets not appearing in Swing application's text area
How to display toolbars like forte4java
Swing GUI Interface Question
How can I use the Class JpcapCaptor loopPacket method without blocking my Windows app
Need Help resizing my JScrollPane