The Action interface provides a useful extension to the
ActionListener
interface in cases where the same functionality may be accessed by
several controls.
This class enables one to store an entry in the defaults
table that isn't constructed until the first time it's
looked up with one of the getXXX(key) methods.
A lightweight object that provides many of the features of
a native frame, including dragging, closing, becoming an icon,
resizing, title display, and support for a menu bar.
JPasswordField is a lightweight component that allows the editing
of a single line of text where the view indicates something was
typed, but does not show the original characters.
A KeyStroke instance represents a key being typed on the keyboard -- it
contains both a char code for the key and a modifier (alt, shift, ctrl,
meta, or a combination).
This class manages repaint requests, allowing the number
of repaints to be minimized, for example by collapsing multiple
requests into a single repaint for members of a component tree.
An exception that indicates the request look & feel management classes
are not present on the user's system.
Package javax.swing Description
Provides a set of "lightweight"
(all-Java language) components that,
to the maximum degree possible, work the same on all platforms.
For a programmer's guide to using these components, see
Creating
a GUI with JFC/Swing, a trail in The Java Tutorial.
For other resources, see
Related Documentation.
The Swing architecture provides a number of advantages in the
areas of:
In general, the class names that start with "J" are the components you add to an application.
Examples: JButton, JLabel, JList, JPanel, JTable, JTree. The remaining files in the swing package
contain the utility classes and interfaces that the components use to function.
Pluggable Look and Feel
Instead of being restricted to a single "look and feel", you can select a look and feel and "plug
it in". An interface made of Swing components can look like a Win32 app, a Motif app, or a Mac app. It can
use the new "Metal" look and feel. Developers can use standard Swing components and design their own
look and feel (L&F) for them, and even allow users to select the look and feel they prefer. The plaf
package includes the standard "Pluggable Look And Feel" classes.
MVC Architecture
The Model-View-Controller (MVC) architecture is used consistently throughout the Swing component set.
The View and Controller parts of the architecture are combined in the component. Each component has an associated
Model class and an interface it uses. You can provide your own data-model for a
component by subclassing the Model class or by implementing the appropriate interface. For example,
you could subclass DefaultListModel or implement the ListModel interface, and then use
the JList setModel method to attach your data-model to the component.
Keystroke Handling
The JComponent architecture makes it easy to handle keyboard events in nested components. You register interest
in a particular combination of keystrokes by creating a KeyStroke object and registering it with the component.
When you register the keystroke combination and its associated action, you also specify one of the following conditions
to determine when the action is initiated:
The component has the focus.
A child or grandchild of the component has the focus.
You can use this capability to define global actions for a composite component. For example, a tree control could
act on the Plus key to expand all elements in the tree, as long as any component in the tree has the focus.
The window the component is in has the focus.
In other words, if the component is a child or grandchild of a window, and any component in that window has the
focus, then the component receives the event. This capability is used to direct the keystroke combination Alt+C
to the Cancel button in a dialog, for example, as long any component in the dialog has the focus.
For an index of the default keystroke behaviors for the Swing components in each of the standard
Look and Feel implementations, see Swing Component Keystroke Assignments.
Action Objects
Action-interface objects provide a single point of control for program actions. For example, a toolbar icon
and a menu item can reference the same Action object. When the Action object is disabled, the GUI items that reference
it are automatically disabled. The Action interface extends ActionListener, specifying an enabled property as well
as properties for text-descriptions and graphic icons.
Nested Containers
As the keystroke handling shows, Swing was designed to manage nested containers gracefully.
The main "heavyweight" containers (JWindow, JFrame, JDialog, and JApplet) as well as the major
"lightweight" containers (JInternalFrame and JComponent) all delegate their operations to a
JRootPane. This commonality produces a high degree of regularity in container nesting. In particular, since
the fundamental component class (JComponent) contains a JRootPane, virtually any component can be
nested within another. It means, for example, that a graphic can be nested in a list, and a combo box can
be nested in a toolbar.
The JRootPane class uses a JLayeredPane to manage a content pane and an optional menu bar in a way that is
virtually transparent to the developer. It also provides for a glass pane -- a single pane that can overlap multiple
containers and be used for drawing or to intercept mouse actions.
Virtual Desktops
The JDesktopPane and JInternalFrame classes can be used to create a virtual desktop, or "multiple
document interface". A JInternalFrame can be specified as iconizable, exandable, or closable, while the
JDesktopPane provides real estate for them to operate in.
Compound Borders
Insets (the space between the edges of the component and the area it is drawn in) can be specified with a blank
border. In addition, many border styles are available, which can be combined to create compound borders.
Customized Dialogs
The JOptionPane class provides a variety of static methods that you can invoke to create and display both message
dialogs and user-choice dialogs in a variety of formats. The "message" displayed in the dialog can be
a string, a string-generating object, or an arbitrary component. You can also replace the choice-buttons with components
you specify for user-selections.
For example, the following line creates a simple message dialog that requires the user's confirmation:
JOptionPane.showMessageDialog(null, "The file will be deleted.");
Additional options for customized dialogs are described in the JOptionPane class.
Standard Dialog Classes
Standard dialogs currently available include:
JFileChooser
JColorChooser
And more standard dialogs are on the way.
Structured Table and Tree Components
The JTable class provides a data-aware matrix. JTree provides hierarchical-structuring of
data elements.
Powerful Text Manipulations
In addition to single-font text fields and text areas, Swing provides a JPassword field for hidden
input and a JTextPane class for displaying multi-font text. In addition, the JEditorPane class provides
editing capabilities for multi-font text, while the text.html and text.rtf packages handle text encoded
in HyperText Markup Language (HTML) or Rich Text Format (RTF).
Generic Undo Capabilities
The undo package provides generic undo capabilities that can be used in a variety of situations.
Accessibility Support
Swing has built-in support for developers to make products that are compatible with Assistive Technologies (for
alternative interfaces like, for example, braille.) All of the Swing components implement interface Accessible.
Submit a bug or feature For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright 1993-2001 Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, California, 94303, U.S.A. All Rights Reserved.