Event Model


Before a component can respond to an event, the class must implement one of the Listener interface classes in the 'java.awt.event' package. Each interface class has required methods which must appear in your program. Finally, each component must be added as a listener for the specific type of event.

We'll stick with the most commonly used events.

ActionListener

The method required to implement ActionListener is:

	void actionPerformed(ActionEvent ae) {}
For each component that will listen for action events, you need to call its 'addActionListener(ActionListener al)' method.
	myButton.addActionListener(this);
The line of code above adds the button as an action listener for the class, because the class implements the ActionListener interface.

To get the item that triggered the event:

	Button thisButton = (Button)ae.getSource();
You can also get the action command (the text on the component):
	String command = ae.getActionCommand();
To test the class of object that responded to the event (in case you have more than one class type as action listeners):
	if ( ae.getSource() instanceof Button ) {
ItemListener

The method required to implement ItemListener is:

	void itemStateChanged(ItemEvent ie) {
For each component that will listen for events, you need to call its 'addItemListener(ItemListener il)' method.
	myChoice.addItemListener(this);
The line of code above adds the Choice as a listener for the class, because the class implements the ItemListener interface.

To get the item that triggered the event:

	Choice thisChoice = (Choice)ie.getSource();
To test the class of object that responded to the event (in case you have more than one class type as item listeners):
	if ( ie.getSource() instanceof Choice ) {
WindowListener

The methods required to implement WindowListener are:

	public void windowActivated(WindowEvent e) {
		// Invoked when the window is set to be the user's active window, which means the window (or one of its subcomponents) will receive keyboard events.
	}

	public void windowClosed(WindowEvent e) {
		// Invoked when a window has been closed as the result of calling dispose on the window.
	}

	public void windowClosing(WindowEvent e) {
		// Invoked when the user attempts to close the window from the window's system menu.
	}

	public void windowDeactivated(WindowEvent e) {
		// Invoked when a window is no longer the user's active window, which means that keyboard events will no longer be delivered to the window or its subcomponents.
	}

	public void windowDeiconified(WindowEvent e) {
		// Invoked when a window is changed from a minimized to a normal state.
	}

	public void windowIconified(WindowEvent e) {
		// Invoked when a window is changed from a normal to a minimized state.
	}

	public void windowOpened(WindowEvent e) {
		// Invoked the first time a window is made visible.
	}
For each component that will listen for events, you need to call its 'addItemListener(ItemListener il)' method.
		myFrame.addWindowListener(this);
The line of code above adds the Frame as a window listener for the class, because the class implements the WindowListener interface.