What is awt event in java

AWT Event Handling

Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen.

Types of Event

The events can be broadly classified into two categories:

  • Foreground Events — Those events which require the direct interaction of user.They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page etc.
  • Background Events — Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.

What is Event Handling?

Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.Let’s have a brief introduction to this model.

Читайте также:  Валидация кода это html

The Delegation Event Model has the following key participants namely:

  • Source — The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it’s handler. Java provide as with classes for source object.
  • Listener — It is also known as event handler.Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns.

The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user interface element is able to delegate the processing of an event to the separate piece of code. In this model ,Listener needs to be registered with the source object so that the listener can receive the event notification. This is an efficient way of handling the event because the event notifications are sent only to those listener that want to receive them.

Steps involved in event handling

  • The User clicks the button and the event is generated.
  • Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
  • Event object is forwarded to the method of registered listener class.
  • the method is now get executed and returns.

Points to remember about listener

  • In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class.
  • If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
Читайте также:  Php сгенерировать уникальное значение

Callback Methods

These are the methods that are provided by API provider and are defined by the application programmer and invoked by the application developer. Here the callback methods represents an event method. In response to an event java jre will fire callback method. All such callback methods are provided in listener interfaces.

If a component wants some listener will listen to it’s events the the source must register itself to the listener.

Event Handling Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo < private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo()< prepareGUI(); >public static void main(String[] args) < AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showEventDemo(); >private void prepareGUI() < mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent windowEvent)< System.exit(0); >>); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); > private void showEventDemo() < headerLabel.setText("Control in action: Button"); Button okButton = new Button("OK"); Button submitButton = new Button("Submit"); Button cancelButton = new Button("Cancel"); okButton.setActionCommand("OK"); submitButton.setActionCommand("Submit"); cancelButton.setActionCommand("Cancel"); okButton.addActionListener(new ButtonClickListener()); submitButton.addActionListener(new ButtonClickListener()); cancelButton.addActionListener(new ButtonClickListener()); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); >private class ButtonClickListener implements ActionListener < public void actionPerformed(ActionEvent e) < String command = e.getActionCommand(); if( command.equals( "OK" )) < statusLabel.setText("Ok Button clicked."); >else if( command.equals( "Submit" ) ) < statusLabel.setText("Submit Button clicked."); >else < statusLabel.setText("Cancel Button clicked."); >> > >

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

Verify the following output

Источник

Package java.awt.event

The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses.

The listener interface for receiving «interesting» mouse events (press, release, click, enter, and exit) on a component.

The listener interface for receiving WindowEvents , including WINDOW_GAINED_FOCUS and WINDOW_LOST_FOCUS events.

A class which extends the EventListenerProxy specifically for adding an AWTEventListener for a specific event mask.

A low-level event which indicates that a component moved, changed size, or changed visibility (also, the root class for the other component-level events).

A low-level event which indicates that a container’s contents changed because a component was added or removed.

An event which executes the run() method on a Runnable when dispatched by the AWT event dispatcher thread.

Package java.awt.event Description

Provides interfaces and classes for dealing with different types of events fired by AWT components. See the java.awt.AWTEvent class for details on the AWT event model. Events are fired by event sources. An event listener registers with an event source to receive notifications about the events of a particular type. This package defines events and event listeners, as well as event listener adapters, which are convenience classes to make easier the process of writing event listeners.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Package java.awt.event

Provides interfaces and classes for dealing with different types of events fired by AWT components. See the java.awt.AWTEvent class for details on the AWT event model. Events are fired by event sources. An event listener registers with an event source to receive notifications about the events of a particular type. This package defines events and event listeners, as well as event listener adapters, which are convenience classes to make easier the process of writing event listeners.

The listener interface for receiving notification of events dispatched to objects that are instances of Component or MenuComponent or their subclasses.

The listener interface for receiving «interesting» mouse events (press, release, click, enter, and exit) on a component.

The listener interface for receiving WindowEvents , including WINDOW_GAINED_FOCUS and WINDOW_LOST_FOCUS events.

A class which extends the EventListenerProxy specifically for adding an AWTEventListener for a specific event mask.

A low-level event which indicates that a component moved, changed size, or changed visibility (also, the root class for the other component-level events).

A low-level event which indicates that a container’s contents changed because a component was added or removed.

An event which executes the run() method on a Runnable when dispatched by the AWT event dispatcher thread.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью