0% found this document useful (0 votes)
2 views45 pages

Java 5

The document provides an overview of Java Swing, a GUI framework that offers flexible and powerful components for building graphical user interfaces in Java. It explains the features of Swing, including its lightweight nature, platform independence, and the Model-View-Controller (MVC) architecture it follows. Additionally, the document covers Java Database Connectivity (JDBC), detailing its architecture, connection steps, and examples for interacting with a MySQL database.

Uploaded by

Akshara Naik
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)
2 views45 pages

Java 5

The document provides an overview of Java Swing, a GUI framework that offers flexible and powerful components for building graphical user interfaces in Java. It explains the features of Swing, including its lightweight nature, platform independence, and the Model-View-Controller (MVC) architecture it follows. Additionally, the document covers Java Database Connectivity (JDBC), detailing its architecture, connection steps, and examples for interacting with a MySQL database.

Uploaded by

Akshara Naik
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/ 45

MODULE 5

Graphical User Interface & Database


support of Java
CHAPTER 1
SWING

1
SWING FUNDAMENTALS
Java Swing is a GUI Framework that contains a set of classes
provide more powerful and flexible GUI components than AWT.
Swing provides the look and feel of modern Java GUI.
Swing library is an official Java GUI tool kit released by Su
Microsystems.
It is used to create graphical user interface with Java.
Swing classes are defined in javax.swing
javax package and its su
packages.
Java Swing provides platform-independent
platform and lightweig
components.
EDULINE 2
a Swing is a part of Java Foundation Classes (JFC) that is used
ate window-based applications.
s built on the top of AWT (Abstract Windowing Toolkit) API a
tirely written in java
C
he Java Foundation Classes (JFC) are a set of GUI components whi
mplify the development of desktop applications.
he javax.swing package provides classes for java swing API such
Button, JTextField, JTextArea, JRadioButton,
JRadioButton JCheckbox, JMen
ColorChooser etc.

EDULINE 3
Features of Swing
atform Independent:
It is platform independent, the swing components that are used t
build the program are not platform specific.
It can be used at any platform and anywhere.
ghtweight:
Swing components are lightweight which helps in creating the U
lighter.
Swings component allows it to plug into the operating system use
interface framework that includes the mappings for screens o
device and other user interactions like key press and mous
movements.

EDULINE 4
ugging:
It has a powerful component that can be extended to provide th
support for the user interface that helps in good look and feel t
the application.
It refers to the highly modular-based
based architecture that allows it t
plug into other customized implementations and framework fo
user interfaces.
Manageable: It is easy to manage and configure. Its mechanism an
omposition pattern allows changing the settings at run time a
ell. The uniform changes can be provided to the user interfac
ithout doing any changes to application code.

EDULINE 5
MVC:
They mainly follows the concept of MVC that is Model Vie
Controller.
With the help of this, we can do the changes in one componen
without impacting or touching other components.
It is known as loosely coupled architecture as well.
ustomizable:
Swing controls can be easily customized.
customized It can be changed and th
visual appearance of the swing component application
independent of its internal representation.
representation
ch Controls :
Swing provides a rich set of advanced controls like Tre
TabbedPane, slider, colorpicker, and table controls.
EDULINE 6
Difference between AWT and Swing

EDULINE 7
Hierarchy of Java Swing classes

EDULINE 8
The Model-View-Controller
Controller Architecture
Swing uses the model-view-controller
controller architecture (MVC) as th
fundamental design behind each of its components
Essentially, MVC breaks GUI components into three elements. Eac
of these elements plays a crucial role in how the compone
behaves.
The Model-View-Controller is a well known software architectur
pattern ideal to implement user interfaces on computers b
dividing an application intro three interconnected parts

EDULINE 9
Main goal of Model-View-Controller,
Controller, also known as MVC, is
separate internal representations of an application from the wa
information are presented to the user.
Initially, MVC was designed for desktop GUI applications but it
quickly become an extremely popular pattern for designing we
applications too.
MVC pattern has the three components :
Model that manages data, logic and rules of the application
View that is used to present data to user
Controller that accepts input from the user and converts it
ommands for the Model or View.

EDULINE 10
the MVC pattern defines the interactions between these thre
components like you can see in the following figure :

EDULINE 11
The Model receives commands and data from the Controller.
stores these data and updates the View.
The View lets to present data provided by the Model to the user.
The Controller accepts inputs from the user and converts it
commands for the Model or the View.
View

EDULINE 12
COMPONENTS & CONTAINERS
A component is an independent visual control, such as a pus
button or slider.
A container holds a group of components.
components Thus, a container is
special type of component that is designed to hold oth
components.
Swing components inherit from the javax.Swing.JComponent clas
which is the root of the Swing component hierarchy.

EDULINE 13
COMPONENTS
Swing components are derived from the JComponent class.
JComponent provides the functionality that is common to a
components. For example, JComponent supports the pluggab
look and feel.
JComponent inherits the AWT classes Container and Componen
Thus, a Swing component is built on and compatible with an AW
component.
All of Swing’s components are represented by classes define
within the package javax.swing.
The following table shows the class names for Swing components

EDULINE 14
JApplet
JColorChooser • JTogglebutton
JDialog • JViewport
JFrame • JButton
JLayeredPane • JComboBox
JMenuItem • JEditorPane
JPopupMenu • JInternalFrame
JRootPane • JList
JSlider • JOptionPane
JTable • JProgressBar

EDULINE 15
Notice that all component classes begin with the letter J.
For example, the class for a label is JLabel; the class for a pus
button is JButton; and the class for a scroll bar is JScrollBar
CONTAINERS
Swing defines two types of containers.
containers The first are top-lev
containers: JFrame, JApplet, JWindow, and JDialog. The
containers do not inherit JComponent.
JComponent They inherit the AW
classes Component and Container.
Container
The second type container are lightweight and the top-lev
containers are heavyweight. This makes the top-level containers
special case in the Swing component library.

EDULINE 16
n Java, Containers are divided into two types as shown below:

EDULINE 17
ollowing is the list of commonly used containers while designed
UI using SWING.

EDULINE 18
wing Example : A window on the screen.

Output

EDULINE 19
EVENT HANDLING IN SWINGS
The functionality of Event Handling is what is the further step if a
action performed.
Java foundation introduced “Delegation Event Model” i.e describ
how to generate and control the events.
The key elements of the Delegation Event Model are as source an
listeners.
The listener should have registered on source for the purpose
alert notifications.
All GUI applications are event-driven
driven

EDULINE 20
 Java Swing event object
When something happens in the application, an event object
created.
For example, when we click on the button or select an item from
list.
There are several types of events, including ActionEvent, TextEven
FocusEvent, and ComponentEvent.
ComponentEvent
Each of them is created under specific conditions.
An event object holds information about an event that h
occurred.

EDULINE 21
SWING LAYOUT MANAGERS
Layout refers to the arrangement of components within th
container.
Layout is placing the components at a particular position within th
container. The task of laying out the controls is done automatical
by the Layout Manager.
The layout manager automatically positions all the componen
within the container.
Even if you do not use the layout manager, the components a
still positioned by the default layout manager. It is possible to la
out the controls by hand, however, it becomes very difficult
EDULINE 22
Java provides various layout managers to position the contro
Properties like size, shape, and arrangement varies from one layo
manager to the other.
There are following classes that represents the layout managers:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
javax.swing.GroupLayout
javax.swing.ScrollPaneLayout
javax.swing.SpringLayout etc
etc.
EDULINE 23
BorderLayout GridLayout

24
FlowLayout BoxLayout

25
CardLayout GroupLayout

26
xample of JButton

EDULINE 27
xample of JTextField

EDULINE 28
xample of Jlabel - It is used for placing text in a box

EDULINE 29
MODULE 5

CHAPTER 2
JDBC

1
Java DataBase Connectivity (JDBC)
JDBC stands for Java Database Connectivity, which is a standa
Java API for database-independent
independent connectivity between the Jav
programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentione
below that are commonly associated with database usage.
Making a connection to a database
Creating SQL or MySQL statements
Executing SQL or MySQL queries in the database
Viewing & Modifying the resulting records

EDULINE 2
JDBC Architecture
JDBC Architecture consists of two layers
JDBC API: This provides the application-to-JDBC Manage
connection.
JDBC Driver API: This supports the JDBC Manager-to-Drive
Connection.
The JDBC API uses a driver manager and database-specific drive
to provide transparent connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used t
access each data source.

EDULINE 3
Following is the architectural diagram,
diagram which shows the location
the driver manager with respect to the JDBC drivers and the Jav
application

EDULINE 4
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with th
database using JDBC. These steps are as follows:
. Register the Driver class
. Create connection
. Create statement
. Execute queries
. Close connection

EDULINE 5
The forName() method is used to register the driver class.
The getConnection() method of DriverManager class is used
establish connection with the database
The createStatement() method of Connection interface is used
create statement. The object of statement is responsible to execu
queries with the database.
The executeQuery() method of Statement interface is used
execute queries to the database.. This method returns the object
ResultSet that can be used to get all the records of a table.
By closing connection object statement and ResultSet will b
closed automatically. The close() method of Connection interface
used to close the connection.
EDULINE 6
Java Database Connectivity with MySQL
To connect Java application with the MySQL database, we need
follow 5 following steps.
In this example we are using MySql as the database. So we need
know following informations for the mysql database:
. Driver class: The driver class for the mysql database
com.mysql.jdbc.Driver.
. Connection URL: The connection URL for the mysql database
jdbc:mysql://localhost:3306/sonoo
sonoo where jdbc is the API, mysql
the database, localhost is the server name on which mysql
running, we may also use IP address, 3306 is the port numb
and sonoo is the database name.
name We may use any database,
such case, we need to replace the sonoo with our databa
name.
EDULINE 7
Username: The default username for the mysql database is root.
Password: It is the password given by the user at the time of
talling the mysql database. In this example, we are going to use
ot as the password.
Let's first create a table in the mysql database, but before creatin
able, we need to create database first.
create database sonoo;
use sonoo;
create table emp(id int(10),name varchar(40),age int(3));

EDULINE 8
Example to Connect Java Application with mysql database

This example will


fetch all the records o
emp table.

EDULINE 9
Creating a sample MySQL database
eate database SampleDB;
se SampleDB;
REATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`fullname` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`user_id`)

EDULINE 10
Connecting to the database
ring dbURL = "jdbc:mysql://localhost:3306
3306/sampledb";
ring username = "root";
ring password = "secret";
y{
Connection conn = DriverManager.getConnection(dbURL,
DriverManager usernam
assword);
if (conn != null) {
System.out.println("Connected");
}
catch (SQLException ex)

ex.printStackTrace();

EDULINE 11
Once the connection was established, we have a Connection obje
which can be used to create statements in order to execute SQL querie
In the above code, we have to close the connection explicitly after fini
working with the database:
conn.close();
INSERT Statement Example
et’s write code to insert a new record into the table Users wi
ollowing details:
username: bill
password: secretpass
fullname: Bill Gates
email: [email protected]
EDULINE 12
tring sql = "INSERT INTO Users (username, password, fullnam
mail) VALUES (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
conn
statement.setString(1, "bill");
statement.setString(2, "secretpass
secretpass");
statement.setString(3, "Bill Gates");
Gates")
statement.setString(4, "bill.gates@microsoft
[email protected]");
nt rowsInserted = statement.executeUpdate
executeUpdate();
f (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}
EDULINE 13
SELECT Statement Example

utput
User #1: bill - secretpass - Bill Gates - [email protected]
EDULINE 14
UPDATE Statement Example

EDULINE 15
DELETE Statement Example
The following code snippet will delete a record whose username
field contains “bill”

EDULINE 16

You might also like