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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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())); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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"); } } |
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.