Home
>>     < >




In-class notes for 01/05/2018

CS 284 (MCA), Interim 2018

Submitted questions on assignments and technology

Submitted questions on graphics reading

Event-driven programming

  • In event-driven programming, a programmer writes code that specifies how a program should respond to events that typically originate outside of that program. This is the usual approach for writing user-interface code, since a user's interaction and system activities ordinarily initiate program actions (e.g., through a button click, text entry, or from a network communication). These interactions and activities are called events, and the code for responding to a particular event is called a callback or event handler.

  • A Java interface is a specification of methods required for a particular type of event callback. For example, the interface ActionListener indicates the method used for button-click callbacks, called actionPerformed()

    public interface ActionListener extends EventListener {
      void actionPerformed(ActionEvent e);
    }
    
    This interface is predefined in the package java.awt.event. Note that the method actionPerformed() is declared in the interface, but not defined there -- i.e., no code is provided for the method here. In order to respond to button clicks, a program must provide an implementation of actionPerformed() appropriate for that particular program and button. This is called implementing the interface.

  • Walkthrough of ~cs284/java/Scribble.java

    • Callback code

    • Creating components (e.g., button)

    • Registering callbacks with event sources. This arranges for those callbacks to be called when those events occur.

    • Placing components into the (Applet) container

  • An object that implements a callback is called an adapter in Java.

Adapter styles

  • Java provides several ways to implement callback methods in classes of a program. See ~cs284/java/adapters

    Java classes can inherit from only one subclass (keyword extends), but they can implement any number of interfaces. Interfaces provide many of the advantages of subclassing. For example, any instance of a class that implements interface X can be passed as an argument of type X for in a method (e.g., addActionListener).
  • "Camel capitalization"




< >