Skip to main content

Creating multi-threaded Swing apps that consume web services

Posted by robogeek on December 3, 2007 at 11:04 AM PST

Java is (potentially) capable of taking part in the 'Rich Internet Application' game. This is especially interesting as we finish bringing the OpenJDK project to being completely open source (instead of the open-source-but-for-a-few-encumbrances situation we have today) in that an open source Java offers the potential to be an open source answer to Flex.

A question which hovers in my mind is, does the platform truly behave right in regards to being acceptable in the role that Flash/Flex/AJAX/DHTML is filling today.

I just came across this tutorial: Creating multi-threaded Swing apps that consume web services addressing part of this question. An Internet-Aware application would be consuming multiple data services over the Internet and displaying them in an application. So how do you do that and not mess up application behavior?

One thing that seems to regularly trip up Swing developers is the single threaded model. An event listener in Swing executes on the Event Dispatch Thread (EDT) and if the listener takes too long to execute this prevents applications from repainting properly or otherwise feeling very responsive. While it might seem straightforward coding to make database or webservices calls in a listener and then show the result on the screen.. the webservice or database call will require an indeterminate amount of time (due to network traffic vagaries) and during the call the app window will not repaint properly.

The blog entry shows several ways to use SwingWorker to get around this.

Related Topics >>