Writing Event Listeners |
This section summarizes what events can be generated by which Swing components, focusing on the events that typical programs handle. Events generated by Swing components fall into three categories:
- Commonly Handled Events that All Swing Components Can Generate
- Other Commonly Handled Events
- Events Not Commonly Handled
Commonly Handled Events that All Swing Components Can Generate
Because all Swing components descend from the AWTComponent
class, all Swing components support the following AWT-defined events:
- Component Events
- Notify listeners of changes in the component's size, position, or visibility.
- Focus Events
- Notify listeners that the component gained or lost the ability to receive keyboard input.
- Key Events
- Notify listeners of key presses; generated only by the component that has the current keyboard focus.
- Mouse Events
- Notify listeners of mouse clicks and the user moving the cursor into or out of the component's drawing area.
- Mouse Motion Events
- Notify listeners of changes in the cursor's position over the component.
Although all Swing components descend from the AWT
Container
class, many of them aren't used as containers. So technically speaking any Swing component can generate container events, which notify listeners that a component has been added to or removed from the container. However, realistically speaking only containers such as panels, frames, and so on, generate container events.Other Commonly Handled Events
The following table lists commonly handled events that various Swing components generate.
Note: An asterisk '*' in a column header indicates an AWT-defined event. All other event types in the table are defined injavax.swing.event
.
Swing Component Types of Events Generated and Commonly Handled action
*caret
change
document
internal
frameitem
*list
selectionundoable
editwindow
*ColorSelectionModel
(JColorChooser
's default selection model.)
Note: This is not aJComponent
subclass!X Document
(JTextComponent
's data model.)
Note: This is not aJComponent
subclass!X X JButton
X X X JCheckBox
X X X JComboBox
X X JDialog
X JEditorPane
X JFileChooser
X JFrame
X JInternalFrame
X JList
X JMenuItem
X X X JOptionpane
X JPasswordField
X X JProgressBar
X JRadioButton
X X X JSlider
X JTabbedPane
X JTextArea
X JTextComponent
X JTextField
X X JTextPane
X JToggleButton
X X X JViewport
X ListSelectionModel
(JList
's default selection model.)
Note: This is not aJComponent
subclass!X Timer
Note: This is not aJComponent
subclass!X Events Not Commonly Handled
For the record, this section lists other events that Swing components can generate but that typical programs don't need to handle.All components that inherit from
JComponent
can generate the events described in the following list.The following table lists all the other events defined in
- Ancestor Events
- A component generates ancestor events when one of its containment ancestors is added to or removed from a container, hidden, made visible, or moved. This event type is an implementation detail and can generally be ignored.
- Property Change Events
- Defined in
java.beans
Swing components generate this type of event because they are JavaBeans compliant. Beans use property change events to implement bound properties.- Vetoable Change Events
- Defined in
java.beans
Swing components generate this type of event because they are JavaBeans compliant. Beans use vetoable change events to implement constrained properties.javax.swing.event
not previously mentioned. [PENDING: make these api links?] [PENDING: this tutorial *should* cover the table and tree events]
Cell Editor Events Menu Key Events Tree Expansion Events Hyperlink Events Menu Events Tree Model Events List Data Events Popup Menu Events Tree Selection Events Menu Drag Mouse Events Table Model Events Tree Will Expand Events
Writing Event Listeners |