Way2Java

Java JFC Swing Introduction

Swing Introduction: JFC stands for Java Foundation Classes. It is a collection of five APIs : AWT, Accessibility, 2D API, Drag and Drop and Swing. Similar to JFC are NFC (Netscape Foundation classes) or IFC(Internet Foundation Classes, of course with different functionality).

AWT API is used to display GUI components which is the original toolkit supplied by Java. AWT provides the foundation upon which the rest of the JFC is built. AWT components lack many GUI features which other GUI technologies give. For example, we cannot place an image on the button or change the border of a button. All these are overcome in Swing. Swing adds many features to AWT components. To achieve this, designers added new classes and came out with a special packages javax.swing and javax.swing.event. It is preferred to know AWT before attempting Swing.

Accessibility API is used to map components according to screen setting in terms of pixels. It consists of classes that enable Swing components to interact with assistive(related) technologies for users.

2D API is used to display GUI components in two – dimensional and three – dimensional elevations. The border of a component can be drawn in different thicknesses styles.

Drag and Drop API is used in JavaBeans to drag components from ToolBox and placing them on to the BeanBox.

Swing API is a set of mostly lightweight components built on top of the AWT. Swing provides lightweight replacements for the AWT’s heavyweight components. Swing also supplies multiple components which AWT is lacking like JScrollPane, JPasswordField, JColorChooser etc. Swing offers a very attractive look and feel appearance for its components than AWT.

Swing Introduction: What is heavyweight component?

Whenever you create a AWT GUI component like a button, a call will to the underlying OS to get the design of the button. AWT does not maintain the structure and appearance of a button. It depends on the OS where the Java program is being executed. Java copies the look of the button and displays to the user. It is a very time consuming process. For this reason, AWT components are known as "heavyweight components".

Swing Introduction: Why Java AWT is designed to be heavyweight components?

It is all done by keeping in view of the user-friendliness (even by loosing performance). Think AWT got its own design of the button and you display it in a frame. Besides your Java frame, a Windows wizard exists with Windows style of button. Now user feels inconvenience by seeing two different styles of buttons. If Java frame gives the same type of button of Windows; you feel comfortable as you are using the same button with which you are quite a long time acquainted.

If you execute the same button program on three different OS, you get three different styles of buttons. It is for user’s convenience only. Some people (of always argument nature) argue, basing on this feature, Java is not platform independent language.

Note: At the outset, I find three reasons why Java execution is slower. 1) bytecode conversion to binary at the time of execution by JVM 2) String immutability 3) AWT components heavyweight nature.

Swing Introduction: Why AWT components have fewer features compared to other similar GUI environments?

As AWT components are heavyweight, designers took all the common features of GUI components that exist in all the OS. Suppose one OS supports an image on the button and if the other does not support. If I write a Java program with the image on the button, if the other OS does not support then what is the fate of the program when executed. Finally, the result is less features.

Swing Introduction: What is lightweight component?

Contrary to AWT components, the swing components maintain their own design, appearance, look and feel. They do not depend on the underlying OS. That is, a swing button program executed on any OS gets you the same design of button. For this reason, swing components are known as "lightweight components". As swing does not depend on the OS, it added a lot of features to its GUI components like adding images to components to look better and displaying a label in multiple rows with bullets (of HTML type) etc.


Differences between AWT and Swing

1. Unlike AWT components, Swing components carries information along with it and hence will not contact the OS. This increases the performance of the application.
2. Swing components are lightweight components and where as AWT components are heavyweight components.
3. The extra features of Swing which do not exist in AWT are, for example, it is possible to display images on button, setting a tool-tip for a button and specifying an access key (or hotkey or shortcut key) for a button.
4. Lightweight components need not be rectangular while heavyweight components are always rectangular.
5. AWT can make use of both 1.0 and 1.1 event handling. But JFC components will work with 1.1 event handling.

Swing Introduction: Main New Features

1. Lightweight: Not built on native window-system windows.
2. Much bigger set of built-in controls: Trees, image buttons, tabbed panes, sliders, toolbars, color choosers, tables, text areas to display HTML or RTF(Rich Text Format) etc.
3. Much more customizable: Can change border, text alignment, or add image to almost any control. Can customize how minor features are drawn. Can separate internal representation from visual appearance.
4. "Pluggable" look and feel: Can change look and feel at runtime, or design own look and feel.
5. Many miscellaneous new features: Double-buffering built in, tool tips, dockable tool bars, keyboard accelerators, custom cursors, etc
6. Model-View-Controller architecture: It lets us change the internal data representation for lists, trees, tables, etc.
7. Metallic look: We get Java Metal look by default and we can change to the native look when ever wanted.

For more features visit on Swing Introduction: Swing Overview

The supporting packages for swing components are javax.swing and javax.swing.event.

Content pane: The content pane is associated with JFrame. The content pane is the container that contains the components. The content pane resides on JFrame. Components should be added to a JFrame through content pane and not directly to the JFrame itself (in AWT, we add components to frame itself). getContentPane() method of JFrame returns an object of Container class as follows :

Container c = getContentPane() ;

The default layout manager is BorderLayout.

Following is the method signature of the getContentPane() method:

public Container getContentPane()

That is, we add swing components to the Container returned by the getContentPane() method of JFrame. In turn, Container adds to JFame (as container resides on JFrame, the components look as if added to JFrame). The default layout manager for Container is BorderLayout.

Why all this content pane or container?

Another new layout manager introduced with Swing is OverlayLayout Manager and BoxLayout Manager.