AWT Events in Java

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 by ActionListener interface The events …

What is ActionListener ActionEvent with Example? Read More »

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 »

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 »

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 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 »

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 MouseMotionListener is an interface and …

MouseMotionListener vs MouseMotionAdapter Read More »

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 implements WindowListener, WindowStateListener, WindowFocusListener WindowListener …

WindowListener vs WindowAdapter 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 »