User Interface Events

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.
The event handling involves four types of classes.
1. Event Sources: - Event sources are components, subclasses of java.awt.Component, capable to generate events. The event source can be a button, TextField or a Frame etc.
2. Event classes: - Almost every event source generates an event and is named by some Java class. For example, the event generated by button is known as ActionEvent and that of Checkbox is known as ItemEvent. All the events are listed in java.awt.event package. Following list gives a few components and their corresponding listeners.
Component Event it generates
Button, TextField, List, Menu ActionEvent
Frame WindowEvent
Checkbox, Choice, List ItemEvent
Scrollbar AdjustmentEvent
Mouse (hardware) MouseEvent
Keyboard (hardware) KeyEvent

The events generated by hardware components (like MouseEvent and KeyEvent) are known as low-level events and the events generated by software components (like Button, List) are known as semantic events.
3. Event Listeners: - The events generated by the GUI components are handled by a special group of interfaces known as "listeners". Note, Listener is an interface. Every component has its own listener, say, AdjustmentListener handles the events of scrollbar Some listeners handle the events of multiple components. For example, ActionListener handles the events of Button, TextField, List and Menus. Listeners are from java.awt.event package.
* Listner: - Listeners are a group of interfaces from java.awt.event package. Listeners are capable to handle the events generated by the components like button, choice, frame etc. These listeners are implemented to the class which requires handling of events.
Example: - public class ButtonDemo1 extends Frame implements ActionListener
The class ButtonDemo1 implements ActionListener as ButtonDemo1 includes some buttons which require event handling. The button events (known as action events) are handled by ActionListener.
Even though, some listeners handle the events of a few components, generally every component event is handled by a separate listener. For example, the ActionListener handles the events of Button, TextField, List and Menu.
4. Event Adapters : - When a listener includes many abstract methods to override, the coding becomes heavy to the programmer. For example, to close the frame, you override seven abstract methods of WindowListener, in which, infact you are using only one method. To avoid this heavy coding, the designers come with another group of classes known as "adapters". Adapters are abstract classes defined in java.awt.event package. Every listener that has more than one abstract method has got a corresponding adapter class.
* Adapters: - Adapters are abstract classes from java.awt.event package introduced with JDK 1.1. Adapter classes make event handling simple and easy. Adapters usage replaces the listeners; adapters make event handling easy to the programmer. Every listener that includes more than one abstract method has got a corresponding adapter class. The advantage of adapter is that we can override any one or two methods we like instead of all. But incase of a listener, we must override all the abstract methods. For example, to close the window, all the 7 abstract methods of WindowListener should be overridden atleast with empty bodies. But is not the case with WindowAdapter. With WindowAdapter only one method windowClosing() is enough to override. Adapters can be used in inner classes also and is illustrated in "Java Frame Closing – Anonymous inner class".
Event Listener interface Event Listener Adapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener MouseMotionAdapter
WindowListener WindowAdapter

The values in the parentheses indicate the abstract methods available in the listener interface.
Types of Event
The events can be broadly classified into two categories:
1. 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.
2.. 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.
Steps involved in event handling
1. The User clicks the button and the event is generated.
2. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
3. Event object is forwarded to the method of registered listener class.
4. The method is now get executed and return
Things to remember using Event
1. 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.
2. 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.
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.
Example: -
import java.awt.*;
import java.awt.event.*;
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.");
}
}
}
}
Output: -
C:\java\swing>javac AwtControlDemo.java
C:\java\swing>java AwtControlDemo


Free Web Hosting