Showing posts with label Event Handling in java. Show all posts
Showing posts with label Event Handling in java. Show all posts

Tuesday, 14 May 2019

Mouse Event handling in java - Online Help

Mouse Event handling in Java:

The mouse event handling in Java has two methods for interfaces. There are "MouseListeners" and "MouseMotionListeners".These methods are called when the mouse interacts with components.


Each of the mouse event-handling methods takes a mouse event object as its arguments. A mouse event object contains information about the mouse event that occurred, including the x- and y-coordinated of the location where the event occurred.

MouseListener interface methods:

public void mousePressed(MouseEvent e)

This method is called when a mouse button is pressed continuously.

public void mouseReleased(MouseEvent e)

This method is called when a mouse button is released after being pressed.

public void mouseClicked(MouseEvent e)

This method is called when a mouse button is pressed and released. (when the mouse cursor exited from a component).

public void mouseEntered(MouseEvent e)

This method is called when a mouse cursor is entered into the component.

public void mouseExited(MouseEvent e)

This method is called when a mouse cursor is leaves from a component.


MouseMotionListener interface methods:

public void mouseMoved(MouseEvent e)

This method is called when the mouse cursor is moved over the component.

public void mouseDragged(MouseEvent e)

This method is called when the mouse button is pressed on a component and moved.



Sample program for Mouse-Event Handling:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="AdaptorDemo" width=300 height=200>
</applet>*/
public class AdaptorDemo extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
addMouseListener(new MyMouseAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter
{
AdaptorDemo adapterdemo;
public MyMouseAdapter(AdaptorDemo adapterdemo)
{
this.adapterdemo=adapterdemo;
}
public void mouseClicked(MouseEvent e)
{
adapterdemo.showStatus("mouse clicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter
{
AdaptorDemo adapterdemo;
public MyMouseMotionAdapter(AdaptorDemo adapterdemo)
{
this.adapterdemo=adapterdemo;
}
public void mouseDragged(MouseEvent e)
{
adapterdemo.showStatus("mouse dragged");
}
}

Sunday, 12 May 2019

Key Event Handling in java - online help

Key Event Handling in java:

Key events in java are generated when a key is pressed, the key is typed and a key is released. The key Listener interface is handling key events.

Each of the key event handling methods takes a keyEvent object is its arguments.

A key event object contains information about the key event. The various methods available in the keyListener interface are as follows:

void keyPressed(keyEvent e)

The keyPressed ()method is used to call when a key is pressed.

void keyTyped(keyEvent e)

The keyTyped ()method is used to call when a key is typed.

void KeyReleased(keyEvent e)

The keyReleased()method is used to call when typed or continuously pressed a key is released.

getKeyChat()

The getKeyChat()method is used to return the character of the key from which the event is generated.

Sample Program for key event handling in java:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="impkey" width=200 height=300></applet>*/
public class impkey extends Applet implements KeyListener
{
TextField t
TextArea ta;
pulic void init()
{
t=new TextField(20);
ta=new TextArea();
add(t);
add(ta);
t.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("key pressed");
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("keyPressed");
}
public void keyReleased(keyEvent ke)
{
if(ke.getSource()==t)
ta.append("Key Released");
}
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()==t)
{
char c=ke.getKeyChar();
ta.append("KeyTyped"+c);
}
}


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);
}
}