Meaningless Drivel is fun!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using SwingWorker" Watch "Using SwingWorker" New topic
Author

Using SwingWorker

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 40
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?

Thanks,
Paul
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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: 40
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
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using SwingWorker
 
Similar Threads
Returning A List Of Variables From A Folder Of Documents And Returning Them Into A New Document
Finding best deal of mall program in java
Using synchronized still doesn't solve the "lost update" problem
Threads and Synchronization examples
Phone subscribers multiple threads: incompatible type errors and cannot find symbols error