In this article we will learn about using delegation event model i.e., how to implement event handling in Java programs.
Following are the basic steps in using delegation event model or for handling events in a Java program:
- Implement the appropriate listener interface.
- Register the listener with the source.
- Provide appropriate event handler to handle the event raised on the source.
Key Events Handling
Following is a Java program which handles key events. In the program when the user types characters in the text field, they are displayed in a label below the text field.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet implements KeyListener
{
JTextField jtf;
JLabel label;
public void init()
{
setSize(600,300);
setLayout(new FlowLayout());
jtf = new JTextField(20);
add(jtf);
jtf.addKeyListener(this);
label = new JLabel();
add(label);
}
public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke)
{
label.setText(String.valueOf(ke.getKeyChar()));
}
}
Initial output of the above program is as follows:
After the user enters a character into the text field, the same character is displayed in the label beside the text field as shown in the below image:
Mouse Events Handling
Following is a Java program which handles both mouse events and mouse motion events. When the user performs a mouse event on the applet it is updated in the label.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet implements MouseListener, MouseMotionListener
{
JLabel label;
public void init()
{
setSize(600,300);
setLayout(new FlowLayout());
label = new JLabel();
add(label);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
label.setText("Mouse is clicked");
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseDragged(MouseEvent me){}
public void mouseMoved(MouseEvent me)
{
label.setText("Mouse is moved");
}
}
Initial output of the above program is as shown below:
When the user moves the mouse cursor over the applet display area, then the output is as shown below:
When the user clicks on the applet display area, the output is as shown below:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
I’m really impressed together with your writing skills as neatly as with the layout in your weblog.
Is this a paid subject or did you customize it your self?
Either way stay up the excellent high quality writing, it is rare
to look a great weblog like this one these days..