Java

How to Use Deligation Event Model in Java Programming4 min read

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:

  1. Implement the appropriate listener interface.
  2. Register the listener with the source.
  3. 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.

Initial output of the above program is as follows:

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:

  1. Implement the appropriate listener interface.
  2. Register the listener with the source.
  3. 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.

This code defines an applet called “MyApplet” that extends JApplet and implements the KeyListener interface. The applet contains a JTextField and a JLabel. The init() method is called when the applet is first loaded, and it sets the size and layout of the applet, as well as initializing the JTextField and JLabel.

The JTextField is added to the applet and a KeyListener is added to it. The keyTyped() method is overridden to set the text of the label to the character of the key pressed in the JTextField. The keyPressed() and keyReleased() methods are also overridden but they are empty and do nothing.

When the applet is run, the user will be able to type into the JTextField, and as they type, the text of the label will update to show the character of the key that was just typed.

It’s worth noting that the JTextField will be a single line text field, with a width of 20 characters.

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.

This code defines an applet called “MyApplet” that extends JApplet and implements the MouseListener and MouseMotionListener interfaces. The applet contains a JLabel. The init() method is called when the applet is first loaded, and it sets the size and layout of the applet, as well as initializing the JLabel and adding it to the applet. The applet also adds both MouseListener and MouseMotionListener to itself, which allows it to respond to different types of mouse events.

The mouseClicked() method is overridden to set the text of the label to “Mouse is clicked” when the mouse is clicked on the applet. The other methods mousePressed(), mouseReleased(), mouseEntered() and mouseExited() are also overridden but they are empty and do nothing.

The mouseDragged() and mouseMoved() methods are also overridden. The mouseDragged() method is empty and does nothing. The mouseMoved() method sets the text of the label to “Mouse is moved” when the mouse is moved on the applet.

This applet will respond to the mouse events by updating the label with the specific mouse event that occurs.

Take your time to comment on this article.

Leave a Comment