In AWT (Abstract Window Toolkit), event handling is the process of detecting and responding to user actions or system events, such as button clicks, mouse movements, and key presses. AWT provides a number of pre-defined event listeners, such as ActionListener, MouseListener, and KeyListener, which can be used to handle these events. To handle an event in AWT, you need to register an event listener with the component that generates the event, using the appropriate add method. When an event occurs, AWT creates an event object and passes it to the registered event listener for processing.
Create the following java program using any editor of your choice in say E:/ > 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();
}
// Prepare the GUI components and set up the main frame
private void prepareGUI() {
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
// Exit the application when the window is closed
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);
}
// Create and display the buttons
private void showEventDemo() {
headerLabel.setText("Control in action: Button");
Button okButton = new Button("OK");
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
// Set action commands for each button
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
// Add an action listener to each button
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
// Add buttons to the control panel
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
// ActionListener for the buttons
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.");
}
}
}
}
In AWT (Abstract Window Toolkit), event handling is accomplished through the use of several classes and interfaces. Some of the key event handling classes and interfaces in AWT include:
1. Event: This is the superclass of all event objects in AWT.
2. EventObject: This is the superclass of all event objects that do not involve user input, such as WindowEvent and ComponentEvent.
3. EventListener: This is the base interface for all event listener interfaces in AWT.
4. EventListenerProxy: This is a convenience class that can be used to create listener proxies.
5. EventMulticaster: This is a utility class that can be used to manage multiple event listeners.
6. EventHandler: This is a convenience class that can be used to create event handling methods that conform to the expected method signature of an event listener.