stu nicholls dot com | menu - Professional dropdown #3

Events

Event is an object that describes a state of change in a source.

May be generated as a consequence of a person interacting with the GUI elements
Pressing a button.
Entering a character through keyboard
Selecting an item from a list

May also be generated that are not directly caused due to user interactions
Timer expires
Counter exceeds a value

Event Sources

Event Listeners

Steps to Handle Events

Event Delegation Model



Event Class

Click here for .java code.

Click here for .html code.

There are many event classes and these are the core of Java,s event handling mechanism.

Few of the classes are ActionEvent, FocusEvent, InputEvent, keyEvent, MouseEvent, WindowEvent etc.

ActionEvent class:

An ActionEvent is generated when a button is pressed, a list item is double clicked, or a menu item is selected ActionListener interface defines one method to receive action events. The method is actionPerformed() is invoked when an action event occurs.
Syntax is:
     void actionPerformed(ActionEvent ae)

Event Delegation Model


WindowListener


interface WindowListener extends EventListener
{ 
	public void windowClosing(WindowEvent e);
	public void windowClosed(WindowEvent e); 
	public void windowOpened(WindowEvent e);
	public void windowIconified(WindowEvent e);
	public void windowDeIconified(WindowEvent e);
	public void windowActivated(WindowEvent e);
	public void windowDeacivated(WindowEvent e);
}

Steps to handle events: (Event Delegation Model)

In this following program all the methods of the Interface have overridden and it is necessary

Click here for example.

Click here for .java code.

Click here for .html code.

Problem with Listener Interface

Adapter classes

WindowAdapter


public abstract class WindowAdapter implements WindowListener
{
	public void windowClosing(WindowEvent e){ }
	public void windowClosed(WindowEvent e) { }
	public void windowOpened(WindowEvent e){ }	
	public void windowIconified(WindowEvent e){ }
	public void windowDeIconified(WindowEvent e){ }
	public void windowActivated(WindowEvent e){ }
	public void windowDeacivated(WindowEvent e){ }
}


Using WindowAdapter class


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class TestFrameEvent2 extends WindowAdapter
{
	Frame f;
     	TestFrameEvent2()
	{
     		f=new Frame("New Frame");
     		f.setSize(300,200);
     		f.setVisible(true);
     		f.addWindowListener(this);
      	}
   	public void windowClosing(WindowEvent e)
        {
		System.out.println("Windows is closing");
                System.exit(0);
        }
	public static void main(String args[])
	{
      		TestFrameEvent2 tf=new TestFrameEvent2();
       	}
}

In this example WindowsAdapter class is used when you are interested only in one event say windowClosing() event.

Using Anonymous Inner class

Click here for .java code.

Click here for .html code.

The size of the program can be reduced by using inner classes. They allow you to put the event handling class and method right next to the place where you declare the control or register the call back listener. Anonymous classes are the refinement of inner classes allowing you to combine the definition of the class with the instance allocation.

An anonymous inner class is one that is not assigned a name. Let us consider an applet which will display a string "Mouse Pressed" in the status bar of the applet viewer or browser when the mouse is pressed.

There is one top level class in this program: AnonymousInnerClassDemo. The init() method calls the addMouseListener() method. Its argument is an expression that defines and instantiates an anonymous inner class.

The syntax new MouseAdapter() {...} indicates to the compiler that the code between the braces defines an anonymous inner class.

Furthermore that class extends Mouse Adapter. This new class is not named, but it is automatically instantiated when this expression is executed.

Because this anonymous inner class is defined within the scope of AnonymousInnerClassDemo, it has access to all of the variables and methods within the scope of that class. Therefore, it can call showStatus() method directly.

Thus you can create more efficient code.

Our aim is to provide information to the knowledge

comments powered by Disqus
click here
click here



click here
click here
click here
Footer1