Creating multi-threaded Swing apps that consume web services

; Date: Mon Dec 03 2007

Tags: Java

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: (web.archive.org) 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.

Source: (web.archive.org) weblogs.java.net

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.