简体   繁体   中英

Threading with Java Swing

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM