2

I have a swing worker which configures a serial port connection. This process takes an indeterminable time to complete, sometimes 1 minute sometimes a lot more.

My problem arises when users click a button that needs configuration data whilst the worker thread is still configuring.

I would like to know how to execute a user's request only if the worker thread has completed. Else if worker thread in still alive, I want execution to wait until worker thread has finished.

4
  • 1
    bind the button's action's enabled property to the (successful) termination of the worker, f.i. in a propertyChangeListener to the worker (as @mKorbel suggested) Commented Oct 5, 2012 at 11:05
  • I got round this problem differently, the button gets data from an external system so I had to put that within another thread in the actionPerformed(ActionEvent e) of the action listener. Commented Oct 10, 2012 at 9:00
  • glad you got it working :-) Whatever you did, be sure that you access all components/properties only on the EDT. Commented Oct 10, 2012 at 9:04
  • the configuration was done on the EDT. This is a downside because it takes quite a long time to load the UI. I think I will have to live with that. It was just getting too complex for me. Commented Oct 10, 2012 at 10:10

2 Answers 2

5
Sign up to request clarification or add additional context in comments.

1 Comment

+1 basically correct - next +10 would go for some example code (or a link to one of your examples :-)
2

You could check in your buttons ActionEvent if task.isDone() - where task is your SwingWorker - and continue only if it is true. But you might want to show a popup or something, otherwise the user might get confused why nothing is happening.

Another simple solution is to expose the button and disable it while the task is running and enable it again when it's finished. Then the user can't click the button until it's ready.

1 Comment

Also consider allowing the button to cancel the worker, as shown here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.