Using SwingWorker to update Swing Components asynchronously

I have a JList, the contents of which are updated asynchronously by a different thread. The way I update the JList is by updating the model:

The only problem is, if this is not done in the EventDispatchThread, the UI becomes unresponsive, and starts behaving weird.

The way to solve this to use the SwingWorker.

I have a Jini server, which streams data to a Jini client, in this case, a Swing JFrame. I have a JList to which I publish the data as and when it becomes available.

Jini Service API

Its a simple streaming service:

At its heart is the interface RemoteDataListener, on to which the server publishes data as it becomes available.

Client Implementation

The key is to implement the interface RemoteDataListener along with the SwingWorker as shown below as an inner class in the JFrame:

We invoke the long running service in the doInBackground() method and add itself as a listener. When new data is received in the newData() method, we immediately call the publish() method, which delegates it to the process() method. The process() method is invoked within the eventDispatcherThread. This ensures that any updates made to the model of the JList is reflected on the UI. So, the code to update Swing components reside here. Also note that the done() method is called by the SwingWorker after the processing thread is finished.

Running the example

Run the SpringNonSecureRmiServer to start the Reggie and the Jini Server. After the Jini Server starts up, run the RmiStreamingDemoFrame.

rmi-data-streaming-1

rmi-data-streaming-2

Sources

The sources can be found here: https://github.com/paawak/blog/tree/master/code/jini/unsecure/streaming-with-jini

There are 3 Maven projects under that:

  1. rm-api
  2. rmi-client
  3. rmi-server

Leave a Reply

Your email address will not be published. Required fields are marked *