I’m using Scala to write a GUI application in Swing that starts multiple threads to download files via HTTP concurrently. I’d like to use a number of JProgressBars to display the status of the downloads. Obviously, the progress bars have to be updated from the event dispatching thread and besides, I would like to keep the HTTP download code separate from the GUI.
I’m wondering what the best way to do this would be? SwingWorker is Swing specific and Observable seems somewhat archaic in that the parameter passed to the observer is an object and would have to be cast. Scala is a powerful modern language so I was hoping there’s a simpler way to do what I want.
I achieved what I wanted by implementing my own listener callback system using Scala closures. The following code goes in the Download class:
and this goes in a nested SwingWorker in the GUI: