For my current application, I am struggling with identifying the Swing threads in my application. With Swing threads I mean:
- Initial threads
- The event dispatch thread
- Worker threads
My application:
- simple user interface which is supposed to display data received on a socket
- the data is described by many model classes
- the received data is XML which is parsed and the model objects are instantiated
- the user interface is supposed to display the received data
- these data is updated very frequently, which means the XML messages are short, but there are many of them
- to put it into context: I am programming a Java profiler.
I have read the Swing tutorial so far, so here are my guesses and questions:
The background task is the server socket, respectively the background tasks are the number of opened connections on which the application receives data.
-
The tasks have no final result, so I guess the
SwingWorker<T,S>should only define the generic type for the Interim Result? For every parsed XML I would make a call topublish. But how do I distinguish which data I have received? Maybe the XML data contains only enough information to build aclass Aor maybe the data contains enough information to buildclass Aandclass B, but how do I wrap both into one Interim Result? A wrapper class? -
The
process()method invokes changes to make it visible to the user interface, doesn’t it? I don’t see how this works. Where do I launch my tasks? Is it in order to invoke theSwingWorker.execute()in theJFrameconstructor? -
Should the XML Reader be the Task or should each Thread which handles an incoming connection be the task?
In the context you describe, I am not sure I would use
SwingWorker.My basic idea would be:
main(), start several threads for serving sockets (standardThreadAPI)finalvariable, and callSwingUtilities.invokeLater()to display this result in your UIAnother alternative that I have used successfully in the past would be to use an EventBus that would take care of callingthe UI update method in the EDT, your socket threads would send the “result object” to that EventBus.
About
SwingWorkeruse, I would say the main use is when the end user starts an action (e.g. by clicking a button or a menu item) and this action is long and should be processed in background, the background processing method would then have to feed back information to the UI.