Series: Mouse Keyboard

Event handling with mouse and keyboard explained in simple terms and programs for a beginne

MouseMotionAdapter Example getX() getY() Cursor Location

MouseMotionAdapter Example is a good example to integrate classes. Two classes extending Applet and MouseMotionAdapter are integrated through their constructors with Composition – "has-a" relationship. Let us see the logic of the code. Before this, if not acquainted with adapters, go through Java AWT Adapters. As usual applet requires a HTML file to run. Following …

MouseMotionAdapter Example getX() getY() Cursor Location Read More »

MouseListener Java Example

The events of Java AWT can be divided into two broad categories – Semantic events and Low-level events. Semantic events are generated by software (GUI) components like frame, button etc and low-level events are generated by hardware components like mouse, keyboard etc. You have seen so far the semantic events and their handling. Now let …

MouseListener Java Example Read More »

MouseMotionListener Example Applet

Note: Before going into this tutorial, it is advised to have better knowledge of Applet. Applets are also capable to raise mouse and key events like with frame. Following program illustrates how to use MouseMotionListener with applet. MouseMotionListener Example as Applet HTML file to run the applet Java applet File Name: AppletMouseMotionListener.java import java.applet.Applet; import …

MouseMotionListener Example Applet Read More »

MouseListener and MouseMotionListener Example

We did two separate programs on mouse event handling with MouseListener and MouseMotionListener. Now let us have an example implementing both mouse listener interfaces. It is very simple, but required to override 5 abstract methods of MouseListener and 2 abstract methods of MouseMotionListener. To make the program simple, just I am changing the background color …

MouseListener and MouseMotionListener Example Read More »

Anonymous Inner class with Listeners Adapters

Anonymous inner classes were introduced, at the first, to make event handling simpler and easier. With inner classes, forget the laborious days of implementing listeners and extending adapters classes. Now the code comes right at the place of linking the listener with the component in the constructor itself. Two programs are given (using Anonymous Inner …

Anonymous Inner class with Listeners Adapters Read More »

MouseListener MouseAdapter Applet

We know in Java, every listener interface that has got more than one abstract method comes with a corresponding adapter class. Usage of adapters classes in Java programming makes coding simple. Two programs (one as applet and the other as frame) are given to handle the events of mouse with MouseListener and MouseAdapter. To understand …

MouseListener MouseAdapter Applet Read More »

KeyListener Example

After seeing the low-level events handling with MouseListener and MouseMotionListener, let us go to the actions of keyboard. Keyboard actions are handled by KeyListener. Keyboard generates KeyEvent. Three types of actions can be done with keyboard – by pressing the key, by releasing the key and typing the key. These actions are represented by three …

KeyListener Example Read More »

Handling Mouse Events with MouseAdapter Example

1. What is MouseListener? We know, java.awt.event package comes with many event classes and listener interfaces to handle the events raised by AWT components like frame or button etc (known as semantic events). But the same package also comes with listeners that can handle the events of hardware components, known as low-level events. Two listeners …

Handling Mouse Events with MouseAdapter Example Read More »

Find x, y Coordinates of Mouse Position Example

We have seen earlier to handle mouse events separately with MouseListener and MouseMotionListener and also both listeners in a single program. In this Mouse Position Example also, both listeners are implemented but the task is to find the x and y coordinates where mouse action take place on the frame. To find, the MouseEvent class …

Find x, y Coordinates of Mouse Position Example Read More »

KeyAdapter Example getKeyChar() getKeyCode()

Note 1: Before going into this notes, if not aware of about adapter classes, It is advised to go through Java AWT Adapters. Note 2: For notes on integration of classes (it is done here also), refer Applet: MouseMotionAdapter getX() and getY() Example . Two methods getKeyChar() and getKeyCode() of KeyEvent class are used in …

KeyAdapter Example getKeyChar() getKeyCode() Read More »

MouseEvent MouseListener Example Applet

Note: Before going into this tutorial, it is advised to have better knowledge of Applet. What mouse events and key events possible with frame are also possible with applet because frame and applet are containers (that can hold AWT components) derived from the same java.awt.Container class. Following is their hierarchy. As usual every applet file …

MouseEvent MouseListener Example Applet Read More »

KeyAdapter and KeyEvent Simple Example

Java permits to handle low-level events of mouse and keyboard. For keyboard the java.awt.event package comes with KeyListener and KeyAdapter. KeyAdapter is an abstract class that replaces the usage of KeyListener. Instead of implementing KeyListener and overriding its 3 abstract methods, we can as well extend KeyAdapter and override the interested abstract methods – may …

KeyAdapter and KeyEvent Simple Example Read More »

Keyboard Input KeyEvent KeyListener Example Applet

We have seen earlier to handle the mouse stable and mouse movement events with Applet. Now let us see with keyboard. A similar program is available with frame. In this simple KeyListener Example, depending on the key typed by the user, the words are displayed in the applet window. getKeyChar() of KeyEvent returns the key …

Keyboard Input KeyEvent KeyListener Example Applet Read More »

Mouse Right Click Example Java

This Java code on Mouse Right Click Example explains how to handle the right clicks of mouse key. Mouse Right Click Example import java.awt.*; import java.awt.event.*; public class Demo extends Frame { Button btn; public Demo() { setLayout(new FlowLayout()); btn = new Button(“OK”); btn.addMouseListener(new MyListener()); add(btn); setSize(300,300); setVisible(true); } public static void main(String args[]) { …

Mouse Right Click Example Java Read More »

MouseListener vs MouseAdapter

MouseListener vs MouseAdapter We know earlier the difference between a listener and adapter. Let us learn now MouseListener vs MouseAdapter. First let us discuss interms of API. Following are the signatures as defined in Java API. 1. public interface MouseListener extends EventListener 2. public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener MouseListener is an interface …

MouseListener vs MouseAdapter Read More »

Mouse Double Click Example Java

The code explains how to handle double clicks of mouse key. You even can handle triple clicks also, if required. Example on Mouse Double Click Example import java.awt.*; import java.awt.event.*; public class Demo extends Frame { Button btn; public Demo() { setLayout(new FlowLayout()); btn = new Button(“OK”); btn.addMouseListener(new MyListener()); add(btn); setSize(300,300); setVisible(true); } public static …

Mouse Double Click Example Java Read More »

Handling MouseEvent with MouseMotionListener Example

There is no MouseMotionEvent in java. The same MouseEvent used in MouseListener is used in MouseMotionListener also. All the abstract methods of both the interfaces takes MouseEvent as parameter. Two types of events can be generated with by mouse button when the mouse is in motion. One is just moving the mouse on a component …

Handling MouseEvent with MouseMotionListener Example Read More »

MouseListener vs MouseMotionListener

The java.awt.event package comes with two interfaces to handle the events of mouse – MouseListener and MouseMotionListener. They are differentiated by the state of the mouse (whether the mouse is moving or not moving) when an event generated. MouseListener vs MouseMotionListener 1. Implement MouseListener when the mouse is stable while handling the mouse event. 2. …

MouseListener vs MouseMotionListener Read More »