Java Reference
In-Depth Information
To Code the actionPerformed() Method
1. Enter the code as shown in Figure 7-26 on pages 461 and 462.
TextPad displays the code for the actionPerformed() method (Figure 7-27).
code for Exit
command
code for Insert
New DVD
command
code to call the
search method
with appropriate
data
FIGURE 7-27
It can be difficult to know whether to use the getActionCommand()
method or the getSource() method to ascertain what item was clicked. The
getActionCommand() method is preferred for objects that may have multiple
states, such as menus and buttons. For example, a Show/Hide button sometimes
might display the word, Show, and other times it might display the word, Hide.
The getSource() method would indicate that the button was clicked, but
would not indicate the caption of the button at the time of the click. The
getActionCommand() method, however, would return a String allowing a
modal component like the Show/Hide button to specify its current state. The
getSource() method would return the same source object in each case, but the
getActionCommand() method would identify the intended action.
Coding All Possible Clicks
It is important to provide code for all possible user clicks in the
interface. Use a storyboard when possible to account for each
menu item and each button, making sure you have written an if
statement for each in the actionPerformed() method.
 
Search WWH ::




Custom Search