Package javax.swing


package javax.swing
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.

Swing's Threading Policy

In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread.

Typical Swing applications do processing in response to an event generated from a user gesture. For example, clicking on a JButton notifies all ActionListeners added to the JButton. As all events generated from a user gesture are dispatched on the event dispatching thread, most developers are not impacted by the restriction.

Where the impact lies, however, is in constructing and showing a Swing application. Calls to an application's main method, or methods in Applet, are not invoked on the event dispatching thread. As such, care must be taken to transfer control to the event dispatching thread when constructing and showing an application or applet. The preferred way to transfer control and begin working with Swing is to use invokeLater. The invokeLater method schedules a Runnable to be processed on the event dispatching thread. The following two examples work equally well for transferring control and starting up a Swing application:

 import javax.swing.SwingUtilities;

 public class MyApp implements Runnable {
     public void run() {
         // Invoked on the event dispatching thread.
         // Construct and show GUI.
     }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new MyApp());
     }
 }
Or:
 import javax.swing.SwingUtilities;

 public class MyApp {
     MyApp(String[] args) {
         // Invoked on the event dispatching thread.
         // Do any initialization here.
     }

     public void show() {
         // Show the UI.
     }

     public static void main(final String[] args) {
         // Schedule a job for the event-dispatching thread:
         // creating and showing this application's GUI.
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new MyApp(args).show();
             }
         });
     }
 }
This restriction also applies to models attached to Swing components. For example, if a TableModel is attached to a JTable, the TableModel should only be modified on the event dispatching thread. If you modify the model on a separate thread you run the risk of exceptions and possible display corruption.

Although it is generally safe to make updates to the UI immediately, when executing on the event dispatch thread, there is an exception : if a model listener tries to further change the UI before the UI has been updated to reflect a pending change then the UI may render incorrectly. This can happen if an application installed listener needs to update the UI in response to an event which will cause a change in the model structure. It is important to first allow component installed listeners to process this change, since there is no guarantee of the order in which listeners may be called. The solution is for the application listener to make the change using SwingUtilities.invokeLater(Runnable) so that any changes to UI rendering will be done post processing all the model listeners installed by the component.

As all events are delivered on the event dispatching thread, care must be taken in event processing. In particular, a long running task, such as network io or computational intensive processing, executed on the event dispatching thread blocks the event dispatching thread from dispatching any other events. While the event dispatching thread is blocked the application is completely unresponsive to user input. Refer to SwingWorker for the preferred way to do such processing when working with Swing.

More information on this topic can be found in the Swing tutorial, in particular the section on Concurrency in Swing.

Swing's Serialization policy

Warning: Serialized objects of any Swing class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package.

Related Documentation

For overviews, tutorials, examples, guides, and other documentation, please see:
Since:
1.2
  • Class
    Description
    This class provides default implementations for the JFC Action interface.
    Defines common behaviors for buttons and menu items.
    A base class for CellEditors, providing default implementations for the methods in the CellEditor interface except getCellEditorValue().
    The abstract definition for the data model that provides a List with its contents.
    This class provides the ChangeListener part of the SpinnerModel interface that should be suitable for most concrete SpinnerModel implementations.
    The Action interface provides a useful extension to the ActionListener interface in cases where the same functionality may be accessed by several controls.
    ActionMap provides mappings from Objects (called keys or Action names) to Actions.
    Factory class for vending standard Border objects.
    Defines the data model used by components like Sliders and ProgressBars.
    A lightweight container that uses a BoxLayout object as its layout manager.
    An implementation of a lightweight component that participates in layout but has no view.
    A layout manager that allows multiple components to be laid out either vertically or horizontally.
    This class is used to create a multiple-exclusion scope for a set of buttons.
    State model for buttons.
    This interface defines the methods any general editor should be able to implement.
    This class is inserted in between cell renderers and the components that use them.
    The editor component used for JComboBox components.
    A data model for a combo box.
    A ComponentInputMap is an InputMap associated with a particular JComponent.
    Graphics subclass supporting graphics debugging.
    A generic implementation of BoundedRangeModel.
    The default implementation of a Button component's data model.
    The default editor for table and tree cells.
    The default model for combo boxes.
    This is an implementation of the DesktopManager.
    This class has been obsoleted by the 1.4 focus APIs.
    Renders an item in a list.
    A subclass of DefaultListCellRenderer that implements UIResource.
    This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur.
    Default data model for list selections.
    An implementation of RowSorter that provides sorting and filtering around a grid-based data model.
    DefaultRowSorter.ModelWrapper is responsible for providing the data that gets sorted by DefaultRowSorter.
    A generic implementation of SingleSelectionModel.
    DesktopManager objects are owned by a JDesktopPane object.
    Drop modes, used to determine the method by which a component tracks and indicates a drop location during drag and drop.
    This class has been obsoleted by the 1.4 focus APIs.
    An image filter that "disables" an image by turning it into a grayscale image, and brightening the pixels in the image.
    GroupLayout is a LayoutManager that hierarchically groups components in order to position them in a Container.
    Enumeration of the possible ways ParallelGroup can align its children.
    A small fixed size picture, typically used to decorate components.
    An implementation of the Icon interface that paints Icons from Images.
    InputMap provides a binding between an input event (currently only KeyStrokes are used) and an Object.
    This class provides the validation mechanism for Swing components.
    A FocusTraversalPolicy which can optionally provide an algorithm for determining a JInternalFrame's initial Component.
    Deprecated, for removal: This API element is subject to removal in a future version.
    The Applet API is deprecated, no replacement.
    An implementation of a "push" button.
    An implementation of a check box -- an item that can be selected or deselected, and which displays its state to the user.
    A menu item that can be selected or deselected.
    JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color.
    A component that combines a button or editable field and a drop-down list.
    The interface that defines a KeySelectionManager.
    The base class for all Swing components except top-level containers.
    A container used to create a multiple-document interface or a virtual desktop.
    The main class for creating a dialog window.
    A text component to edit various kinds of content.
    JFileChooser provides a simple mechanism for the user to choose a file.
    JFormattedTextField extends JTextField adding support for formatting arbitrary values, as well as retrieving a particular object once the user has edited the text.
    Instances of AbstractFormatter are used by JFormattedTextField to handle the conversion both from an Object to a String, and back from a String to an Object.
    Instances of AbstractFormatterFactory are used by JFormattedTextField to obtain instances of AbstractFormatter which in turn are used to format values.
    An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture.
    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.
    This component represents an iconified version of a JInternalFrame.
    A display area for a short text string or an image, or both.
    JLayer<V extends Component>
    JLayer is a universal decorator for Swing components which enables you to implement various advanced painting effects as well as receive notifications of all AWTEvents generated within its borders.
    JLayeredPane adds depth to a JFC/Swing container, allowing components to overlap each other when needed.
    A component that displays a list of objects and allows the user to select one or more items.
    A subclass of TransferHandler.DropLocation representing a drop location for a JList.
    An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar.
    An implementation of a menu bar.
    An implementation of an item in a menu.
    JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.