Wednesday, April 23, 2008

Foundation of Java Swing Components

In this post, we will try to understand the underlying class hierarchy and role of each class for a Swing Component.
In swing, following components are considered as heavyweight:
JFrame, JDialog, JApplet, JWindow
All the heavyweight components implement RootPaneContainer Interface and hence delegate their operations to JRootPane.
Following diagram shows the class hierarchy of JRootPane:

As shown in the diagram, JRootPane contains JLayeredPane. JLayeredPane contains optional JMenuBar and JContentPane.
And on top of all, there is JGlassPane.

The role of each pane is as follows:
JRootPane:
It is the base pane on which all other swing containers reside

JLayeredPane:
JMenuBar and JContentPane reside on JLayeredPane
There can be multiple layered panes for graphics application. For instance, overlapping shapes is internally implemented by swing by creating overlapping JLayeredPanes.

JMenuBar:
Contains the menu bar for Swing application

ContentPane:
It contains the actual components. LayoutManager operates in this layer to determine shape, size and position of the components.

JGlassPane:
It is positioned above all the panes. It allows programmer to draw on top of all the underlying components. It also deals with mouse movements for Swing components.