The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes #20
Stuart C. Shapiro
Spring, 2007


EventHandlers: Event-Driven Programming

An object can listen for an event, such as being clicked on. When it is notified of that event, it can perform some action.

If button is a JButton, add an ActionListener by doing button.addActionListener(listener), where listener is an instance of a class that implements the java.awt.event.ActionListener Interface.

The only method specified by the ActionListener Interface is public void actionPerformed(java.awt.event.ActionEvent e). When you implement an ActionListener's actionPerformed method, you may ignore the e parameter, which you are advised to do for now. Otherwise the actionPerformed method may do anything, including manipulate other objects it knows about.

First Previous Next

Copyright © 2007 by Stuart C. Shapiro. All rights reserved.

Last modified: Mon Nov 5 10:47:12 EST 2007
Stuart C. Shapiro <shapiro@cse.buffalo.edu>