Swing is a tool kit in Java which provides a way to build cross platform user interfaces. It is built on top of and designed as a replacement for AWT, the other UI toolkit built into Java. SWT is a third toolkit you may hear about. SWT is an open source toolkit and is a full topic in of itself, see SWT's Homepage for more information.
Swing provides many controls and widgets to build user interfaces with. Swing class names typically begin with a J such as
JFrame. This is mainly to differentiate them from their AWT counterparts and in general are one-to-one replacements. Swing is built on the concept of Lightweight components vs AWT and SWT's concept of Heavyweight components. The difference between the two is that the Lightweight components are rendered (drawn) using purely Java code, such as drawLine and drawImage, whereas Heavyweight components use the native operating system to render the components.
Some components in Swing are actually heavyweight components. The top-level classes and any derived from them are heavyweight as they extend the AWT versions. This is needed because at the root of the UI, the parent windows need to be provided by the OS. These top-level classes include
JApplet. All Swing components to be rendered to the screen must be able to trace their way to a root window or one of those classes.
|Generally it is not a good idea to mix heavyweight components with lightweight components (other than as previously mentioned) as you will encounter layering issues, e.g., a lightweight component that should appear "on top" ends up being obscured by a heavyweight component. The few exceptions to this include using heavyweight components as the root pane and for popup windows. Generally speaking, heavyweight components will render on top of lightweight components and will not be consistent with the look and feel being used in Swing. There are exceptions, but that is an advanced topic. The truly adventurous may want to consider reading this article from Sun on mixing heavyweight and lightweight components.
So what does using Swing get you? So far we've only talked about components and rendering. Well, to start with you get the following.
- Controls: Buttons, Check Boxes, Lists, Trees, Tables, Combo boxes (dropdown list), Menus, Text fields
- Displays: Labels, Progress bars, Icons, Tool Tips
- Pluggable look and feels (PLAFs): Windows, CDE/Motif, Mac. Allows for "skinning" the application without changing any code
|Due to legal issues between Sun, Microsoft & Apple, you are only able to use the Windows Look and Feel (LAF) on Windows and the Mac LAF on Apple computers
- Standard Top-Level Windows: Windows, Frames, Dialogs etc.
- Event Listener APIs
- Key bindings & mnemonics: Allow keystrokes to map to specific actions.
- Swing is also referred to incorrectly as JFC (Java Foundation Classes), however the JFC includes APIs in addition to Swing, such as the Java 2D and Drag-n-Drop APIs.
- AWT is the Abstract Windowing Toolkit, where the components are rendered by the native operating system