- AWT - Graphics
- AWT - Menu
- AWT - Containers
- AWT - Layouts
- AWT - Event Adapters
- AWT - Event Listeners
- AWT - Event Classes
- AWT - Event Handling
- AWT - Controls
- AWT - Environment
- AWT - Overview
- AWT - Home
AWT Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
AWT Event Handpng
What is an Event?
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, cpcking on a button, moving the mouse, entering a character through keyboard,selecting an item from pst, scrolpng 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, cpcking on a button, moving the mouse, entering a character through keyboard,selecting an item from pst, scrolpng 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 Handpng?
Event Handpng 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.
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 pstener is also an object. Listener waits until it receives an event. Once the event is received , the pstener 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 pstener can receive the event notification. This is an efficient way of handpng the event because the event notifications are sent only to those pstener that want to receive them.
Steps involved in event handpng
The User cpcks 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 pstener class.
the method is now get executed and returns.
Points to remember about pstener
In order to design a pstener class we have to develop some pstener interfaces.These Listener interfaces forecast some pubpc abstract callback methods which must be implemented by the pstener class.
If you do not implement the any if the predefined interfaces then your class can not act as a pstener class for a source object.
Callback Methods
These are the methods that are provided by API provider and are defined by the apppcation programmer and invoked by the apppcation 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 pstener interfaces.
If a component wants some pstener will psten to it s events the the source must register itself to the pstener.
Event Handpng Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.javapackage com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; pubpc class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; pubpc AwtControlDemo(){ prepareGUI(); } pubpc 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() { pubpc void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setApgnment(Label.CENTER); statusLabel = new Label(); statusLabel.setApgnment(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 ButtonCpckListener()); submitButton.addActionListener(new ButtonCpckListener()); cancelButton.addActionListener(new ButtonCpckListener()); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } private class ButtonCpckListener implements ActionListener{ pubpc void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if( command.equals( "OK" )) { statusLabel.setText("Ok Button cpcked."); } else if( command.equals( "Submit" ) ) { statusLabel.setText("Submit Button cpcked."); } else { statusLabel.setText("Cancel Button cpcked."); } } } }
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:AWT>javac com utorialspointguiAwtControlDemo.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
Advertisements