lecture-6-sept9 - Events, if statement CS 178: Programming...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Events, if statement CS 178: Programming with Multimedia Objects Aditya P. Mathur Sept 9, 2004 Professor of Computer Sciences Purdue University, West Lafayette Last update: September 9, 2004 Learning Objectives What is a event? What is an ActionListener? What is an ItemListener? How to do things based on some condition? A simple "survey" applet 2 Course Introduction Aditya P. Mathur 2004 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 <Enter> key in a TextField.. Item event: Occurs when the user clicks a CheckBox, CheckBoxMenuItem, a Choice item, or a List item. Course Introduction event types will be discussed later. Several other Aditya P. 3 Mathur 2004 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 itemStateChanged method in the ItemListener class handles the item events. The input to each method is an event. 4 Course Introduction Aditya P. Mathur 2004 ActionListener and actionPerformed To allow an applet to respond to action events, make sure you add "implements ActionListener" as follows to the applet declaration: public void myApplet extends Applet implements ActionListener { Course Introduction Aditya P. Mathur 2004 5 ItemListener and itemStateChanged To allow an applet to respond to item events, make sure you add "implements ItemListener" as follows to the applet declaration: public void myApplet extends Applet implements ItemListener { Course Introduction Aditya P. Mathur 2004 6 Processing Action and Item events To allow an applet to respond to item and action events, make sure you add "implements ItemListener, ActionListener" as follows to the applet declaration: public void myApplet extends Applet implements ItemListener, ActionListener { Course Introduction Aditya P. Mathur 2004 7 The actionPerformed method The actionPerformed method is declared as follows: public void actionPerformed (ActionEvent e) 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. Course Introduction Aditya P. Mathur 2004 8 Identifying the action event The getSource method is used to obtain the source of an action event. For example Object source = e.getSource(); will return the name of the button that was clicked. Course Introduction Aditya P. Mathur 2004 9 Associating an action command An action command is a string. You can associate an action command with any button. For example: yesButton.setActionCommand("INCREMENT") associates the action command INCREMENT with the yesButton. Course Introduction Aditya P. Mathur 2004 10 Retrieving the action command The action command associated with a button can be obtained as follows: String eventCode =e.getActionCommand(); sets the value of the eventCode variable to the action command associated with the event that occured. Course Introduction Aditya P. Mathur 2004 11 Processing an action event First step: Identify the source of event Second step: Process the event using appropriate code. Course Introduction Aditya P. Mathur 2004 12 The itemStateChanged method The itemStateChanged method is declared as follows: public void itemStateChanged (ItemEvent e) 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 Course Introduction Aditya P. Mathur 2004 13 Identifying the item event The getSource method is used to obtain the source of an item event (same as for the action event). For example Object source = e.getSource(); will return the name of the CheckBox that was clicked. Course Introduction Aditya P. Mathur 2004 14 Associating an "item" command An "item" command is not associated with an item such as a CheckBox. Course Introduction Aditya P. Mathur 2004 15 Processing an item event First step: Identify the source of the event. Second step: Process the event using appropriate code. Course Introduction Aditya P. Mathur 2004 16 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. Course Introduction Aditya P. 17 Mathur 2004 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. } Course Introduction Aditya P. Mathur 2004 18 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 Compound condition using AND (&&) age and speed are integer variables. drinking is a Course Introduction Aditya P. boolean variable. 19 Mathur 2004 Relational and boolean operators Relational operators: <, >, <=,>=, <> Boolean operators: && (and), || (or), ! (not) More details? Read Chapter 6 of the text. Course Introduction Aditya P. Mathur 2004 20 ...
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 20

lecture-6-sept9 - Events, if statement CS 178: Programming...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online