CSCI15a: Events and Interfaces

CSCI111
Events, Interfaces and Implements
review


Event-Driven Programming

A lot of what we will see in this chapter, you have already seen through examples. Hopefully, now the code in the examples will make sense.

You have seen Buttons and ActionListeners; TextFields and ActionListeners; Component and java.awt.event.MouseListener .

Remember MVC? In Java, much of programming is event driven. Thus the user has components (views) which are associated with particular actions (interfaces - which constitute the controllers) when events happen (eg., pushing the button).

Different components generate different events and expect different interfaces to be implemented.

To implement an interface means to actually have code for the methods defined in a given interface. The API can be used to see which interfaces certain components expect to be implemented.

Follow some Listeners on the API to see the kinds of methods that are expected to be implemented. Look at Lists, CheckBoxes, etc. Also note that the methods in different Listener's have different event parameters.

Here is an example with a scrollbar:

Remember how each component needs the three statements in the init():

  1. instantiate the component
  2. add the component to the applet
  3. add the listener to the component
Remember that an Applet first calls init() then is painted. Three major methods to remember for an Applet thus become
  1. init()
  2. paint(Graphics g)
  3. methods of the Interfaces used
Remember to call repaint() in the Interface methods but not in the paint method. Why?

A bunch of examples using Scrollbar: (look at the applets and then the code to see how different values change)


For more on Components and Layout Managers, see
this page and for Applications and menus, you might find this page interesting.

Also, SWING might interest you

Remember the code that you were supposed to make Object Models for in Lab 5: DrawTest and its applet and TicTacToe and its applet . Let's look and see if we understand why certain methods are there now.