Next page | Contents page |

Events

And other event objects and classes to handle them, defined in various awt and swing classes - see the API documentation. Most of the fundamental event classes are in package java.awt.event

Connecting event sources to listeners

Event sources create event objects. Other classes, registered as listeners for each type of event, are then invoked by the JVM so they can handle the events.

Suppose we have class Src which can generate Xxx and Yyy events:

	public class Src extends Component
	{ ... }

The listener classes must also be defined, usually implementing interfaces so that the classes can subclass anything else:

	class X1 implements XxxListener
	{ ... }
	class X2 implements XxxListener
	{ ... }
	class Y1 implements YyyListener
	{ ... }

Then we might use them all like this:

	Src src = new Src ();
	X1 x1 = new X1 ();
	X2 x2 = new X2 ();
	Y1 y1 = new Y1 ();
	src.addXxxListener (x1);
	src.addXxxListener (x2);
	src.addYyyListener (y1);

NB: Xxx events go to 2 listeners in this example.

Event objects

When an event occurs it is represented by an object derived from java.util.EventObject. This has method getSource() returning the Object which generated the event. This will generally need to be cast to the real class of the object.

Both awt and Swing derive more complex objects from EventObject. Eg, javax.swing.event.MenuEventObject

Event listeners

XxxListener interfaces can specify any number of methods which may be implemented by a class to handle Xxx-type events. Eg, MouseListener handles various kinds of MouseEvent:

	void mousePressed (MouseEvent e); // A button pressed
	void mouseReleased (MouseEvent e); // A button released
	void mouseClicked (MouseEvent e); // Pressed & released
	void mouseEntered (MouseEvent e); // Mouse entered component
	void mouseExited (MouseEvent e);  // Mouse left the component

The MouseEvent object can then tell you more details: eg, getX() and getY() or getPoint() get the mouse coordinates when the event occurred. It also inherits getComponent() and getSource(). See API documentation for details.

MouseMotionListener

MouseEvents are unusual in having two kinds of listeners: as well as MouseListener (above) there is MouseMotionListener. This has 2 methods:

	void mouseDragged (MouseEvent e); // Moved, button held down
	void mouseMoved (MouseEvent e);   // Moved, no button down

Adapter classes

XxxListener interfaces often have corresponding XxxAdapter classes which you can subclass and just override the method(s) you need, rather than implementing all the methods of an interface. Adapter classes are trivial implementations of Listener interfaces, just having empty method implementations.

Anonymous inner class event handlers

It is quite common to write event handlers as anonymous inner classes.

Eg (using a Listener interface):

	JFrame jf = new JFrame ("Title");
	jf.addFocusListener 
	(
		new FocusListener ()
		{  // Implement all methods in the interface:
			public void focusGained (FocusEvent e) { /* code */ }
			public void focusLost (FocusEvent e) { /* code */ }
		} 
	);

Eg (using an Adapter class):

	Panel p = new Panel (); // A blank container
	p.addMouseListener 
	(
		new MouseAdapter ()
		{  // Override just one method in the class:
			public void mouseClicked (MouseEvent e) { /* code */ }
		} 
	);

Which mouse button was pressed?

You shouldn't ask this question: it's platform-dependent! A Mac only has 1 mouse button and it is used in conjunction with modifier keys (Mac command key = Windows alt key). However, java.awt.event.InputEvent (in the event class hierarchy) has flags BUTTONx_MASK, where x = 1..3. Eg,

	public void mouseClicked (MouseEvent e)
	{
		int mods = e.getModifiers ();

		if (mods & InputEvent.BUTTON1_MASK != 0)
		{ // That's a bit-wise AND!
			// Left button was pressed ...
Next page | Contents page |