|
Java Events
Two categories:
| Mouse Event | Description |
|---|---|
| mouse pressed | the mouse button is pressed |
| mouse released | the mouse button is released |
| mouse clicked | the mouse button is pressed down and released without moving the mouse in between |
| mouse entered | the mouse pointer is moved onto ( over ) a component |
| mouse exited | the mouse pointer is moved off a component |
| Mouse Motion Event | Description |
|---|---|
| mouse moved | the mouse is moving |
| mouse dragged | the mouse is moved while the mouse button is being pressed down |
|
|
|
|
In the RubberLines program example, the listener interface contains event methods that are not relevant to a particular program and we provide empty definitions to satisfy the interface definitions.
A alternative technique for creating a listener class is to extend an event adapter class. Each listener interface that contains more than one method has a corresponding adapter class that already contains empty definitions for all of the methods in the interface.
|
|
|
|
For example addActionListener() accepts a parameter of type ActionListener(), the interface. Instead of accepting a parameter of only one object type, the addActionListener() method can accept any object of any class that implements the ActionListener() interface.
|
|
|
|
|
An exception is an object that defines an unusual or erroneous situation.
Examples:
Uncaught exceptions:
|
Output: Exception in thread "main" java.lang.ArithmeticException: / by zero at Zero.main(Zero.java:17) |
As exceptions are objects, when you throw an exception, you throw an object. You can't throw just any object as an exception, however -- only those objects whose classes descend from Throwable. Throwable serves as the base class for an entire family of classes, declared in java.lang, that your program can instantiate and throw. A small part of this family is shown in the Figure below.

An example: Coffee sipping

The try-catch statement
|
|
The finally clause ( optional for try-catch ) defines a seciton of code that is executed no matter how the try block is exited.