What is Swing?
Swing is the "new" GuiToolkit
, which partly replaces the JavaAwt
toolkit. Only partly, because Swing uses some fundamental mechanisms of the AWT, like the event handling. Basically, Swing uses the platform-independent AWT features, and the AWT top level windows to get access to the native GUI system.
The most important things to know about Swing are that
- All of the components are lightweight; they are not platform-native widgets
- The components (subclasses of the JComponent class) are drawn by delegates
The implication of the delegation architecture is that you may radically change the look and feel of a Swing-based application by asking the UIManager to use a given set of UI delegates. Swing comes with look-and-feels that mimic native widgets on Windows and Motif, plus one Sun-designed L&F called "Metal". The OS X port of Java comes with that candy-pill Aqua look and feel.
Furthermore, Swing maintains a database of properties that influence the delegates. For example, you may set the property governing the default background color of all JPanels, and all JPanels constructed from that moment on will have that background color. This is good because you needn't subclass JComponents to change their behavior, which in turn is good because even components created by third-party code will exhibit your customized attributes.
How are UIs specified?
There are several ways that UIs are created in Swing. Some people write code by hand, others use visual tools to generate the code. In general, it is possible to specify a UI not through code, but by serializing the components -- JavaBeans
-- involved. This seems to be less commonly used.
Until JDK 1.4, the serialised format of Swing components was not guaranteed to remain stable between different versions of the JDK, and so was not recommended for long term storage, only short term persistence and sending between address spaces, in RMI messages for example.
A common way to specify a GUI in Swing is to subclass one of the window classes (JFrame for main windows, JDialog for dialogs) and implement the window contents (allocation and placement of all the widgest) in that subclass. This is not strictly required, but has emerged as a common idiom. That idiom is also useful because it allows to implement said subclasses as JavaBeans
, thus the window implementations become re-usable components when used in a Bean's framework. Note: All modern Java IDEs contain a JavaBean management framework. So these own windows can be added to the IDE's tools pallet and be re-used with a few clicks.
GUI builders are usually also relying on the introspection features for JavaBeans
and generate their window code as subclasses of one of the Swing window classes.
How are widgets arranged on the screen?
There are several LayoutManager
s, e.g., BorderLayout
This author has found combinations of BorderLayout
to sufficient for all but a few applications. Unfortunately, every once in a while, only GridBagLayout
will do. GridBagLayout
is powerful but bletcherous. BorderLayout
are conceptually simple, and BorderLayout
is the way to go when you want your layout to resize
a given component on the fly.
How are the UI and the application model kept in sync?
All Swing components keep their data in models, e.g., Document (a model for text components), TableModel?
. These models can be models in the original ModelViewController
-sense, but often they are acting as adapters to an underlying application model.
A Swing component registers a listener with an appropriate model. When the model changes and its listeners are notified, the components update the visual representation accordingly. When a user the component, this is directly written through to the model and listeners are notified.
How are actions triggered from the UI?
Interested parties register ActionListener
s with suitable UI components.
Contributors: MichaelSchuerig ThomasWeidenfeller JonathanFeinberg
The Last Word in Swing Threads
Swing Pages: DrawingModelOfSwing
Please see also the java wiki JinxWiki
on the SwikiFarm
to the pages
There is a discussion with the topic Swing: Handling Complex Layouts http://jinx.swiki.net/236
on the JinxWiki
on the SwikiFarm
See also: JavaEventHandling