The Java.
lang package
[Link] package contains the classes that are fundamental to the
design of the Java programming language.
[Link] package classes contains fundamental classes like wrapper
classes, classes for thread operations, classes for String manipulation, classes
for mathematics operations etc.
classes can be imported using following syntax:
import [Link].*;
Here we've used * operator to import all classes from [Link] package and
now any class can be used in the program.
Important classes in [Link] Package :
Boolean
Byte
Character
Double
Enum
Float
Integer
Long
Math
Number
Number
Package
String
StringBuffer
System
[Link] Package in Java
The [Link] package is one of the most widely used packages in the
Java programming language. It provides a set of utility classes that support data
structures, date and time manipulation, random number generation, event
handling, and other commonly used functionalities in Java programs.
Provides a rich Collection Framework (List, Set, Map) for data handling.
Includes utility classes (Collections, Arrays) for sorting and searching.
Offers Date, Calendar, and Random for time and random operations.
Supports Properties for configuration management.
Contains concurrent and legacy classes for compatibility.
import java. util.*;
This single import statement allows access to all classes and interfaces inside
the [Link] package.
Scanner class
In Java, the Scanner class is present in the [Link] package is used to
obtain input for primitive types like int, double, etc., and strings. We can use
this class to read input from a user or a file.
The scanner class helps take the standard input stream in Java. So, we
need some methods to extract data from the stream like:
Method Description
Used for reading Boolean value
nextBoolean()
nextByte() Used for reading Byte value
nextDouble() Used for reading Double value
nextFloat() Used for reading Float value
Method Description
nextInt() Used for reading Int value
nextLine() Used for reading Line value
AWT
AWT stands for Abstract Window Toolkit. It is a platform-dependent
API to develop GUI (Graphical User Interface) or window-based applications in
Java.
It contains a large number of classes and methods, which are used for
creating and managing GUI.
It includes various editing tools like graphics tool and imaging tools.
It provides functionality to include shapes, colors and font classes.
Event Handling in Java
An event is a change in the state of an object triggered by some
action such as Clicking a button, Moving the cursor, Pressing a key on the
keyboard, Scrolling a page, etc. \
In Java, the [Link] package provides various event classes to
handle these actions.
Event handling is a mechanism that allows programs to control events
and define what should happen when an event occurs. Java uses the
Delegation Event Model to handle events. This model consists of two main
components:
Source: Events are generated from the source. There are various
sources like buttons, checkboxes, list, menu-item, choice, scrollbar, text
components, windows, etc., to generate events.
Listeners: Listeners are used for handling the events generated from
the source. Each of these listeners represents interfaces that are responsible
for handling events.
Event Classes and Listener Interfaces
Java provides a variety of event classes and corresponding listener
interfaces.
Event Class Listener Interface Description
An event that indicates that a
component-defined action
ActionEvent ActionListener occurred like a button click or
selecting an item from the
menu-item list.
The adjustment event is
AdjustmentEvent AdjustmentListener emitted by an Adjustable
object like Scrollbar.
An event that indicates that a
component moved, the size
ComponentEvent ComponentListener
changed or changed its
visibility.
These are focus-related
FocusEvent FocusListener events, which include focus,
focusin, focusout, and blur.
An event that indicates
ItemEvent ItemListener whether an item was selected
or not.
An event that occurs due to a
KeyEvent KeyListener sequence of keypresses on
the keyboard.
The events that occur due to
MouseListener &
MouseEvent the user interaction with the
MouseMotionListener
mouse (Pointing Device).
An event which indicates
WindowEvent WindowListener whether a window has
changed its status or not.
Methods in Listener Interfaces
Each listener interface contains specific methods(abstract methods) that
must be implemented to handle events. Below table demonstrates the key
methods for each interface:
Listener Interface Methods
ActionListener actionPerformed()
adjustmentValueChanged(
AdjustmentListener
componentResized()
componentShown()
ComponentListener
componentMoved()
componentHidden()
focusGained()
FocusListener
focusLost()
keyTyped()
KeyListener keyPressed()
keyReleased()
mousePressed()
mouseClicked()
MouseListener mouseEntered()
mouseExited()
mouseReleased()
mouseMoved()
MouseMotionListener
mouseDragged()
windowActivated()
windowDeactivated()
windowOpened()
WindowListener
windowClosed()
windowClosing()
windowIconified()
Listener Interface Methods
windowDeiconified()
Flow of Event Handling
The event handling process in Java follows these steps:
1. User Interaction with a component is required to generate an event.
2. The object of the respective event class is created automatically after
event generation, and it holds all information of the event source.
3. The newly created object is passed to the methods of the registered
listener.
4. The method executes and returns the result.
Adapter Classes in Java
In Java's event handling mechanism, adapter classes are abstract
classes provided by the Java AWT (Abstract Window Toolkit) package for
receiving various events. These classes contain empty implementations of the
methods in an event listener interface, providing a convenience for creating
listener objects.
The adapter classes in Java are :
• WindowAdapter
• KeyAdapter
• MouseAdapter
• FocusAdapter
• ContainerAdapter
• ComponentAdapter
These adapter classes implement interfaces like WindowListener,
KeyListener, MouseListener, FocusListener, ContainerListener, and
ComponentListener respectively, which contain methods related to specific
events.
Benefits of Java Adapter Classes
Code Optimization - By using adapter classes, we only need to override
the required methods, rather than implementing all methods in an event
listener interface. This results in cleaner, more maintainable code.
Flexibility - Adapter classes provide the flexibility to create listener
objects specific to our requirements, enhancing the program's overall
efficiency
Easier Event Handling - Adapter classes simplify the event handling
process in Java by providing a default implementation of event listener
interfaces