0% found this document useful (0 votes)
7 views15 pages

UNIT - 4 Java

The document provides an overview of Java Foundation Classes (JFC), including Swing and AWT for building graphical user interfaces (GUIs). It explains components, containers, layout managers, event handling, and JDBC for database connectivity in Java applications. Key concepts such as event delegation, JDBC drivers, and connection management are also discussed.

Uploaded by

f49077041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

UNIT - 4 Java

The document provides an overview of Java Foundation Classes (JFC), including Swing and AWT for building graphical user interfaces (GUIs). It explains components, containers, layout managers, event handling, and JDBC for database connectivity in Java applications. Key concepts such as event delegation, JDBC drivers, and connection management are also discussed.

Uploaded by

f49077041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT – 4

JFC stands for Java Foundation Classes.


It is a set of APIs (Application Programming Interfaces) provided by Java to build rich, platform-
independent graphical user interfaces (GUIs) for desktop applications.

❖ Swing :-

Swing is a part of Java's standard library used for building Graphical User Interfaces (GUIs). It is part of the
Java Foundation Classes (JFC) and provides a rich set of GUI components for creating desktop applications.

What is AWT in Java?

AWT stands for Abstract Window Toolkit.


It is the original platform-dependent GUI toolkit in Java, used to create windows-based applications.

AWT is part of the Java Foundation Classes (JFC), but Swing (which came later) is built on top of AWT
and is more advanced.
COMPONENTS in Java:

A Component is an object having a graphical representation that can be displayed on the screen and can
interact with the user.

Common Characteristics:

• Visual elements (buttons, text fields, labels, etc.)


• Can trigger events (click, input, selection)
• Can be added to a container

Examples of Swing Components:

Component Description
JLabel Displays a short text or image
ImageIcon It allows you to load and display an image in a JLabel.
JButton Clickable button
JTextField Allows single-line text input
JTextArea Multi-line editable text area
JCheckBox A checkbox to toggle true/false
JRadioButton Used in groups for selecting one option
JComboBox Drop-down menu
JList A list of selectable items
JTable Displays tabular data
JScrollBar Scrollbar used with scrollable content

Full Example: Using All Components in One GUI


Containers:
Swing defines two types of containers.
1. Top-level containers/ Root containers: JFrame, JApplet,JWindow, and JDialog.
As the name implies, a top-level container must be at the top of a containment hierarchy. A top-level
container is not contained within any other container. Furthermore, every containment hierarchy must
begin with a top-level container. The one most commonly used for applications are JFrame and JApplet.
Unlike Swing’s other components, the top-level containers are heavyweight. Because they inherit AWT
classes Component and Container.
Whenever we create a top level container four sub-level containers are automatically created:
Glass pane (JGlass)
Root pane (JRootPane)
Layered pane (JLayeredPane)
Content pane

2. Lightweight containers – Containers do inherit JComponent. An example of a lightweight container is


JPanel, which is a general-purpose container. Lightweight containers are often used to organize and manage
groups of related components.
Layout Managers in Java Swing
─────────────────────────────

Layout managers are used to arrange components (like buttons, labels, text fields) in a container (like
JFrame or JPanel) in a specific pattern or layout.

Why Layout Managers?


They provide a flexible way to automatically position and resize components depending on the container's
size and resolution.

Common Layout Managers:

1. FlowLayout
2. BorderLayout
3. GridLayout

Event Delegation Model in Java


──────────────────────────────

Java uses the Event Delegation Model to handle user interactions such as mouse clicks, key presses, or
button actions.

Key Concepts:

1. Event Source – The GUI component that generates the event (e.g., JButton)
2. Event Object – Carries information about the event (e.g., ActionEvent)
3. Event Listener – Interface that receives the event and performs some action (e.g., ActionListener)
What Is Event Handling in Java?

Event Handling in Java refers to the mechanism that allows your GUI program to respond to user
interactions such as:

• Clicking a button
• Pressing a key
• Selecting an item from a combo box, etc

Core Concepts:

1. Event Source: Component that generates events (e.g. JButton, JTextField).


2. Event Listener: Object that listens and responds to the event (implements a listener interface).
3. Event Class: Encapsulates information about the event (e.g. ActionEvent, MouseEvent).
4. Listener Interface: Interface to implement (e.g. ActionListener, MouseListener).
5. Adapter Class: Provides empty implementation of listener interface, used to override only required
methods (e.g. MouseAdapter, KeyAdapter).
JDBC (Java Database Connectivity)

JDBC is an API that helps applications to communicate with databases, it allows Java programs to connect
to a database, run queries, retrieve, and manipulate data. Because of JDBC, Java applications can easily
work with different relational databases like MySQL, Oracle, PostgreSQL, and more.

Explanation:
Application: It can be a Java application or servlet that communicates with a data source.
The JDBC API: It allows Java programs to execute SQL queries and get results from the database. Some
key components of JDBC API include -
o Interfaces like Driver, ResultSet, RowSet, PreparedStatement, and Connection that helps
managing different database tasks.
o Classes like DriverManager, Types, Blob, and Clob that helps managing database connections.
DriverManager: Driver manager is responsible for loading the correct database-specific driver to establish
a connection with the database. It manages the available drivers and ensures the right one is used to
process user requests and interact with the database.
It plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively
connect enterprise applications to databases.
JDBC Test Suite: It is used to test the operation(such as insertion, deletion, updating) being performed by
JDBC Drivers.
JDBC drivers: These drivers handle interactions between the application and the database. JDBC drivers
are client-side adapters (installed on the client machine, not on the server) that convert requests from Java
programs to a protocol that the DBMS can understand.
Connecting to the Database using JDBC
To connect a Java application to a database, you need to perform the following steps using JDBC:

Step 1: Import the Packages


Step 2: Load the drivers using the forName() method
Step 3: Register the drivers using DriverManager
Step 4: Establish a connection using the Connection class object
Step 5: Create a statement
Step 6: Execute the query
Step 7: Process the ResultSet
Step 8: Close the connections

JDBC Drivers :-

JDBC (Java Database Connectivity) drivers are essential components that act as a bridge between Java
applications and databases. They allow Java programs to communicate with various database systems by
converting Java calls into database-specific commands. There are four types of JDBC drivers, each with
different architectures and use cases.

1. Type 1 – JDBC-ODBC Bridge Driver: This driver uses the ODBC (Open Database Connectivity)
driver to interact with the database. It acts as a bridge between JDBC and ODBC. However, it's
platform-dependent and no longer supported in Java 8 and above.
2. Type 2 – Native-API Driver: This driver converts JDBC calls into native database API calls using
native libraries provided by the database vendor. It is faster than Type 1 but still platform-dependent.
3. Type 3 – Network Protocol Driver: It communicates with a middleware server, which then
communicates with the database. This driver is platform-independent and suitable for internet-based
applications.
4. Type 4 – Thin Driver (Pure Java Driver): This is the most widely used and recommended driver.
It converts JDBC calls directly into the database-specific protocol without needing native libraries or
middleware. It is platform-independent and ideal for web-based applications.

Connection in JDBC (Java Database Connectivity)

In JDBC, the Connection interface is used to establish a link between a Java application and a database. It
represents a session with a specific database. Through this connection, SQL statements can be executed,
transactions can be managed, and data can be retrieved or updated.

To obtain a Connection object, the DriverManager.getConnection() method is used, which requires the
database URL, username, and password.
Statement in JDBC

The Statement interface in JDBC is used to execute static SQL queries (i.e., queries without parameters)
against the database. It is a part of the java.sql package and works with the Connection interface to send
SQL commands such as SELECT, INSERT, UPDATE, or DELETE.
PreparedStatement in JDBC

The PreparedStatement interface in JDBC is used to execute parameterized SQL queries. It is a


subinterface of Statement, and it's preferred over Statement for security, efficiency, and reusability—
especially when the same query needs to be run multiple times with different values.
Example
Connecting to the Database using JDBC
To connect a Java application to a database, you need to perform the following steps using JDBC:

Steps to Connect:

1. Import JDBC package


2. Load the JDBC driver
3. Establish a connection
4. Create Statement or PreparedStatement
5. Execute the SQL query
6. Process the ResultSet
7. Close all connections

You might also like