Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:

Similar presentations


Presentation on theme: "CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:"— Presentation transcript:

1 CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update: September 9, 2004 Events, if statement

2 Course Introduction  Aditya P. Mathur 2004 2 Learning Objectives What is an ActionListener? What is a event? What is an ItemListener? How to do things based on some condition? A simple “survey” applet

3 Course Introduction  Aditya P. Mathur 2004 3 Event Types An action, e.g. clicking a button, selecting an item in a checkbox, checking or unchecking a box. Event types: Action event: For example occurs when a button is clicked, double clicking an item in a List of items, selection of a MenuItem, pressing of the key in a TextField.. Item event: Occurs when the user clicks a CheckBox, CheckBoxMenuItem, a Choice item, or a List item. Several other event types will be discussed later.

4 Course Introduction  Aditya P. Mathur 2004 4 Handling Events Events generated by components such as Buttons and Checkboxes, are handled by Listeners. The actionPerformed method in the ActionListener class handles the action events. The input to each method is an event. The itemStateChanged method in the ItemListener class handles the item events.

5 Course Introduction  Aditya P. Mathur 2004 5 ActionListener and actionPerformed public void myApplet extends Applet implements ActionListener { To allow an applet to respond to action events, make sure you add “implements ActionListener” as follows to the applet declaration:

6 Course Introduction  Aditya P. Mathur 2004 6 ItemListener and itemStateChanged public void myApplet extends Applet implements ItemListener { To allow an applet to respond to item events, make sure you add “implements ItemListener” as follows to the applet declaration:

7 Course Introduction  Aditya P. Mathur 2004 7 Processing Action and Item events public void myApplet extends Applet implements ItemListener, ActionListener { To allow an applet to respond to item and action events, make sure you add “implements ItemListener, ActionListener” as follows to the applet declaration:

8 Course Introduction  Aditya P. Mathur 2004 8 The actionPerformed method public void actionPerformed (ActionEvent e) The actionPerformed method is declared as follows: Note that e is an input parameter. It is of type ActionEvent. Its value tells us the source of the event e.g. which button was clicked.

9 Course Introduction  Aditya P. Mathur 2004 9 Identifying the action event Object source = e.getSource(); The getSource method is used to obtain the source of an action event. For example will return the name of the button that was clicked.

10 Course Introduction  Aditya P. Mathur 2004 10 Associating an action command yesButton.setActionCommand(“INCREMENT”) An action command is a string. You can associate an action command with any button. For example: associates the action command INCREMENT with the yesButton.

11 Course Introduction  Aditya P. Mathur 2004 11 Retrieving the action command String eventCode =e.getActionCommand(); The action command associated with a button can be obtained as follows: sets the value of the eventCode variable to the action command associated with the event that occured.

12 Course Introduction  Aditya P. Mathur 2004 12 Processing an action event First step: Identify the source of event Second step: Process the event using appropriate code.

13 Course Introduction  Aditya P. Mathur 2004 13 The itemStateChanged method public void itemStateChanged (ItemEvent e) The itemStateChanged method is declared as follows: Note that e is an input parameter. It is of type ItemEvent. Its value tells us the source of the event e.g. which CheckBox was checked

14 Course Introduction  Aditya P. Mathur 2004 14 Identifying the item event Object source = e.getSource(); The getSource method is used to obtain the source of an item event (same as for the action event). For example will return the name of the CheckBox that was clicked.

15 Course Introduction  Aditya P. Mathur 2004 15 Associating an “item” command An “item” command is not associated with an item such as a CheckBox.

16 Course Introduction  Aditya P. Mathur 2004 16 Processing an item event First step: Identify the source of the event. Second step: Process the event using appropriate code.

17 Course Introduction  Aditya P. Mathur 2004 17 The if statement The if statement allows selective execution of Java statements. For example: if (customer.Age>60) { processSeniorCitizen(customer); } checks the age of a customer object and invokes the processSeniorCitizen() method only if the age is greater than 60.

18 Course Introduction  Aditya P. Mathur 2004 18 The if statement: format The if statement allows selective execution of Java statements. For example: if (condition) { sequence of Java statements separated by semicolon. There is no limit on the number of statements in this sequence. }

19 Course Introduction  Aditya P. Mathur 2004 19 Conditions A condition is any expression that evaluates to true or false. Here are some examples: age<65 speed>55 speed>65 && age<18 speed>65 && drinking age and speed are integer variables. drinking is a boolean variable. Compound condition using AND (&&)

20 Course Introduction  Aditya P. Mathur 2004 20 Relational and boolean operators Relational operators:, =, <> Boolean operators: && (and), || (or), ! (not) More details? Read Chapter 6 of the text.


Download ppt "CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:"

Similar presentations


Ads by Google