AWT Events in Java

What is ActionListener ActionEvent with Example?

Every GUI component when interacted by the user (like by clicking over a button or pressing enter key in a text field etc.), the component generates an event. Handling the event and executing some method is known as event handling. Button generates an event known as ActionEvent and is handled…

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…

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…

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…

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 vs MouseMotionAdapter

we know earlier difference between a listener and adapter. Now let us learn specifically with MouseMotionListener and MouseMotionAdapter. First let us discuss interms of API MouseMotionListener vs MouseMotionAdapter Following are the signatures as defined in Java API. 1. public interface MouseMotionListener extends EventListener 2. public abstract class MouseMotionAdapter implements MouseMotionListener…

WindowListener vs WindowAdapter

WindowListener vs WindowAdapter We know earlier difference between a listener and adapter. Now let us dig for WindowListener vs WindowAdapter. First let us discuss interms of API WindowListener vs WindowAdapter. Following are the signatures as defined in Java API. 1. public interface WindowListener extends EventListener 2. public abstract class WindowAdapter…

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…

Listener vs Adapter

Listener vs Adapter: Explanation, Importance, Replacement etc. are discussed. 1. What is an event? Whenever the user interacts with a component, the component generates an event. Here component may be a window frame, button, text field or a mouse click. The interaction by the user with the component may be…

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…

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…

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"…

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…

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…

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…

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…

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()…

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…

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.…

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.…