Typical Command Line Program: Non-Interactive Linear Execution Program
Typical Command Line Program: Non-Interactive Linear Execution Program
Non-interactive program:
GUI Event Handling
main()
{
Linear execution code;
code;
code;
code;
code;
code;
code;
code;
code;
code;
code;
} 2
}
…
Event callback procs Callback2()
{ code;
} }
} …
3 4
5 6
1
An example of Event Handling The Event Handling process
9 10
Second, it looks for a line of code which Third, the event handler must have a piece
registers an instance of the event handler of code that implements the methods in the
class as a listener of one or more listener interface.
components because, as mentioned earlier,
the object must be registered as an event public void actionPerformed(ActionEvent e)
listener. {
… //code that reacts to the action …
anyComponent.addActionListener(instance
Of DemoClass); // if e.source()==button1 …
}
11 12
2
Types of Events Types of Events (2)
Below, are some of the many kinds of events,
swing components generate. Act causing Event Listener Type
Act causing Event Listener Type User moving the mouse MouseMotionListener
over a component
User clicks a button, presses ActionListener
Enter, typing in text field Component becomes ComponentListener
User closes a frame WindowListener visible
3
The MouseListener Methods (2) The MouseMotionListener Methods
Event handling when the mouse button Invoked when the mouse button is pressed
is pressed on a component. over a component and dragged. Called
public void mousePressed(MouseEvent e) several times as the mouse is dragged
Event handling when the mouse button public void mouseDragged(MouseEvent e)
is released on a component. Invoked when the mouse cursor has been
public void mouseReleased(MouseEvent e) moved onto a component but no buttons
have been pushed.
public void mouseMoved(MouseEvent e)
19 20
21 22
4
Adapter classes for Event
Handling. Adapter classes - Illustration.
Why do you need adapter classes? You want create a class that implements a
Implementing all the methods of an interface MouseListener interface, where you
involves a lot of work. require only a couple of methods to be
If you are interested in only using some methods implemented. If your class directly
of the interface. implements the MouseListener, you must
Adapter classes implement all five methods of this
Built-in in JAVA interface.
Implement all the methods of each listener Methods for those events you don't care
interface with more than one method. about can have empty bodies
Implementation of all empty methods
25 26
29 30
5
Creating GUI applications
Using Inner classes (contd..) with Event Handling.
Use a class inside your Applet subclass that Guidelines:
extends the MouseAdapter class.
1. Create a GUI class
public class MyClass extends Applet { ... Describes the appearance of your
someObject.addMouseListener(new
MyAdapter());
GUI application.
... 2. Create a class implementing the
class MyAdapter extends MouseAdapter {
void mouseClicked(MouseEvent e) {
public
appropriate listener interface
//Event listener implementation here... } May refer to the same class as
} step 1.
}
31 32
33 34
35 36