Monday, September 4th, 2006

Bridging Java Swing with Ajax

Category: Ajax, Java

Richard Bair of Sun Microsystems has created XMLHttpRequest and JSONHttpRequest beans, both part of the SwingX-WS project.

Interestingly Richard “ased the design and implementation on the W3C Working Draft Specification for XMLHttpRequest. So the good news is, for those of you familiar with XHR, you have essentially the same API available now for your Swing apps.”

This is pretty pragmatic. We all know that XHR may not be the best API in the world, but Richard see’s that a lot of people now know it, so he created something similar on the Swing side (Maybe we will see ProtoSwing or DojoSwing to give a nice wrapper?).

Here is an example in code. Java code remember:

  1. final HttpRequest req = new HttpRequest();
  2.         req.addOnReadyStateChangedListener(new PropertyChangeListener() {
  3.             public void propertyChange(PropertyChangeEvent evt) {
  4.                 if (evt.getNewValue() == ReadyState.LOADED) {
  5.                     String response = req.getResponseText();
  6.                     //do what you like with the response here
  7.                     //for example, if the XML was XMLDecoder
  8.                     //compatible:
  9.                     java.beans.XMLDecoder decoder = new java.beans.XMLDecoder(
  10.                         new ByteArrayInputStream(response.getBytes()));
  11.                     Customer c = (Customer)decoder.readObject();
  12.                     decoder.close();
  13.                 }
  14.             }
  15.         });
  16.         try {
  17.   , new URL("\"A2342DAD\"/"));
  18.             req.send();
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }

Posted by Dion Almaer at 2:24 pm

3.5 rating from 51 votes


Comments feed TrackBack URI

wow, I feel like I just had a Fearsome Premavision of what javascript would be like if it was, you know… strictly typed.


Comment by henrah — September 4, 2006

:) yes..a downward and backward leap made for ? reason…swing already sucked enough as it was….lol

Comment by Jesse Kuhnert — September 4, 2006

Any JS programmer who directly uses XHR, instead of a simple library call like call(“/cats”, feedPet) needs to make a pretty strong justification for it.

Comment by Michael Mahemoff — September 5, 2006

This is the best article abou Java Swing with Ajax

Comment by Počasí — December 2, 2006

Leave a comment

You must be logged in to post a comment.