Monday, March 05, 2007

SWING CORNER

I have been involved with development J2EE projects from past several years. As I am looking to work with a SWING project now, I wanted to collate all the information regarding the SWING that should help me refresh my SWING skills & also to help new programmer to ramp up on SWING quickly. I also wanted to use this SWING corner blog as my SWING books-marks for my future reference.So I will keep adding/editing the information here. :-)

Real time applications are best testimonials for any technology.Here are the set of real world applications for non-SWING believers.

Selected links to start with Java SWING for beginners:
wikipedia SWING Overview
Best place to start with SWING
Swing Pointers
Sun Tutorial about SWING architecture

It is always best approach to follow best SWING programmers to learn new techniques. Here is my favorite list & their blogs. All are committed SWING developers.
1. Romain Guy with amazing swing skills
2. Ethan Nicholas
3. Kirill Grouchnikov & http://www.curious-creature.org/
4. Shannon Hickey
One more interesting Blog

There are also equally credible people saying Java SWING has reached it's dead end :-( like Bruce Eckel of Thinking Java fame, So it is equally important to watch the Adobe Flex APIs & Flash. Also there is lot of effort is being put on developing JSF components. After looking into exadel , GWT & ICE Faces I feel like do we really need thick clients?

Some Useful Java Links that will be helpful for new development:
Lot of links Articles,Blog Posts,Interviews Commercial/Open Source Projects reated to dektop development
Libraries that can ease development effort
Great Swing Components from Swing labs
Popular layout managers (Gridbaglayout has been very un-popular & difficult to work)
FormLayout
TableLayout
JGoodies Validation https://validation.dev.java.net
JGoodies Binding https://binding.dev.java.net
Spin http://spin.sf.net
Foxtrot http://foxtrot.sf.net
Swing Code samples http://www.codeguru.com/java/Swing/index.shtml
Chart building solution for swing : http://www.jfree.org/jfreechart/ - Google AdWords & Jasper Reports uses these APIs

SWING Books & Frameworks:
Popularity of any technology can also be measured from the dedicated books that have been published, Considerably there are less books focusing completely on SWING, although most of Java generic books covers SWING superficially.

There are two books published by Orielly on SWING (Hacking Swing & Java SWING ) & both are good ones, There is one book from Manning publisher Swing in Action & there is also Desktop Java Live—published by SourceBeat.
USefulness of these books are limited as it will be totally API driven & time bound, the sample source code can be downloaded from the provided by these books & learn APIs by going though the code.I guess that's the best way.The cooper's book on Design patterns contains the design pattern samples developed in SWING. The book & source code are free & can be downloaded from here.

The single-most important point to choose Java as development platform for me is availability of massive amount of open source frameworks & utilities. I guess this point is not well appreciated by well known Java opponents Joel& Paul Graham (Although they are on my favorite writers list :-)). Unfortunately there are less number of frameworks & re-usable components in SWING compared web part of java (Tag libraries, JSF components, Library utilities & MVC frameworks)
But these are few attempts in that direction, basically these frameworks tries to come up with configurable action handling, form validation, custom UI components built over standard JFC components, Custom layout managers to ease out component layering & lot of static utilities for handling validation & threading issues.

http://www.artima.com/lejava/articles/swingframework.html
http://carbonfive.sourceforge.net/springadapter/api/com/carbonfive/flash/spring/package-summary.html#documentation
Spring Rich Client - As usual Spring efforts here also are very singificant in reducing source code lines which is very important in the SWING context
"Spring makes heavy use of the "Inversion of Control" (IoC) design pattern, meaning simply that individual JavaBeans don't go looking for data, the data is handed to them by the framework"

Some important Concepts:

1. Understanding lightweight architecture
Each heavyweight component contains its own z-order layer (the notion of depth of layering) all lightweight components are inside heavyweight components and maintain their own layering scheme defined by Swing.when a heavyweight inside another heavyweight container is placed it will, by definition overlap all lightweights in that container.we have to use mixing of AWT and SWING components cautiously

-A lightweight does not contain the native code (JNI), You don't have call "System.loadLibraty()" & hence inherently cross-platform, there are may off the shelf compoenents providing different Skins (Look and Feel Factory)
-A lightweight component can have transparent pixels; a heavyweight is always opaque.
-A lightweight component can appear to be non-rectangular because of its ability to set transparent areas; a heavyweight can only be rectangular.
-Mouse events on a lightweight component fall through to its parent; mouse events on a heavyweight component do not fall through to its parent.
-When a lightweight component overlaps a heavyweight component, the heavyweight component is always on top, regardless of the relative z-order of the two components.
-JFrame , JDialog, JApplet,JWindow are the top level components & hence extend AWT compoenent instead of JComponent unlike other components

A Swing component does NOT have a corresponding native OS GUI 'peer', and is free to render itself in any way that is possible with the graphics APIs.However, at its core, every Swing component relies on an AWT container, since (Swing's) JComponent extends (AWT's) Container. AWTEvent root event class for all AWT events & there is no JVM from Sun that uses full HW acceleration, and it SHOWS. Java 5 has *partial* HW acceleration - although significantly more than java 1.4 had

2. Understand the layout managers,data binding & validation well

3. Understand Good coding practices w.r.t. Swing.
Follow the open source code (As per the list above)
Written in 2000, but still good to go though http://www.oreillynet.com/pub/a/oreilly/java/news/learningjava_0500.html

There has been quite lot of work on SWING (Now unfortunately it looks like Sun also started loosing interest in pushing Java in Desktop arena, It is now left for Open source hacker to push SWING to next level) Swing classes contributed heavily to the exponential growth of total number of java classes as part of JDK.JFC has got solid framework & design.
Total # of Java Files as of now stands at
JDK1.4 : 3,883
JDK1.5 : 6,558
JDK1.6 : 7,061


As a SWING proponent (With vested interest :-) as of now), I tried to find some answers for the current stated SWING problems

A. SWING is very slow compared to SWT.
IntelliJ-IDEA (Swing) is faster than Eclipse IDEA and Eclipse have matching complexity, and despite Eclipse is backed by IBM and open source. it still does not perform better than IDEA, which is proprietary and developed by small group of smart individuals.
Java IDE's like netbeans, Weblogic Workshop & JBuilder are written in SWING.
So it's all about how well we understand & write the code.

B. SWING solutions have very bad distribution model (With lot of confusing JREs)
I guess Sun is working hard to improve the Webstart & I hope it will be as simple as Flash plug-in (Both in terms of size and ease of use)

3. SWING is complex to work with
I guess with proper usage of scripting (Like F3, Groovy solutions) & XML should make the life simpler

Hope this blog will be useful for new as well as old SWING developers. :-)
Bookmark and Share