Swing (Java)
From Free net encyclopedia
Image:Gui-widgets.png Swing is a GUI toolkit for Java. Swing is one part of the Java Foundation Classes (JFC). Swing includes graphical user interface (GUI) widgets such as text boxes, buttons, split-panes, and tables.
Swing widgets provide more sophisticated GUI components than the earlier Abstract Windowing Toolkit. Since they are written in pure Java, they run the same on all platforms, unlike the AWT which is tied to the underlying platform's windowing system. Swing supports pluggable look and feel – not by using the native platform's facilities, but by roughly emulating them. This means you can get any supported look and feel on any platform. The disadvantage of lightweight components is possibly slower execution. The advantage is uniform behavior on all platforms.
Contents |
History
The Internet Foundation Classes (IFC) were a graphics library for Java originally developed by Netscape Communications Corporation and first released on December 16 1996.
On April 2 1996, Sun Microsystems and Netscape Communications Corporation announced their intention to combine IFC with other technologies to form the Java Foundation Classes. In addition to the components originally provided by IFC, Swing introduced a mechanism that allowed the look and feel of every component in an application to be altered without making substantial changes to the application code. The introduction of support for a pluggable look and feel allowed Swing components to emulate the appearance of native components while still retaining the benefits of platform independence.
Originally distributed as a separately downloadable library, Swing has been included as part of the Java Standard Edition since release 1.2. The Swing classes are contained in the Template:Javadoc:SE package hierarchy.
Relationship to AWT
Since early versions of Java, a portion of the Abstract Windowing Toolkit (AWT) has provided platform independent APIs for user interface components. In AWT, each component is rendered and controlled by a native peer component specific to the underlying windowing system.
By contrast, Swing components are often described as lightweight because they do not require allocation of native resources in the operating system's windowing toolkit. The AWT components are referred to as heavyweight components.
Much of the Swing API is generally a complementary extension of the AWT rather than a direct replacement. In fact, every Swing lightweight interface ultimately exists within an AWT heavyweight component because all of the top-level components in Swing (Template:Javadoc:SE, Template:Javadoc:SE, Template:Javadoc:SE, and Template:Javadoc:SE) extend an AWT top-level container. The core rendering functionality used by Swing to draw its lightweight components is provided by Java2D, another part of JFC. However, the use of both lightweight and heavyweight components within the same window is generally discouraged due to Z-order incompatibilities.
Relationship to SWT
The Standard Widget Toolkit (SWT) is a competing toolkit originally developed by IBM and now maintained by the Eclipse Foundation. SWT's implementation has more in common with the heavyweight components of AWT. This confers benefits such as more accurate fidelity with the underlying native windowing toolkit, at the cost of an increased exposure to the native resources in the programming model.
The advent of SWT has given rise to a great deal of division among Java desktop developers with many strongly favouring either SWT or Swing. A renewed focus on Swing look and feel fidelity with the native windowing toolkit in the approaching Java SE 6 release (as of February 2006) is probably a direct result of this.
Example
The following is a Hello World program using Swing.
import javax.swing.JFrame; import javax.swing.JLabel; public final class HelloWorld extends JFrame { private HelloWorld() { setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); add(new JLabel("Hello, World!")); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { new HelloWorld().setVisible(true); } }
See also
References
- Matthew Robinson: Swing, Manning, ISBN 1-930110-88-X
- David M. Geary: Graphic Java 2, Volume 2: Swing, Prentice Hall, ISBN 0-13-079667-0
- James Elliott, Robert Eckstein, Marc Loy, David Wood, Brian Cole: Java Swing, O'Reilly, ISBN 0-596-00408-7
- Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour: The JFC Swing Tutorial: A Guide to Constructing GUIs, Addison-Wesley Professional, ISBN 0-201-91467-0
- Joshua Marinacci, Chris Adamson: Swing Hacks, O'Reilly, ISBN 0-596-00907-0
External links
- The Swing Tutorial
- The Swing Connection
- Template:Javadoc:SE-guide
- The Swing architecture
- JavaDesktop
- Java Look And Feel
- ClientJava.com
- Presentation "Professional Swing: Creating Polished Apps, Part 1/2" by Ben Galbraith
- Presentation "Professional Swing: Creating Polished Apps, Part 2/2" by Ben Galbraith
- Article "What does "Swing is Slow" mean?" by Sermet Yucel
- Article "Java Desktop Development" by Andrei Cioroianu
- Article "BlackMamba: A Swing Case Study" by Ashwin Jayaprakash
- Articles on AWT/Swing
- Citations from CiteSeer
- Swing wiki containing best practices, tips, tricks and howto tutorials
- JGoodies de:Swing (Java)
es:Swing (biblioteca gráfica) fr:Swing (Java) nl:Swing (Java) ja:Swing pt:Swing (api) ru:Swing (Java) zh:Swing