I have written a GUI for operating a card reader - mainly consisting of an ADD button that brings up a FileChooser
dialog and queues the chosen File
onto a CardHopper, which is displayed visually as a JList
.
Now I want for the CPU (another JFrame
, on another thread) to be able to make requests of the card reader; e.g. read a card and send it to me. Before the card reader had a GUI, it was just a model that ran on the same thread as the CPU, so I could just call its readCard()
method. Now that it's on a separate thread, it seems like the right way to communicate is with message-passing.
I was about to implement something using a PriorityBlockingQueue
, whereby the CPU would put
a read-card command on the queue and the CardReader would take
and execute the command, until I realized that the CardReader thread would normally be blocked somewhere within its NetBeans-supplied Swing code, waiting for a GUI event and blind to the arrival of anything on my event queue. Moreover, this "back end" request would alter the data model - so even if I could somehow execute some code in the model, would it be kosher to "fire" notifications to the GUI's ListDataListeners
while the GUI is waiting for GUI events?
I hope this isn't too cryptic - I'm still trying to get my arms around the mechanics of GUIs and threads.