Saturday, June 21, 2008

Type Safe Enums

Ever thought about how much effort is saved by following line:
enum Color{ RED, BLACK}

It looks like a simple enum but before Java 5.0, to achieve somewhat same functionality following was required:

public final class Color
{
private Color ()
{
}
public static final Color RED = new Color ();
public static final Color BLACK = new Color ();
}

And still above Color type cannot be passed to RMI/CORBA as it is not serializable.

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.