Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Related Tutorials


 
 

Share on Google+Share on Google+

Events

Advertisement
In this section, you will learn how to handle events in Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through the example and use methods through which you can implement the event dri

Events

     

In this section, you will learn how to handle events in Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through the example and use methods through which you can implement the event driven application.

For any event to occur, the objects registers themselves as listeners. No event takes place if there is no listener i.e. nothing happens when an event takes place if there is no listener. No matter how many listeners there are, each and every listener is capable of processing an event. For example, a SimpleButtonEvent applet registers itself as the listener for the button's action events that creates a Button instance.

ActionListener can be implemented by any Class including Applet. One point to remember here is that all the listeners are always notified. Moreover, you can also call AWTEvent.consume() method whenever you don't want an event to be processed further. There is another method which is used by a listener to check for the consumption. The method is isConsumed() method. The processing of the events gets stopped with the consumption of the events by the system once a listener is notified. Consumption only works for InputEvent and its subclasses. Moreover, if you don't want any input from the user through keyboard then you can use consume() method for the KeyEvent. 

The step by step procedure of Event handling is as follow:

  1. When anything interesting happens then the subclasses of AWTEvent are generated by the component.
  2. Any class can act like a Listener class permitted by the Event sources. For example, addActionListener() method is used for any action to be performed, where Action is the event type. There is another method by which you can remove the listener class which is removeXXXListener() method, where XXX is the event type. 
  3. A listener type has to be implemented for an event handling such as ActionListener.
  4. There are some special type of listener types as well for which you need to implement multiple methods like key Events. There are three methods which are required to be implemented for Key events and to register them i.e. one for key release, key typed and one for key press. There are some special classes as well which are known as adapters that are used to implement the listener interfaces and stub out all the methods. these adapter classes can be sub classed and and can override the necessary method. 

AWTEvent

Most of the times every event-type has Listener interface as Events subclass the AWTEvent class. However, PaintEvent and InputEvent don't have the Listener interface because only the paint() method can be overriden with PaintEvent etc.

Low-level Events

A low-level input or window operation is represented by the Low-level events. Types of Low-level events are mouse movement, window opening, a key press etc. For example, three events are generated by typing the letter 'A' on the Keyboard one for releasing, one for pressing, and one for typing. The different type of low-level events and operations that generate each event are show below in the form of a table.

 FocusEvent   Used for Getting/losing focus.
 MouseEvent   Used for entering, exiting, clicking, dragging, moving, pressing, or releasing.
 ContainerEvent   Used for Adding/removing component.
 KeyEvent   Used for releasing, pressing, or typing (both) a key.
 WindowEvent   Used for opening, deactivating, closing, Iconifying, deiconifying, really closed.
 ComponentEvent   Used for moving, resizing, hiding, showing.

Semantic Events

The interaction with GUI component is represented by the Semantic events like changing the text of a text field, selecting a button etcThe different events generated by different components is shown below.

 ItemEvent  Used for state changed.
 ActionEvent   Used for do the command.
 TextEvent   Used for text changed.
AdjustmentEvent   Used for value adjusted.

Event Sources

If a component is an event source for something then the same happens with its subclasses. The different event sources are represented by the following table.

 Low-Level Events
 Window   WindowListener
 Container   ContainerListener
 Component   ComponentListener
 FocusListener
 KeyListener
 MouseListener
 MouseMotionListener

 

 Semantic Events
  Scrollbar    AdjustmentListener
 TextArea
 TextField
  TextListener
 Button
 List
 MenuItem
 TextField 
 ActionListener
 Choice
 Checkbox
 Checkbox
 CheckboxMenuItem
 List 
 ItemListener

Event Listeners

Every
listener interface has at least one event type. Moreover, it also contains a method for each type of event the event class incorporates. For example as discussed earlier, the KeyListener has three methods, one for each type of event that the KeyEvent has: keyTyped(), keyPressed(), and keyReleased().

The Listener interfaces and their methods are as follow:

 Interface  Methods
 WindowListener   windowActivated(WindowEvent e)
 windowDeiconified(WindowEvent e)
 windowOpened(WindowEvent e)
 windowClosed(WindowEvent e) 
 windowClosing(WindowEvent e)
 windowIconified(WindowEvent e)
 windowDeactivated(WindowEvent e)
 ActionListener  actionPerformed(ActionEvent e)
 AdjustmentListener  adjustmentValueChanged(AdjustmentEvent e)
 MouseListener  mouseClicked(MouseEvent e)
 mouseEntered(MouseEvent e)
 mouseExited(MouseEvent e)
 mousePressed(MouseEvent e)
 mouseReleased(MouseEvent e)
 FocusListener  focusGained(FocusEvent e)
 focusLost(FocusEvent e)
 ItemListener  itemStateChanged(ItemEvent e)
 KeyListener  keyReleased(KeyEvent e)
 keyTyped(KeyEvent e)
   keyPressed(KeyEvent e)
 ComponentListener  componentHidden(ComponentEvent e)
 componentMoved(ComponentEvent e)
 componentShown(ComponentEvent e)
 componentResized(ComponentEvent e)
 MouseMotionListener  mouseMoved(MouseEvent e)
   mouseDragged(MouseEvent e)
 TextListener  textValueChanged(TextEvent e)
 ContainerListen er  componentAdded(ContainerEvent e)
   componentRemoved(ContainerEvent e)

   

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Related Tutorials

Discuss: Events   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
vindesh kumar
June 24, 2011
event

shown one programb of mouse action
DMCA.com