package
javax.swing


Show All Login
Java SE 6
  
Details

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 {@code JButton} notifies all ActionListeners added to the {@code 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 {@code 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:

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(args));
    }
}
Or:
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 {@code 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.

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 How to Use Threads.

Related Documentation

For overviews, tutorials, examples, guides, and other documentation, please see:

serialexclude

Subpackages
javax.swing.border
javax.swing.colorchooser
javax.swing.event
javax.swing.filechooser
javax.swing.plaf
javax.swing.table
javax.swing.text
javax.swing.tree
javax.swing.undo

Interfaces
Action
BoundedRangeModel
ButtonModel
CellEditor
ComboBoxEditor
ComboBoxModel
DesktopManager
Icon
  .KeySelectionManager
ListCellRenderer
ListModel
ListSelectionModel
MenuElement
MutableComboBoxModel
Renderer
RootPaneContainer
Scrollable
ScrollPaneConstants
SingleSelectionModel
SpinnerModel
SwingConstants
  .ActiveValue
  .LazyValue
WindowConstants

Enumerations
DropMode
  .Alignment
  .PrintMode
  .ComponentPlacement
  .ComparisonType
SortOrder
  .StateValue

Exceptions
UnsupportedLookAndFeelException

Classes
AbstractAction
AbstractButton
  .AccessibleAbstractButton
  .ButtonChangeListener
AbstractCellEditor
AbstractListModel
AbstractSpinnerModel
ActionMap
BorderFactory
Box
  .AccessibleBox
  .Filler
  .AccessibleBoxFiller
BoxLayout
ButtonGroup
CellRendererPane
  .AccessibleCellRendererPane
ComponentInputMap
DebugGraphics
DefaultBoundedRangeModel
DefaultButtonModel
DefaultCellEditor
  .EditorDelegate
DefaultComboBoxModel
DefaultDesktopManager
DefaultFocusManager
DefaultListCellRenderer
  .UIResource
DefaultListModel
DefaultListSelectionModel
DefaultRowSorter
  .ModelWrapper
DefaultSingleSelectionModel
FocusManager
GrayFilter

GroupLayout
  .Group
  .ParallelGroup
  .SequentialGroup
ImageIcon
  .AccessibleImageIcon
InputMap
InputVerifier
InternalFrameFocusTraversalPolicy
JApplet
  .AccessibleJApplet
JButton
  .AccessibleJButton
JCheckBox
  .AccessibleJCheckBox
JCheckBoxMenuItem
  .AccessibleJCheckBoxMenuItem
JColorChooser
  .AccessibleJColorChooser
JComboBox
  .AccessibleJComboBox
JComponent
  .AccessibleJComponent
  .AccessibleContainerHandler
  .AccessibleFocusHandler
JDesktopPane
  .AccessibleJDesktopPane
JDialog
  .AccessibleJDialog
JEditorPane
  .AccessibleJEditorPane
  .AccessibleJEditorPaneHTML
  .JEditorPaneAccessibleHypertextSupport
  .HTMLLink
JFileChooser
  .AccessibleJFileChooser
JFormattedTextField
  .AbstractFormatter
  .AbstractFormatterFactory
JFrame
  .AccessibleJFrame
JInternalFrame
  .AccessibleJInternalFrame
  .JDesktopIcon
  .AccessibleJDesktopIcon
JLabel
  .AccessibleJLabel
JLayeredPane
  .AccessibleJLayeredPane
JList
  .AccessibleJList
  .AccessibleJListChild
  .DropLocation
JMenu
  .AccessibleJMenu
  .WinListener
JMenuBar
  .AccessibleJMenuBar
JMenuItem
  .AccessibleJMenuItem
JOptionPane
  .AccessibleJOptionPane
JPanel
  .AccessibleJPanel
JPasswordField
  .AccessibleJPasswordField
JPopupMenu
  .AccessibleJPopupMenu
  .Separator
JProgressBar
  .AccessibleJProgressBar
JRadioButton
  .AccessibleJRadioButton
JRadioButtonMenuItem
  .AccessibleJRadioButtonMenuItem
JRootPane
  .AccessibleJRootPane
  .RootLayout
JScrollBar
  .AccessibleJScrollBar
JScrollPane
  .AccessibleJScrollPane
  .ScrollBar
JSeparator
  .AccessibleJSeparator
JSlider
  .AccessibleJSlider
JSpinner
  .AccessibleJSpinner
  .DateEditor
  .DefaultEditor
  .ListEditor
  .NumberEditor
JSplitPane
  .AccessibleJSplitPane
JTabbedPane
  .AccessibleJTabbedPane
  .ModelListener
JTable
  .AccessibleJTable
  .AccessibleJTableCell
  .AccessibleJTableModelChange
  .DropLocation
JTextArea
  .AccessibleJTextArea
JTextField
  .AccessibleJTextField
JTextPane
JToggleButton
  .AccessibleJToggleButton
  .ToggleButtonModel
JToolBar
  .AccessibleJToolBar
  .Separator
JToolTip
  .AccessibleJToolTip
JTree
  .AccessibleJTree
  .AccessibleJTreeNode
  .DropLocation
  .DynamicUtilTreeNode
  .EmptySelectionModel
  .TreeModelHandler
  .TreeSelectionRedirector
JViewport
  .AccessibleJViewport
  .ViewListener
JWindow
  .AccessibleJWindow
KeyStroke
LayoutFocusTraversalPolicy
LayoutStyle
LookAndFeel
MenuSelectionManager
OverlayLayout
Popup
PopupFactory
ProgressMonitor
  .AccessibleProgressMonitor
ProgressMonitorInputStream
RepaintManager
RowFilter
  .Entry
RowSorter
  .SortKey
ScrollPaneLayout
  .UIResource
SizeRequirements
SizeSequence
SortingFocusTraversalPolicy
SpinnerDateModel
SpinnerListModel
SpinnerNumberModel
Spring
SpringLayout
  .Constraints
SwingUtilities
SwingWorker
Timer
ToolTipManager
  .insideTimerAction
  .outsideTimerAction
  .stillInsideTimerAction
TransferHandler
  .DropLocation
  .TransferSupport
UIDefaults
  .LazyInputMap
  .ProxyLazyValue
UIManager
  .LookAndFeelInfo
ViewportLayout