epubjs
Version:
Render ePub documents in the browser, across many devices
214 lines (212 loc) • 36.4 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Event Summary</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Event Summary"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-16-SECT-3"/>Event Summary</h1></div></div></div><p><a id="idx10951" class="indexterm"/>Tables <a class="xref" href="ch16s03.html#learnjava3-CHP-16-TABLE-1" title="Table 16-1. Swing component and container events">16-1</a> and <a class="xref" href="ch16s03.html#learnjava3-CHP-16-TABLE-2" title="Table 16-2. Component-specific swing events">16-2</a>
summarize commonly used Swing events, which Swing components fire them,
and the methods of the listener interfaces that receive them. The events
and listeners are divided between the packages <a id="I_indexterm16_id790274" class="indexterm"/><code class="literal">java.awt.event</code> and
<a id="I_indexterm16_id790285" class="indexterm"/><code class="literal">javax.swing.event</code>.</p><div class="table"><a id="learnjava3-CHP-16-TABLE-1"/><p class="title">Table 16-1. Swing component and container events</p><div class="table-contents"><table summary="Swing component and container events" width="100%" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col/><col/><col/><col/></colgroup><thead><tr><th style="text-align: left" valign="top"><p>Event</p></th><th style="text-align: left" valign="top"><p>Fired by</p></th><th style="text-align: left" valign="top"><p>Listener
interface</p></th><th style="text-align: left" valign="top"><p>Handler
methods</p></th></tr></thead><tbody><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790374" class="indexterm"/> <code class="literal">java.awt.event.ComponentEvent</code>
</p></td><td style="text-align: left" valign="top"><p>All
components</p></td><td style="text-align: left" valign="top"><p> <code class="literal">ComponentListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790414" class="indexterm"/> <code class="literal">componentResized()</code> </p> <p>
<a id="I_indexterm16_id790429" class="indexterm"/> <code class="literal">componentMoved()</code>
</p> <p> <a id="I_indexterm16_id790444" class="indexterm"/> <code class="literal">componentShown()</code>
</p> <p> <a id="I_indexterm16_id790459" class="indexterm"/> <code class="literal">componentHidden()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790480" class="indexterm"/> <code class="literal">java.awt.event.FocusEvent</code>
</p></td><td style="text-align: left" valign="top"><p>All
components</p></td><td style="text-align: left" valign="top"><p> <code class="literal">FocusListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790519" class="indexterm"/> <code class="literal">focusGained()</code>
</p> <p> <a id="I_indexterm16_id790534" class="indexterm"/> <code class="literal">focusLost()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790554" class="indexterm"/> <code class="literal">java.awt.event.KeyEvent</code> </p></td><td style="text-align: left" valign="top"><p>All
components</p></td><td style="text-align: left" valign="top"><p> <code class="literal">KeyListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790593" class="indexterm"/> <code class="literal">keyTyped()</code>
</p> <p> <a id="I_indexterm16_id790608" class="indexterm"/> <code class="literal">keyPressed()</code>
</p> <p> <a id="I_indexterm16_id790623" class="indexterm"/> <code class="literal">keyReleased()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790643" class="indexterm"/> <code class="literal">java.awt.event.MouseEvent</code>
</p></td><td style="text-align: left" valign="top"><p>All
components</p></td><td style="text-align: left" valign="top"><p> <code class="literal">MouseListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790682" class="indexterm"/> <code class="literal">mouseClicked()</code>
</p> <p> <a id="I_indexterm16_id790697" class="indexterm"/> <code class="literal">mousePressed()</code>
</p> <p> <a id="I_indexterm16_id790712" class="indexterm"/> <code class="literal">mouseReleased()</code>
</p> <p> <a id="I_indexterm16_id790727" class="indexterm"/> <code class="literal">mouseEntered()</code>
</p> <p> <a id="I_indexterm16_id790742" class="indexterm"/> <code class="literal">mouseExited()</code>
</p></td></tr><tr><td style=""> </td><td style=""> </td><td style="text-align: left" valign="top"><p> <code class="literal">MouseMotionListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790777" class="indexterm"/> <code class="literal">mouseDragged()</code>
</p> <p> <a id="I_indexterm16_id790792" class="indexterm"/> <code class="literal">mouseMoved()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790812" class="indexterm"/> <code class="literal">java.awt.event.ContainerEvent</code>
</p></td><td style="text-align: left" valign="top"><p>All
containers</p></td><td style="text-align: left" valign="top"><p> <code class="literal">ContainerListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id790851" class="indexterm"/> <code class="literal">componentAdded()</code>
</p> <p> <a id="I_indexterm16_id790866" class="indexterm"/> <code class="literal">componentRemoved()</code> </p></td></tr></tbody></table></div></div><div class="table"><a id="learnjava3-CHP-16-TABLE-2"/><p class="title">Table 16-2. Component-specific swing events</p><div class="table-contents"><table summary="Component-specific swing events" width="100%" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col width="10%"/><col width="10%"/><col width="10%"/><col width="10%"/></colgroup><thead><tr><th style="text-align: left" valign="top"><p>Event</p></th><th style="text-align: left" valign="top"><p>Fired by</p></th><th style="text-align: left" valign="top"><p>Listener
interface</p></th><th style="text-align: left" valign="top"><p>Handler
method</p></th></tr></thead><tbody><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id790964" class="indexterm"/><code class="literal">java.awt.event.ActionEvent</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id790981" class="indexterm"/><code class="literal">JButton</code> </p>
<p><a id="I_indexterm16_id790994" class="indexterm"/><code class="literal">JCheckBoxMenuItem</code>
</p> <p> <code class="literal">JComboBox</code>
</p> <p><a id="I_indexterm16_id791015" class="indexterm"/><code class="literal">JFileChooser</code>
</p> <p><a id="I_indexterm16_id791028" class="indexterm"/><code class="literal">JList</code> </p>
<p><a id="I_indexterm16_id791040" class="indexterm"/><code class="literal">JRadioButtonMenuItem</code> </p>
<p><a id="I_indexterm16_id791054" class="indexterm"/><code class="literal">JTextField</code>
</p> <p><a id="I_indexterm16_id791066" class="indexterm"/><code class="literal">JToggleButton</code>
</p></td><td style="text-align: left" valign="top"><p><code class="literal">Action</code><code class="literal">Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791099" class="indexterm"/> <code class="literal">actionPerformed()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791118" class="indexterm"/><code class="literal">java.awt.event.</code><code class="literal">AdjustmentEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791143" class="indexterm"/><code class="literal">JScrollBar</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">Adjustment-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791172" class="indexterm"/><code class="literal">adjustmentValue-Changed()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791191" class="indexterm"/> <code class="literal">javax.swing.event.CaretEvent</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791209" class="indexterm"/><code class="literal">JTextComponent</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">CaretListener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791238" class="indexterm"/><code class="literal">caretUpdate()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791256" class="indexterm"/><code class="literal">javax.swing.event.</code><code class="literal">HyperlinkEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791281" class="indexterm"/><code class="literal">JEditorPane</code>
</p> <p><a id="I_indexterm16_id791294" class="indexterm"/><code class="literal">JTextPane</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">Hyperlink-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791323" class="indexterm"/><code class="literal">hyperlinkUpdate()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791342" class="indexterm"/><code class="literal">java.awt.event.Internal</code><code class="literal">FrameEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791367" class="indexterm"/><code class="literal">JInternalFrame</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">InternalFrame-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791396" class="indexterm"/><code class="literal">internalFrame-Activated()</code> </p>
<p><a id="I_indexterm16_id791410" class="indexterm"/><code class="literal">internalFrame-Closed()</code> </p>
<p><a id="I_indexterm16_id791423" class="indexterm"/><code class="literal">internalFrame-Closing()</code> </p>
<p><a id="I_indexterm16_id791436" class="indexterm"/><code class="literal">internalFrame-Deactivated()</code> </p>
<p> <a id="I_indexterm16_id791451" class="indexterm"/> <code class="literal">internalFrame-Deiconified()</code> </p>
<p> <a id="I_indexterm16_id791466" class="indexterm"/> <code class="literal">internalFrame-Iconified()</code> </p> <p>
<a id="I_indexterm16_id791482" class="indexterm"/> <code class="literal">internalFrame-Opened()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791501" class="indexterm"/><code class="literal">java.awt.event.ItemEvent</code> </p></td><td style="text-align: left" valign="top"><p> <code class="literal">JCheckBoxMenuItem</code> </p> <p>
<a id="I_indexterm16_id791528" class="indexterm"/><code class="literal">JComboBox</code> </p>
<p> <code class="literal">JRadioButtonMenuItem</code>
</p> <p> <code class="literal">JToggleButton</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">ItemListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id791575" class="indexterm"/> <code class="literal">itemStateChanged()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791594" class="indexterm"/><code class="literal">javax.swing.event.List</code><code class="literal">DataEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791619" class="indexterm"/><code class="literal">ListModel</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">ListDataListener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791648" class="indexterm"/><code class="literal">contentsChanged()</code>
</p> <p><a id="I_indexterm16_id791662" class="indexterm"/><code class="literal">intervalAdded()</code>
</p> <p><a id="I_indexterm16_id791674" class="indexterm"/><code class="literal">intervalRemoved()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791693" class="indexterm"/><code class="literal">javax.swing.event.List</code><code class="literal">SelectionEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791718" class="indexterm"/><code class="literal">JList</code> </p>
<p><a id="I_indexterm16_id791730" class="indexterm"/><code class="literal">ListSelectionModel</code> </p></td><td style="text-align: left" valign="top"><p> <code class="literal">ListSelection-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791760" class="indexterm"/><code class="literal">valueChanged()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id791779" class="indexterm"/><code class="literal">javax.swing.event.MenuEvent</code></p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791795" class="indexterm"/><code class="literal">JMenu</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">MenuListener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791824" class="indexterm"/><code class="literal">menuCanceled()</code>
</p> <p><a id="I_indexterm16_id791837" class="indexterm"/><code class="literal">menuDeselected()</code>
</p> <p><a id="I_indexterm16_id791850" class="indexterm"/><code class="literal">menuSelected()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791868" class="indexterm"/><code class="literal">javax.swing.event.PopupMenuEvent</code></p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791884" class="indexterm"/><code class="literal">JPopupMenu</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">PopupMenu-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791913" class="indexterm"/><code class="literal">popupMenuCanceled()</code> </p> <p>
<a id="I_indexterm16_id791928" class="indexterm"/><code class="literal">popupMenuWill-BecomeInvisible()</code> </p>
<p><a id="I_indexterm16_id791941" class="indexterm"/><code class="literal">popupMenuWill-BecomeVisible()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791959" class="indexterm"/><code class="literal">javax.swing.event.MenuKeyEvent</code></p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id791976" class="indexterm"/><code class="literal">JMenuItem</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">MenuKeyListener</code> </p></td><td style="text-align: left" valign="top"><p> <a id="I_indexterm16_id792006" class="indexterm"/> <code class="literal">menuKeyPressed()</code>
</p> <p> <a id="I_indexterm16_id792021" class="indexterm"/> <code class="literal">menuKeyReleased()</code> </p>
<p><a id="I_indexterm16_id792035" class="indexterm"/><code class="literal">menuKeyTyped()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792053" class="indexterm"/><code class="literal">javax.swing.event.MenuDrag</code><code class="literal">MouseEvent</code></p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792077" class="indexterm"/><code class="literal">JMenuItem</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">MenuDragMouse-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792106" class="indexterm"/><code class="literal">menuDragMouse-Dragged()</code> </p>
<p><a id="I_indexterm16_id792120" class="indexterm"/><code class="literal">menuDragMouse-Entered()</code> </p>
<p><a id="I_indexterm16_id792133" class="indexterm"/><code class="literal">menuDragMouse-Exited()</code> </p>
<p><a id="I_indexterm16_id792146" class="indexterm"/><code class="literal">menuDragMouse-Released()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792164" class="indexterm"/><code class="literal">javax.swing.event.TableColumn</code><code class="literal">ModelEvent</code> </p></td><td style="text-align: left" valign="top"><p> <code class="literal">TableColumnModel</code> <sup>[<a id="id946438" href="#ftn.id946438" class="footnote">a</a>]</sup> </p></td><td style="text-align: left" valign="top"><p> <code class="literal">TableColumn-ModelListener</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792240" class="indexterm"/><code class="literal">columnAdded()</code>
</p> <p><a id="I_indexterm16_id792252" class="indexterm"/><code class="literal">columnMargin-Changed()</code> </p>
<p><a id="I_indexterm16_id792266" class="indexterm"/><code class="literal">columnMoved()</code>
</p> <p><a id="I_indexterm16_id792278" class="indexterm"/><code class="literal">columnRemoved()</code>
</p> <p><a id="I_indexterm16_id792291" class="indexterm"/><code class="literal">columnSelection-Changed()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792310" class="indexterm"/><code class="literal">javax.swing.event.TableModelEvent</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792327" class="indexterm"/><code class="literal">TableModel</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">TableModel-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792356" class="indexterm"/><code class="literal">tableChanged()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792374" class="indexterm"/><code class="literal">javax.swing.event.Tree</code><code class="literal">ExpansionEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792400" class="indexterm"/><code class="literal">Jtree</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">TreeExpansion-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792429" class="indexterm"/> <code class="literal">treeCollapsed()</code>
</p> <p><a id="I_indexterm16_id792443" class="indexterm"/><code class="literal">treeExpanded()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792460" class="indexterm"/><code class="literal">javax.swing.event.TreeModelEvent</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792478" class="indexterm"/><code class="literal">TreeModel</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">TreeModel-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792507" class="indexterm"/><code class="literal">treeNodesChanged()</code> </p>
<p><a id="I_indexterm16_id792520" class="indexterm"/><code class="literal">treeNodesInserted()</code> </p>
<p><a id="I_indexterm16_id792533" class="indexterm"/><code class="literal">treeNodesRemoved()</code> </p>
<p><a id="I_indexterm16_id792546" class="indexterm"/><code class="literal">treeStructure-Changed()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792565" class="indexterm"/><code class="literal">javax.swing.event.Tree</code><code class="literal">SelectionEvent</code></p></td><td style="text-align: left" valign="top"><p> <code class="literal">JTree</code> </p> <p><a id="I_indexterm16_id792597" class="indexterm"/><code class="literal">TreeSelectionModel</code> </p></td><td style="text-align: left" valign="top"><p> <code class="literal">TreeSelection-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792627" class="indexterm"/><code class="literal">valueChanged()</code>
</p></td></tr><tr><td style="text-align: left" valign="top"><p> <code class="literal">javax.swing.event.Undoable</code><code class="literal">EditEvent</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792666" class="indexterm"/><code class="literal">javax.swing.text.Document</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">UndoableEdit-Listener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792696" class="indexterm"/><code class="literal">undoableEdit-Happened()</code> </p></td></tr><tr><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792714" class="indexterm"/><code class="literal">java.awt.event.WindowEvent</code>
</p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792731" class="indexterm"/><code class="literal">JDialog</code> </p>
<p><a id="I_indexterm16_id792744" class="indexterm"/><code class="literal">JFrame</code> </p>
<p><a id="I_indexterm16_id792756" class="indexterm"/><code class="literal">JWindow</code>
</p></td><td style="text-align: left" valign="top"><p> <code class="literal">WindowListener</code> </p></td><td style="text-align: left" valign="top"><p><a id="I_indexterm16_id792785" class="indexterm"/><code class="literal">windowOpened()</code>
</p> <p><a id="I_indexterm16_id792798" class="indexterm"/><code class="literal">windowClosing()</code>
</p> <p><a id="I_indexterm16_id792811" class="indexterm"/><code class="literal">windowClosed()</code>
</p> <p><a id="I_indexterm16_id792824" class="indexterm"/><code class="literal">windowIconified()</code>
</p> <p><a id="I_indexterm16_id792837" class="indexterm"/><code class="literal">windowDeiconified()</code> </p>
<p><a id="I_indexterm16_id792850" class="indexterm"/><code class="literal">windowActivated()</code>
</p> <p><a id="I_indexterm16_id792863" class="indexterm"/><code class="literal">windowDeactivated()</code> </p></td></tr></tbody><tbody class="footnotes"><tr><td colspan="4"><div class="footnote"><p><sup>[<a id="ftn.id946438" href="#id946438" class="para">a</a>] </sup>The <code class="literal">TableColumnModel</code>
class breaks with convention in the names of the methods that
add listeners. They are <code class="literal">addColumnModelListener()</code> and
<code class="literal">removeColumnModelListener()</code>.</p></div></td></tr></tbody></table></div></div><p>In Swing, a component’s model and view are distinct. Strictly
speaking, components don’t fire events; models do. When you press a
<code class="literal">JButton</code>, for example, it’s actually the
button’s data model that fires an <code class="literal">ActionEvent</code>, not the button itself. But <code class="literal">JButton</code> has a convenience method for registering
<code class="literal">ActionListener</code>s; this method passes its
argument through to register the listener with the button model. In many
cases (as with <code class="literal">JButton</code>s), you don’t
have to deal with the data model separately from the view, so we can speak
loosely of the component itself firing the events. <code class="literal">InputEvent</code>s are, of course, generated by the
native input system and fired for the appropriate component, although the
listener responds as though they’re generated by the component.</p><div class="sect2" title="Adapter Classes"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-16-SECT-3.1"/>Adapter Classes</h2></div></div></div><p><a id="idx10911" class="indexterm"/>It’s not ideal to have your application components
implement a bunch of listener interfaces and receive events directly.
Sometimes it’s not even possible. Being an event receiver forces you to
modify or subclass your objects to implement the appropriate event
listener interfaces and add the code necessary to handle the events. And
because we are talking about Swing events here, a more subtle issue is
that you would be, of necessity, building GUI logic into parts of your
application that shouldn’t have to know anything about the GUI. Let’s
look at an example.</p><p>In <a class="xref" href="ch16s03.html#learnjava3-CHP-16-FIG-4" title="Figure 16-4. Implementing the ActionListener interface directly">Figure 16-4</a>, we drew the plans
for our Vegomatic food processor. We made our <code class="literal">Vegomatic</code> object implement the <code class="literal">ActionListener</code> interface so that it can
receive events directly from the three <code class="literal">JButton</code> components: <code class="literal">Chop</code>, <code class="literal">Puree</code>, and <code class="literal">Frappe</code>. The problem is that our <code class="literal">Vegomatic</code> object now has to know more than how
to mangle food. It also has to be aware that it is driven by three
controls—specifically, buttons that send action commands—and be aware of
which methods it should invoke for those commands. Our boxes labeling
the GUI and application code overlap in an unwholesome way. If the
marketing people should later want to add or remove buttons or perhaps
just change the names, we have to be careful. We may have to modify the
logic in our <code class="literal">Vegomatic</code> object. All is
not well.</p><p>An alternative is to place an adapter class between our event
source and receiver. An <span class="emphasis"><em>adapter</em></span> is a simple object
whose sole purpose is to map an incoming event to an outgoing
method.</p><p><a class="xref" href="ch16s03.html#learnjava3-CHP-16-FIG-5" title="Figure 16-5. Implementing the ActionListener interface using adapter classes">Figure 16-5</a> shows a better design
that uses three adapter classes, one for each button. The implementation
of the first adapter might look like:</p><a id="I_16_tt994"/><pre class="programlisting"> <code class="kd">class</code> <code class="nc">VegomaticAdapter1</code> <code class="kd">implements</code> <code class="n">ActionListener</code> <code class="o">{</code>
<code class="n">Vegomatic</code> <code class="n">vegomatic</code><code class="o">;</code>
<code class="n">VegomaticAdapter1</code> <code class="o">(</code> <code class="n">Vegomatic</code> <code class="n">vegomatic</code> <code class="o">)</code> <code class="o">{</code>
<code class="k">this</code><code class="o">.</code><code class="na">vegomatic</code> <code class="o">=</code> <code class="n">vegomatic</code><code class="o">;</code>
<code class="o">}</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">actionPerformed</code><code class="o">(</code> <code class="n">ActionEvent</code> <code class="n">e</code> <code class="o">)</code> <code class="o">{</code>
<code class="n">vegomatic</code><code class="o">.</code><code class="na">chopFood</code><code class="o">();</code>
<code class="o">}</code>
<code class="o">}</code></pre><div class="figure"><a id="learnjava3-CHP-16-FIG-4"/><div class="figure-contents"><div class="mediaobject"><a id="I_16_tt995"/><img src="httpatomoreillycomsourceoreillyimages1707651.png" alt="Implementing the ActionListener interface directly"/></div></div><p class="title">Figure 16-4. Implementing the ActionListener interface directly</p></div><div class="figure"><a id="learnjava3-CHP-16-FIG-5"/><div class="figure-contents"><div class="mediaobject"><a id="I_16_tt996"/><img src="httpatomoreillycomsourceoreillyimages1707652.png" alt="Implementing the ActionListener interface using adapter classes"/></div></div><p class="title">Figure 16-5. Implementing the ActionListener interface using adapter
classes</p></div><p>So somewhere in the code where we build our GUI, we could register
our listener like this:</p><a id="I_16_tt997"/><pre class="programlisting"> <code class="n">Vegomatic</code> <code class="n">theVegomatic</code> <code class="o">=</code> <code class="o">...;</code>
<code class="n">Button</code> <code class="n">chopButton</code> <code class="o">=</code> <code class="o">...;</code>
<code class="c1">// make the hookup</code>
<code class="n">chopButton</code><code class="o">.</code><code class="na">addActionListener</code><code class="o">(</code> <code class="k">new</code> <code class="n">VegomaticAdapter1</code><code class="o">(</code><code class="n">theVegomatic</code><code class="o">)</code> <code class="o">);</code></pre><p>Instead of registering itself (<code class="literal">this</code>) as the <code class="literal">Button</code>’s listener, the adapter registers the
<code class="literal">Vegomatic</code> object (<code class="literal">theVegomatic</code>). In this way, the adapter acts
as an intermediary, hooking up an event source (the button) with an
event receiver (the virtual chopper).</p><p>We have completely separated the messiness of our GUI from the
application code. However, we have added three new classes to our
application, none of which does very much. Is that good? It depends on
your vantage point.</p><p>Under different circumstances, our buttons may have been able to
share a common adapter class that was simply instantiated with different
parameters. Various tradeoffs can be made between size, efficiency, and
elegance of code. Adapter classes will often be generated automatically
by development tools. The way we’ve named our adapter classes <code class="literal">VegomaticAdapter1</code>, <code class="literal">VegomaticAdapter2</code>, and <code class="literal">VegomaticAdapter3</code> hints at this. More often,
when handcoding, you’ll use an anonymous inner class, as we’ll see in
the next section. At the other extreme, we can forsake Java’s strong
typing and use the Reflection API to create a completely dynamic hookup
between an event source and its listener.<a id="I_indexterm16_id793142" class="indexterm"/></p></div><div class="sect2" title="Dummy Adapters"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-16-SECT-3.2"/>Dummy Adapters</h2></div></div></div><p><a id="idx10928" class="indexterm"/>Many listener interfaces contain more than one event
handler method. Unfortunately, this means that to register yourself as
interested in any one of those events, you must implement the whole
listener interface. To accomplish this, you might find yourself typing
dummy “stubbed-out” methods to complete the interface. There is nothing
wrong with this, but it is a bit tedious. To save you some trouble, AWT
and Swing provide some helper classes that implement these dummy methods
for you. For each of the most common listener interfaces containing more
than one method, there is an adapter class containing the stubbed
methods. You can use the adapter class as a base class for your own
adapters. When you need a class to patch together your event source and
listener, you can subclass the adapter and override only the methods you
want.</p><p>For example, the <code class="literal">MouseAdapter</code>
class implements the <code class="literal">MouseListener</code>
interface and provides the following minimalist implementation:</p><a id="I_16_tt998"/><pre class="programlisting"> <code class="kd">public</code> <code class="kt">void</code> <code class="nf">mouseClicked</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{};</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">mousePressed</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{};</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">mouseReleased</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{};</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">mouseEntered</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{};</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">mouseExited</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{};</code></pre><p>This isn’t a tremendous time saver; it’s simply a bit of sugar.
The primary advantage comes into play when we use the <code class="literal">MouseAdapter</code> as the base for our own adapter
in an anonymous inner class. For example, suppose we want to catch a
<a id="I_indexterm16_id793211" class="indexterm"/><code class="literal">mousePressed()</code> event in
some component and blow up a building. We can use the following to make
the hookup:</p><a id="I_16_tt999"/><pre class="programlisting"> <code class="n">someComponent</code><code class="o">.</code><code class="na">addMouseListener</code><code class="o">(</code> <code class="k">new</code> <code class="n">MouseAdapter</code><code class="o">()</code> <code class="o">{</code>
<code class="kd">public</code> <code class="kt">void</code> <code class="nf">MousePressed</code><code class="o">(</code><code class="n">MouseEvent</code> <code class="n">e</code><code class="o">)</code> <code class="o">{</code>
<code class="n">building</code><code class="o">.</code><code class="na">blowUp</code><code class="o">();</code>
<code class="o">}</code>
<code class="o">}</code> <code class="o">);</code></pre><p>We’ve taken artistic liberties with the formatting, but it’s
pretty readable. Moreover, we’ve avoided creating stub methods for the
four unused event handler methods. Writing adapters is common enough
that it’s nice to avoid typing those extra few lines and perhaps stave
off the onset of carpal tunnel syndrome for a few more hours. Remember
that any time you use an inner class, the compiler is generating a class
for you, so the messiness you’ve saved in your source still exists in
the output classes.<a id="I_indexterm16_id793239" class="indexterm"/></p></div></div></body></html>