Showing posts with label what is event handling in java. Show all posts
Showing posts with label what is event handling in java. Show all posts

Wednesday, 10 April 2019

Event Handling in Java - Online Help

Event Handling in Java:

       Event Handling in Java is nothing but a method that receives an event object deciphers it and processes the user's interaction. Any program that uses GUI (Graphical User Interface)such as Java application written for windows, is event drivenEvent describes the change of state of an object. Example, pressing a button, entering a character in a text box. 

      A source generates an event and sends it to one or more listeners registered with the source. Once the event is received by the listener, they process the event and then return. Events are supported by a number of java packages, like java.util,java.awt, and java.awt.event.
     
      Writing an applet that responds to the user input, introduces us to event handling. we can make our applet respond to the user input by overriding event handler methods in our applet. There are a variety of event handler methods which we will see further.

      Each event must return a boolean value of true or false, indicating whether the event should be made available to other event handlers. If you have processed an event(for example, key down) you might record the value, and return through to show that no other handler should receive the event. If, however, your custom edit box can't process the character, it may want to return false to signal that other components(The panel ara applet in which the component is hosted)should process it.


Components of Event Handling:

Event handling has three main components:
Events:    An event is a change of state of an object.
Events source: Event source is an object that generates an event.
Listeners:  A listener is an object that listens to the event. A listener gets notified when an event occurs.

Important event classes and interface:

Event Classes
Description
Listener Interface
ActionEvent
Generated when the button is pressed, the menu item is selected, the list item is double-clicked.
ActionListener
MouseEvent
Generated when the mouse is dragged, moved, clicked, pressed or released also when he enters or exit a component.
MouseListener
KeyEvent
Generated when the input is received from a keyboard.
KeyListener
ItemEvent
Generated when checkbox  or list item is clicked
ItemListener
TextEvent
Generated when the value of textarea or textfield is changed.
TextListener
MouseWheelEvent
Generated when the mouse wheel is moved.
MouseWheelListener
WindowEvent
Generated when a window is activated, deactivated,deiconified, iconified, open or closed.
WindowListener
ComponentEvent
Generated when the component is hidden, moved, resized or set visible.
ComponentEventListener
ContainerEvent
Generated when the component is added or removed from the container.
ContainerListener
AdjustmentEvent
Generated when the scrollbar is manipulated.
AdjustmentListener
FocusEvent
Generated when a component gains or loses keyboard focus.
FocusListener

Sample program for Event Handling in Java:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="test" width=300 height=100></applet>*/
public class test extends applet implements KeyListener
{
String msg=" ";
public void init()
{
addKeyListener(this);
}
public void keypressed(KeyEvent k)
{
showStatus("key pressed");
}
public void keyReleased(KeyEvent k)
{
showStatus("key released");
}
public void keyTyped(KeyEvent k)
{
msg=msg+k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,20,40);
}
}