WORK BLOG
WORK BLOG
Ajax with Java Swing (JSON)
Monday, June 29, 2009
From some of the web statistics that are available, there seems to be glowing interest in using AJAX in Java Swing applications. That does make sense, given that AJAX is the popular terms nowadays and Java is entrenched in the Enterprise environment. However, AJAX stands for Asynchronous JavaScript And XML, so there is really no Java in there. But I guess what people mean when they talk about Ajax with Java Swing is for a Java Swing Application to read data from a server that is designed for use with AJAX application that is run by a browser.
Then the most important aspect of the application is the Java method that read the data from the web server. The following is such a method that read from a web server that supply data in one AJAX application’s data format JSON.
First the list of class being used in the method.
Note the last 3 items on the list, the classes are open source JSON parsing classes that were obtained from json.org. (For the usage of the JSON classes from json.org, please visit json.org/java for documentation.)
Here is the content of the method.
When the line “JSONObject obj = new JSONObject(tokener);” is reached and executed successfully, a JSON Java object is ready to be use. The JSON Java object would get returned to the caller. All the work that needed to be done from then on would depends on the application and the data returned from the server. For example, if the data contain a field called “displayName” then it can be retrieved from the Java Object “obj” and be displayed on the Java Swing Application. Of course there are much more usage of the data, but that would depends on what the server returns.