Sunday, July 16, 2023

AWT Classes

 The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Fortunately, because it is logically organized in a top-down, hierarchical fashion.









Event Listener Interfaces

 Event Listener Interfaces

 event model has two parts: sources and listeners. 

Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument. 




Sources of Events


 

The TextEvent Class

 

The TextEvent Class Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or program. TextEvent defines the integer constant TEXT_VALUE_CHANGED. 

The one constructor for this class is shown here: TextEvent(Object src, int type) Here, src is a reference to the object that generated this event. The type of the event is specified by type.

The MouseEvent Class

 




Here, src is a reference to the component that generated the event.  The system time at which the mouse event occurred is passed in when. 

The modifiers argument indicates which modifiers were pressed when a mouse event occurred. The coordinates of the mouse are passed in x and y. The click count is passed in clicks. 

The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.

 Two commonly used methods in this class are getX( ) and getY( ). 

These return the X and Y coordinates of the mouse within the component when the event occurred. 


Point getPoint( ) It returns a Point object that contains the X,Y coordinates in its integer members: x and y. 

The translatePoint( ) method changes the location of the event. Its form is shown here: void translatePoint(int x, int y) Here, the arguments x and y are added to the coordinates of the event. 

The getClickCount( ) method obtains the number of mouse clicks for this event. Its signature is shown here: int getClickCount( ) 

The isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform. Its form is shown here:  boolean isPopupTrigger( ) 

 int getButton( ) It returns a value that represents the button that caused the event. The return value will be one of these constants defined by MouseEvent: 

NOBUTTON 

BUTTON1

 BUTTON2 

BUTTON3 

The NOBUTTON value indicates that no button was pressed or released. Java SE 6 added three methods to MouseEvent that obtain the coordinates of the mouse relative to the screen rather than the component. 

They are shown here: 

Point getLocationOnScreen( ) 

int getXOnScreen( )

 int getYOnScreen( ) 

The getLocationOnScreen( ) method returns a Point object that contains both the X and Y coordinate. The other two methods return the indicated coordinate

























The ComponentEvent Class

 


The AdjustmentEvent Class



Main Event Classes in java.awt.event

 

Event ClassDescription
ActionEventGenerated when a button is pressed, a list item is double-clicked, or a menu item is selected.
AdjustmentEventGenerated when a scroll bar is manipulated.
ComponentEventGenerated when a component is hidden, moved, resized, or becomes visible.
ContainerEventGenerated when a component is added to or removed from a container.
FocusEventGenerated when a component gains or loses keyboard focus.
InputEvent Abstract superclass for all component input event classes.
ItemEventGenerated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected.
KeyEventGenerated when input is received from the keyboard.
MouseEventGenerated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component.
MouseWheelEventGenerated when the mouse wheel is moved
TextEventGenerated when the value of a text area or text field is changed.
WindowEventGenerated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

Events

 Events

an event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse.

Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, software or hardware failure occurs, or an operation is completed.

 

Event Sources

A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event.

A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method. Here is the general form:

public void addTypeListener(TypeListener el)

Here, Type is the name of the event, and el is a reference to the event listener.

For example, the method that registers a keyboard event listener is called addKeyListener( ).

The method that registers a mouse motion listener is called addMouseMotionListener( ).

When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. In all cases, notifications are sent only to listeners that register to receive them.

 

Event Listeners

A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. The methods that receive and process events are defined in a set of interfaces found in java.awt.event.

For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and process one or both of these events if it provides an implementation of this interface.

Event Handling

 

1. Events,Event Sources,Event Listeners--  LINK

2. Main Event Classes in java.awt.event--LINK

3.The AdjustmentEvent Class--LINK

4.The ComponentEvent Class--LINK

5.The MouseEvent ClassLINK

6.The TextEvent Class LINK

7.Sources of Events link

8.Event Listener Interfaces LINK

9.AWT Classes LINK

10.Container class  LINK