Figure 4.1: How the Event Delegation Model Works in Java
Figure 4.1: How the Event Delegation Model Works in Java
1. Event SourceGUI Component (e.g.,
Button) registers a Listener via addActionListener()→ User Clicks →
2. Event ObjectSource creates
ActionEvent object & sends it→ Calls Method →
3. Event ListenerExecutes
actionPerformed(ActionEvent e)Explanation
- A source creates an event when an action occurs.
- The registered listener receives it and runs the relevant callback.
Figure 4.2: The Java Event Class Hierarchy (java.util.EventObject → java.awt.AWTEvent)
Figure 4.2: The Java Event Class Hierarchy (java.util.EventObject → java.awt.AWTEvent)
java.util.EventObject (
getSource()) → java.awt.AWTEvent (getID())
Direct Semantic Subclasses of AWTEvent
•
•
•
•
•
ActionEvent (Button click, Menu)•
ItemEvent (Checkbox, Choice)•
AdjustmentEvent (Scrollbar)•
TextEvent (TextField/TextArea)
ComponentEvent & Its Subclasses
•
•
•
•
•
ContainerEvent (Component added/removed)•
FocusEvent (Gained/lost focus)•
WindowEvent (Window opened/closed)•
InputEvent → KeyEvent & MouseEvent
Explanation
- EventObject is the general event base and AWTEvent is an AWT-specific branch.
- More specific event classes carry details such as a key or mouse position.
Figure 4.3: Implementing WindowListener vs Extending WindowAdapter
Figure 4.3: Implementing WindowListener vs Extending WindowAdapter
Without Adapter (implements WindowListener)
- Must write all 7 methods!
windowClosing()(Actual code)- Plus 6 empty methods:
windowOpened(){},windowClosed(){},windowIconified(){}, etc.
With Adapter (extends WindowAdapter)
WindowAdapteralready provides empty bodies for all 7 methods- You override ONLY 1 method:
windowClosing(WindowEvent e)! - Code is clean, short, and readable
Explanation
- Implementing WindowListener requires providing its listener methods.
- Extending WindowAdapter lets a class override only the callbacks it needs.
Figure 4.4: Core Building Blocks of Java Network Communication
Figure 4.4: Core Building Blocks of Java Network Communication
1. IP Address
Unique numerical street address of a computer on the network (e.g., 192.168.1.1)
2. Port Number (0–65535)
16-bit apartment/door number identifying a specific application on that computer (e.g., HTTP = 80)
3. Protocol
Set of communication rules: TCP (Reliable, Connection-oriented) & UDP (Fast, Connectionless)
4. Socket (IP + Port)
The two-way communication endpoint connecting a Client and a Server
Explanation
- An address identifies a network interface and a port identifies an endpoint.
- Sockets give the Java program a way to communicate through those endpoints.
Structure of a Uniform Resource Locator (URL)
Structure of a Uniform Resource Locator (URL)
Protocol: https
Specifies the communication rule (HTTP, HTTPS, FTP) to retrieve the resource.
Host Name: www.campusnotes.in
Domain name or IP address identifying the server on the network.
Port: 443
Target service endpoint on the server (443 for HTTPS, 80 for HTTP).
File Path: /davv/bca/java.html
Exact location of the web page or document on the server's filesystem.
Explanation
- The scheme identifies the protocol and the host identifies the destination machine.
- The path identifies a resource and an optional port selects the service endpoint.
Figure 4.5: How TCP/IP Client-Server Socket Communication Works
Figure 4.5: How TCP/IP Client-Server Socket Communication Works
1. ServerSocket(5000)Server opens port 5000 & waits at
accept()← Connection Request ←
2. Socket("localhost", 5000)Client requests connection to Server IP & Port
↔ Two-Way Streams ↔
3. InputStream / OutputStreamBoth exchange messages & call
close()Explanation
- A server socket listens while the client requests a connection.
- After accept succeeds, the connected sockets exchange bytes through streams.