Low-level Events

Java hardware events like mouse and keyboard.

Java Made Clear: Java Mouse Event Mouse Click Example

When mouse is clicked, mouse generates an event called mouse event represented by MouseEvent class. MouseEvent is handled by MouseListener. Explained in simple terms. Whenever, a GUI component is interacted by the user, it generates an event. For example, if the user clicks over a button, the button generates an event known as ActionEvent. Similarly, …

Java Made Clear: Java Mouse Event Mouse Click Example Read More »

Mouse Multiple Clicks getClickCount()

getClickCount() of MouseEvent class returns the number of clicks done on mouse. Following is the method signature as defined in class java.awt.event.MouseEvent public int getClickCount(): Returns the number of mouse clicks associated with this event. Mouse Multiple Clicks getClickCount() program uses getClickCount() to know the number of clicks done on the button. import java.awt.*; import …

Mouse Multiple Clicks getClickCount() 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 »

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 »

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 »

Applet Mouse Event MouseAdapter Example

We have seen earlier a similar program Handling Mouse Events with MouseAdapter Example with an introduction to MouseAdapter, but the window here is frame. Let us do another program with applet window. The following program displays a message "Mouse is clicked" in the applet window and "MouseMotionAdapter is in action" at status bar when mouse …

Applet Mouse Event MouseAdapter Example 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 »

MouseMotionAdapter Example Java

MouseMotionListener is replaced by MouseMotionAdapter to make mouse event handling code easier and readable. Example given with Screenshot in Simple terms. Go on practice the program. Adapters were introduced from JDK 1.1 to make event handling code easier. Adapters replace listener interfaces. The advantage of adapters over listeners is adapters allow the Programmer to override …

MouseMotionAdapter Example Java 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 Mouse Cursor Location

A similar program is available in frame – Finding x and y Coordinates of Mouse Position. The same frame program is converted to applet. Following is the example. The mouse cursor location in x and y Coordinates and the type of action are displayed on the applet window and also on the applet status bar. …

Find X Y Coordinates Mouse Cursor Location 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 »

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 »

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 »

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 »

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 »

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 »

Handling MouseEvent with MouseListener Example

MouseEvent is handled by MouseListener overriding 5 abstract methods. Example given with Screenshot in Simple terms. Java events are of two types – a) generated by frame, button etc. (AWT components) known as semantic events and b) generated by mouse, keyboard (Hardware components) known as low-level events. When you click a mouse, the mouse generates …

Handling MouseEvent with MouseListener Example Read More »

Java Tutorial Key Event, Keyboard Input and KeyListener

Keyboard generate KeyEvent and handled by KeyListener overriding 3 abstract methods. Example given with Screenshot where key typed is displayed in TextField. Every component either software (like button) or hardware (like keyboard or mouse) when interacted by the user generates an event. The events can be categorized into two – Semantic and Low-level. Semantic events …

Java Tutorial Key Event, Keyboard Input and KeyListener 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 »

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 »