Event Handling
Event Handling
Event Handling:
* Definition: Event handling is the mechanism in Java that allows you to respond to actions
performed by the user (like clicking a button, typing in a text field) or by the system.
* Event-Driven Programming: GUI programming is largely event-driven. The program waits for
events to occur and then reacts accordingly.
* Key Components:
* Event Source: The component that generates the event (e.g., a Button, a TextField, a
Window).
* Event Object: An object that encapsulates information about the event that occurred (e.g.,
the type of event, the source of the event, coordinates of a mouse click). These objects are
typically subclasses of java.util.EventObject.
* Event Listener: An object that is notified when a particular type of event occurs. Listeners
implement specific listener interfaces found in the java.awt.event package (e.g., ActionListener,
MouseListener, KeyListener).
* Event Handler: The method within the listener that contains the code to respond to the
event.
2. Event Types:
The java.awt.event package defines various event classes representing different user
interactions. Some common event types include:
* ActionEvent: Generated when a component like a button is clicked, a menu item is selected,
or a text field receives an Enter key press.
* MouseEvent: Generated when mouse actions occur within a component (e.g., pressing,
releasing, clicking, entering, exiting, moving).
* KeyEvent: Generated when keyboard keys are pressed, released, or typed within a
component that has focus.
* WindowEvent: Generated when window-related actions occur (e.g., opening, closing,
activating, deactivating).
* ItemEvent: Generated when the state of an item changes in a component like a Checkbox or
a List.
* TextEvent: Generated when the text in a TextField or TextArea changes.
* (Many other event types exist for more specific interactions)
3. Event Handling Mechanism (Delegation Event Model):
AWT uses the delegation event model, where the responsibility for handling events is delegated
to listener objects. This model offers better organization and efficiency compared to older
models.
* Registration: Components (event sources) maintain a list of listeners interested in specific
types of events. You need to register a listener with a component to receive notifications about
its events. This is typically done using methods like addActionListener(), addMouseListener(),
addKeyListener(), etc.
* Notification: When an event occurs on a component, the component notifies all the registered
listeners of the appropriate type by calling the relevant event handler methods defined in the
listener interface.
4. Keyboard and Mouse Handling:
* Keyboard Handling:
* To handle keyboard events, you need to implement the KeyListener interface, which defines
three methods:
* keyPressed(KeyEvent e): Called when a key is pressed down.
* keyReleased(KeyEvent e): Called when a key is released.
* keyTyped(KeyEvent e): Called when a character is typed (usually a result of pressing and
releasing a key).
* You then register your KeyListener object with the component you want to monitor for
keyboard events using addKeyListener().
* Mouse Handling:
* To handle general mouse events (press, release, click, enter, exit), implement the
MouseListener interface:
* mousePressed(MouseEvent e)
* mouseReleased(MouseEvent e)
* mouseClicked(MouseEvent e)
* mouseEntered(MouseEvent e)
* mouseExited(MouseEvent e)
* For mouse motion events (moving the mouse or dragging it), implement the
MouseMotionListener interface:
* mouseMoved(MouseEvent e)
* mouseDragged(MouseEvent e)
* Register the appropriate listener(s) with the component using addMouseListener() and/or
addMouseMotionListener().
5. Introduction to AWT & GUI Basics:
* Abstract Window Toolkit (AWT): AWT is Java's original platform-dependent GUI toolkit. It
provides a set of classes and interfaces for creating and managing graphical user interfaces.
AWT components are "heavyweight," meaning they rely on the underlying operating system's
native GUI components.
* GUI Basics:
* Components: Visual elements that users interact with, such as buttons (Button), labels
(Label), text fields (TextField), checkboxes (Checkbox), lists (List), etc. These are typically
subclasses of java.awt.Component.
* Containers: Components that can hold other components. The main containers in AWT are
Window, Frame, Panel, and Dialog.
* Window: A top-level window with no borders or title bar.
* Frame: A top-level window with a title bar and borders. It is typically used as the main
window of an application.
* Panel: A generic container that can hold other components. It doesn't have its own title bar
or borders and is often used for organizing components within a Frame or another Panel.
* Dialog: A top-level window typically used for short-term interactions with the user (e.g.,
displaying messages, getting input).
6. AWT Controls - Frames, Panels, Layout Managers & Other Controls:
● * Frames (java.awt.Frame): As mentioned earlier, Frame is a
fundamental container for creating application windows. You can add
other components (controls, panels) to a Frame. You can set its title, size,
make it visible, and handle window events (like closing).
* Panels (java.awt.Panel): Panel is a versatile container used to group and organize other
components. They help in creating more complex layouts. You can add components to a Panel
and then add the Panel to a Frame or another Panel.
* Layout Managers (java.awt.LayoutManager interface): Layout managers are responsible for
arranging the components within a container. They automatically adjust the size and position of
components when the container is resized or when components are added or removed.
Common AWT layout managers include:
* FlowLayout: Arranges components in a flow, similar to words in a line.
* BorderLayout: Arranges components in five regions: North, South, East, West, and Center.
* GridLayout: Arranges components in a grid of rows and columns.
* CardLayout: Arranges components in a stack, where only one component is visible at a time.
* GridBagLayout: A more flexible and complex layout manager that allows for more intricate
grid-based arrangements with varying component sizes and spans.
* You set the layout manager for a container using the setLayout() method.
* Other Controls: AWT provides a variety of other GUI controls for user interaction:
* Label: Displays static text.
* Button: Triggers an action when clicked.
* TextField: Allows single-line text input.
* TextArea: Allows multi-line text input.
* Checkbox: Represents a boolean choice.
* CheckboxGroup: Manages a set of checkboxes where only one can be selected at a time
(radio buttons).
* Choice (Dropdown List): Allows the user to select one option from a list.
* List: Displays a scrollable list of items, allowing single or multiple selections.
* Scrollbar: Allows the user to scroll through a large amount of content.
* Canvas: A blank rectangular area where you can draw graphics.