DEMYSTIFYING PROGRAMMING: CHAPTER FOUR Chapter Four: ACTION EVENT MODEL 1 Objectives 1 4.1 Additional GUI components 1 JLabel 1 JTextField 1 4.2 Inductive Pause 1 4.4 Events and Interaction 3 Establish an event Model (ActionListener) 3 Register a listener to a GUI object 4 Handle the event when it occurs 4 New GUI components support user interaction 5 4.5 Review Concepts 5 Action Event Model 5 Pattern 6 New methods for JTextField 6 4.6 Challenge 6 4.7 Java Key Words 7 4.8 Exercises 7 Coding Exercises 7 Review Questions 7 Index 9
Chapter Four: ACTION EVENT MODEL Objectives Language components that support user interaction are introduced. The action event model is demonstrated. This model includes implementing an ActionListener, registering a listener, and building an event handler. Additional GUI components JLabel The JLabel is a label to assist users in understanding the GUI and what is expected of them. JTextField This GUI component is like the JTextArea except that it can have only one line of information on it. The JTextField is usually used to collect user input. 4.2 Inductive Pause Enough talk, let s take time out to observe and think. Look at the applet on the following page. Demystifying Programming Ch4: ActionEvent Model 1
Figure 3: ShowName.java Many parts of this applet are familiar; they follow the same patterns that were used in the previous chapter. What do you notice that is different? Which lines contain new elements? What type of object is txtname? On which lines is it referenced? What does the method header on line 41 indicate in its name? (hint: names should be meaningful) What role or purpose is this method performing? On line 42 what do you think is happening with txtname.gettext( )? gettext is a method of class JTextField. If you know the role commonly played by JTextFields, you can probably guess what is happening on line 42. Focus on line 42. What does String username accomplish? What does username accomplish on line 43? Demystifying Programming Ch4: ActionEvent Model 2
4.4 Events and Interaction Solutions become more useful when a user can interact with them. Using the mouse or keyboard a user can supply information or request an action. The program must hear this user action and be programmed to respond in an appropriate manner. Many GUI objects can generate events. Establish an event Model (ActionListener) An event model is usually reflected in the class header. In this chapter we use the ActionListener. The class header for the ShowName class should be: public class ShowName extends JApplet implements ActionListener{ Notice that the ShowName class implements ActionListener. The ActionListener model will assist the programmer in building interactive applets. This event model requires an actionperformed method. This actionperformed method defines the event handler for responding to the enter key being pushed while the prompt is in the JTextField. Carefully explore the following ShowName.java code. It contains many new code items but uses several patterns that were used in the previous chapter. Decide if the line contains new elements. Describe the purpose of each line in your own words. Demystifying Programming Ch4: ActionEvent Model 3
Figure 2: ShowName.java Register a listener to a GUI object The applet must be told to listen to one or more of the GUI components. In the code ShowName applet the JTextField txtname has a listener registered for it. Identify the line that contains a new pattern in the sample after instantiation of the object txtname. Which line registers the listener? Write the code here. The method has one parameter (this) which refers to the applet and its attachment to the browser. The applet listens to any object that has a listener registered for it. When an appropriate event is triggered, the applet gathers information about the event. Handle the event when it occurs Although information has been gathered about an event, a method the event handler is required to respond to the event. The event handler for an action event must be called actionperformed. The header must always be Demystifying Programming Ch4: ActionEvent Model 4
public void actionperformed(actionevent e) The parameter required for this event is always ActionEvent type although it might be called something other than e. For example, the ActionEvent type object might be called evt or george. The code within the curly braces, the method body will always vary depending upon what needs to be done in response to the event. How does the example ShowName.java respond to an ActionEvent? New GUI components support user interaction JLabel Labels are used to provide instructions for users. The text on the face of the label is usually added during instantiation. This GUI object follows the same pattern as the JTextArea. The example provided above has the following code: JLabel lblname; //declared above init( ) //instantiate label and add to Container in body of init lblname = new JLabel("Please enter a name:");//message con.add(lblname); JTextField JTextFields are used to gather information during execution from the user. It follows the same pattern of declaration and instantiation as the JLabel. JTextField txtname; //declared above init( ) //instantiated and added to Container in body of init txtname = new JTextField(15); //size (15) is not required con.add(txtname); 4.5 Review Concepts Action Event Model 1. import the event library 2. implements ActionListener in class header 3. register listener for GUI object 4. create event handler actionperformed( ) Demystifying Programming Ch4: ActionEvent Model 5
Pattern Figure 4: Action Event Pattern The pattern is: 1) import event directory and all its files 2) the class header declares that it implements ActionListener 3) object to be listened to must register a listener in init body 4) an event handler (actionperformed) must be designed to respond to event New methods for JTextField gettext( ): The new method is gettext( ). This method is the opposite of settext( ). As settext writes to the object, gettext reads text from an object. This method must be attached with a dot (.) to an object which has text. String username = txtname.gettext(); Integer.parseInt( ): Another special method may also be useful at times. When a user is asked to type a number in a JTextField, this information can be read using gettext( ). However, this information is text and not a number when it is retrieved from a textfield. If we want to do math with it and if we want to store it in a variable declared for an int (whole number), we ll need to first convert this text into an int. The method uses a wrapper class, Integer and the method parseint: int number = Integer.parseInt( txtusernumber.gettext( ) ); The text typed from the keyboard by the user into the JTextField named txtusernumber is read and parsed (converted) into a whole number. This int is then stored in (assigned to) the newly declared int type variable named number. This method is parseint and is attached to the Integer wrapper class object with a dot (. ) symbol. It is the wrapper class Integer which is parsing the String to int. 4.6 Challenge Is it possible to write to a JLabel or JTextField in the same way that we can to a JTextArea? Prove it to yourself. Demystifying Programming Ch4: ActionEvent Model 6
4.7 Java Key Words implements addactionlistener(this) actionperformed( ) ActionEvent 4.8 Exercises 1. What does the following code accomplish? String myname = txtname.gettext( ); 2. Instantiate each of the GUI objects declared in the following code. JLabel lblfirst, lbllast; JTextField txtfirst, txtlast; JButton btnadd; public void init( ){ } 3. Add each of the GUI objects instantiated in the previous exercise to the Container container. This code would be placed in the init block following the instantiation. Coding Exercises 4. Produce code following the Design Plan from exercise two (2) above. Debug and test your product. 5. Create a JApplet that will receive a user s name and display the name followed by the stated objectives of this chapter. Review Questions 6. An action listener requires an event handler called. Demystifying Programming Ch4: ActionEvent Model 7
7. What library must be imported to support events?. 8. Which of the following lines of code registers a listener? a. btnpress = new JButton( Press ); b. container.add(btnpress); c. btnpress.addactionlistener(this); d. registerlistener(btnpress); Demystifying Programming Ch4: ActionEvent Model 8
Index ActionEvent, 4, 5 ActionListener, 1, 3, 5, 6 actionperformed, 3, 4, 5, 6 assigned, 6 container, 7 event, 1, 3, 4, 5, 6, 7 event handler, 1, 3, 4, 5, 6, 7 event model, 1, 3 events, 3, 7 gettext, 2, 6 GUI, 1, 3, 4, 5, 6, 7 import, 5, 6 instantiate, 5 int, 6 Integer.parseInt, 6 JButton, 7 JLabel, 1, 5, 6 JTextArea, 1, 5, 6 JTextField, 1, 2, 3, 4, 5, 6 methods, 6 new, 2, 3, 4, 5, 6, 7 public, 3, 4, 7 register, 5, 6 String, 2, 6 variable, 6 void, 4, 7 wrapper class, 6 Demystifying Programming Ch4: ActionEvent Model 9