Java Reference
In-Depth Information
Java uses many different listeners, depending on how the user triggers the
event. Table 8-8 displays some of the user events and listener types.
Table 8-8
User Events and Listener Types
EVENT
LISTENER TYPE
User clicks a button, presses the ENTER key while
ActionListener
typing in a text field, or chooses a menu item.
User closes a frame (main window).
WindowListener
User presses a mouse button while the mouse pointer
MouseListener
is positioned on a component.
User moves the mouse over a component.
MouseMotionListener
Component becomes visible.
ComponentListener
Component gets the keyboard focus.
FocusListener
Table or list selection changes.
ListSelectionListener
When the user clicks the Close button, line 161 causes a showConfirmDialog
box to display with the buttons Yes and No. The showConfirmDialog() method
can display one of several common dialog boxes with the chosen Look and Feel.
The showConfirmDialog() method may display different arrangements of but-
tons, as listed in Table 8-9. The return values, represented internally as integers,
indicate how the user has interacted with the dialog box, or which button he/she
has clicked.
Table 8-9 Options of the showConfirmDialog Box
BUTTON OPTIONS
RETURN OPTIONS
DEFAULT_OPTION
YES_OPTION
YES_NO_OPTION
NO_OPTION
YES_NO_CANCEL_OPTION
CANCEL_OPTION
OK_CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
Line 161 (Figure 8-21 on the previous page) specifies the
YES_NO_OPTION, which causes the confirm dialog box to display the Yes and
No buttons. Line 162 tests against the YES_OPTION to verify that the user
clicked the Yes button. Option buttons are not limited to the displayed set of
option buttons. Any button can be selected by using the options parameter.
In line 161, the return value of the user's choice is stored in a variable named
answer. Then, line 162 compares the variable, answer, to the JOptionPane,
YES_OPTION . If the user actually clicked the Yes button, the program exits with
no error, resulting in the file being closed. The confirm dialog box is Box 3 from
Table 8-2 on page 510.
 
Search WWH ::




Custom Search