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 Using SwingWorker Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using SwingWorker" Watch "Using SwingWorker" New topic

Using SwingWorker

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 56
Hello folks,

I'm trying to get my head around SwingWorker, I'm creating a fairly simple vending machine application. And I've identified the following use cases

Start: Message = "MACHINE READY"
Keypad pressed (change>0 && prodcode.length<=2): append to product code, update message with Balance: xx Product: x
Enter pressed (change>0 && code.length>1): check money inserted, check code is valid, check slot for item, check correct change
update item tray with item, reduce slot count by one, update message to Balance: xx Product:
Clear pressed: reset change to 0 prod code to blank and message to "MACHINE READY"
Return change pressed: set change tray to current change for 3 seconds, set change to 0 prod code to blank, update message to "MACHINE READY"

(a)Invalid code: Message = "Invalid code" wait for 3 seconds, update message to Balance: xx Product:
(b)Incorrect change (change>0): Message = "Incorrect change" wait for 3 seconds, update message to Balance: xx Product:
(c)Incorrect change (change==0): Message = "Insert money" wait for 3 seconds, back to start message
(d)Empty slot: Message = "Slot empty" 3 seconds, update message to Balance: xx Product:

Here's the UI

I'm finding it hard identifying what kind of SwingWorker tasks I'm going to need to achieve all this, can anyone help?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10815

From the API docs
An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.

Does your application perform any lengthy tasks which would freeze up the UI? In those scenarios, a SwingWorker would be helpful.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 56
Ok then perhaps I'm just getting confused with how to get my GUI to respond to my commands in general? I've written code which as far as I am aware should work, but when I click on any of the buttons the UI becomes unresponsive. The tasks its doing shouldn't be that labour intensive so I'm a little bit stuck (SwingWorker would probably be overkill from what you're saying).

I think I've included all of the important snippets of code, I must be missing something very fundamental?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Are you sure it's the call to setMessage that freezes your UI, and not the code you omitted at line 33? Can you post that part as well? And the code in vMachine.giveChange(), as that's also a possible time consuming call.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Using SwingWorker
It's not a secret anymore!