OOP JAVA M5 Ktunotes - in
OOP JAVA M5 Ktunotes - in
Module 5
SWING Features
1. 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 was developed by Sun Microsystems In 1995.
It is heavy-weight in use because it is generated by the system’s host operating system.
It contains a large number of classes and methods, which are used for creating and managing GUI.
2. Swing:
Swing is a lightweight Java graphical user interface (GUI) that is used to create various applications.
Swing has components which are platform-independent.
Swing includes packages for creating desktop applications in Java.
Swing components are written in Java language. It is a part of Java Foundation Classes(JFC).
Downloaded from Ktunotes.in
Swing
Swing is a set of program
components for Java programmers
that provide the ability to create
graphical user interface ( GUI )
components, such as buttons and
scroll bars, that are independent of
the windowing system for specific
operating system .
1. Motif
2. Nimbus
3. System
4. Metal (Cross
platform)
5. ...
1. Model
2. View
3. Controller
– The View determines how the component is displayed on the screen, including any
aspects of the view that are affected by the current state of the model. (look)
– The Controller determines how the component reacts to the user. (feel)
• For example, when the user clicks a checkbox, the controller reacts by changing the
model to reflect the user’s choice (checked or unchecked). So view changes.
– Component
– Container
– JComponent.
– JFrame, JPanel
• They can only exist inside container control. They can be components or
containers.
– JTextArea,JLabel, JButton
• When container control is deleted then its child controls are also deleted.
Because containers are components, a container can also hold other containers.
All of Swing’s components are represented by classes defined within the package
javax.swing.
These are the packages used by Swing that are defined by Java SE 6.
– This package must be imported into any program that uses Swing.
– It contains the classes that implement the basic Swing components, such as
push buttons, labels, and check boxes.
2. Applet - Applets are small Java programs that are designed to be included with
the HTML web document.
They require a Java-enabled web browser for execution. At present most of the
browsers do not support applet.
The program uses a JFrame container to hold an instance of a JLabel. The label
displays a short text message
• JFrame is the top-level container that is commonly used for Swing applications.
JLabel is the Swing component that creates a label, which is a component that
displays information.
● GeeksforGeeks - www.geeksforgeeks.org
● Wikipedia - www.wikipedia.org
● Tutorialspoint - www.tutorialspoint.com
● https://itsmeebin.wordpress.com/
● https://renethajb.wordpress.com/
Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the
respective content creators. Document is compiled exclusively for study purpose and shall not be used for
commercial purpose.
Downloaded from Ktunotes.in
CS205 Object Oriented Programming in
Java
Module 5 - Graphical User Interface and
Database support of Java
(Part 3)
Prepared by
Renetha J.B.
AP
Dept.of CSE,
Lourdes Matha College of Science and Technology
Swings
Swing Packages
javax.swing.tree javax.swing.undo
1. desktop application.
2. applet
• The next line of code adds the label to the content pane of the
frame:
jfrm.add(jlab);
Container getContentPane( )
jfrm.setVisible(true);
• Swing uses the same events as does the AWT, and these events
are packaged in java.awt.event.
RUN
appletviewer MySwingApplet.java
1
Prepared by Renetha J.B.
Swings
Swing Layout Managers
– BorderLayout
– GridLayout
– CardLayout
– GridBagLayout
jfrm.setVisible(true); } 8
} }
BorderLayout( )
Insets getInsets( )
– When overriding this method, we must return a new Insets
object that contains the inset spacing you desire.
• The cards that form the deck are also typically objects of type
Panel
• Once these steps are complete, we must provide some way for
the user to select between cards.
– One common approach is to include one push button for each
card in the deck.
jfrm.add(jbtnOk);
jfrm.add(jbtnCancel);
jfrm.add(jlab);
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String s = ae.getActionCommand();
if(s.equalsIgnoreCase("ok"))
jlab.setText("OK pressed.");
else if(s.equalsIgnoreCase("cancel"))
jlab.setText("Cancel pressed.");
}
1
Prepared by Renetha J.B.
Swings
Exploring Swings
JFrame
Jlabel
JTextField
JLabel(String str)
JToggleButton
JCheckBox
JRadioButton
JButton(Icon icon)
JButton(String str)
17
Prepared by Renetha J.B.
21
Prepared by Renetha J.B.
22
Prepared by Renetha J.B.
24
Prepared by Renetha J.B.
25
Prepared by Renetha J.B.
30
Prepared by Renetha J.B.
JList(Object[ ] items)
1
Prepared by Renetha J.B.
JDBC overview
Creating and Executing Queries
create table
delete
Insert
select
– That is why, Java has defined its own API (JDBC API) that
uses JDBC drivers (written in Java language).
1. JDBC API
2. JDBC Drivers
• The JDBC API defines a set of interfaces and classes that all
major database providers follow, so that using JDBC API,
Java developers can connect to many Relational Database
Management Systems (RDBMS).
• Using the JDBC API, we can access virtually any data source
like relational databases , spreadsheets etc. from Java.
java.sql
javax.sql
Type 1: Type 2:
• JDBC-ODBC • Native-
Bridge driver API/partly Java
(Bridge) driver (Native)
Type 3: Type 4:
• All Java/Net- • All
protocol driver Java/Native-
(Middleware) protocol driver
/Thin Driver
(Pure)
Advantage
This driver is fully written in Java
and hence portable.
It is suitable for the web.
Class.forName("com.mysql.jdbc.Driver");
Or
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
E.g. Display name and age of persons with age more than 10.
SELECT name, age FROM Person WHERE age>10;
Prepared by Renetha J.B. 40
DELETE
INSERT
SELECT.
}catch(SQLException se)
{ }
catch(Exception e)
{ }
Prepared by Renetha J.B. 62
con.close();
}catch(SQLException e) { System.out.println("Error is " +e); }
}
Prepared by Renetha J.B. 64
}
Downloaded from Ktunotes.in
Summary
• Programming Language -Java
– for coding, developing interfaces(front end of an
application )
• Database – MySQL , Oracle , PostgreSQL
– For storing data- (back end of an application)
– Relational database -Tabular structure
• E.g. - Oracle, Microsoft Access, MySQL, PostgreSQL,
MongoDB etc
• SQL- Structured Query Language
– SQL statements are used to perform operations on a
database. CREATE TABLE, INSERT, DELETE, UPDATE,
SELECT