OOPs Through Java Unit5 Notes
OOPs Through Java Unit5 Notes
AY: 2023-2024
Object Oriented Programming Through Java
UNIT - 5
Applets – Concepts of Applets, differences between applets and
applications, life cycle of an applet, types of applets, creating applets,
passing parameters to applets. Swing – Introduction, limitations of AWT,
MVC architecture, components, containers, exploring swing- JApplet,
JFrame and JComponent, Icons and Labels, text fields, buttons – The
JButton class, Check boxes, Radio buttons, Combo boxes,Tabbed Panes,
Scroll Panes, Trees, and Tables.
Difference between a Java Application and a Java Applet
Parameters Java Application Java Applet
It supports the reading and writing of It does not support the reading and
Read and Write Operation
files on the local computer. writing of files on the local computer.
A special type of Java program that runs in a Web browser is referred to as Applet. It has less response time because
it works on the client-side. It is much secured executed by the browser under any of the platforms such as Windows,
Linux and Mac OS etc. There are two types of applets that a web page can contain.
1. Local Applet
2. Remote Applet
Local Applet
Local Applet is written on our own, and then we will embed it into web pages. Local Applet is developed locally and
stored in the local system. A web page doesn't need the get the information from the internet when it finds the local
Applet in the system. It is specified or defined by the file name or pathname. There are two attributes used in defining
an applet, i.e., the codebase that specifies the path name and code that defined the name of the file that contains
Applet's code.
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
/*<applet code="FaceApplet.class" width="300" height="300">
</applet> */
public class FaceApplet extends Applet
{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome", 50, 50);
g.drawLine(20, 30, 20, 300);
g.drawRect(70, 100, 30, 30);
g.fillRect(170, 100, 30, 30);
g.drawOval(70, 200, 30, 30);
g.setColor(Color.pink);
g.fillOval(170, 200, 30, 30);
g.drawArc(90, 150, 30, 30, 30, 270);
g.fillArc(270, 150, 30, 30, 0, 180);
}
}
Remote Applet
A remote applet is designed and developed by another developer. It is located or available on a remote computer
that is connected to the internet. In order to run the applet stored in the remote computer, our system is connected
to the internet then we can download run it. In order to locate and load a remote applet, we must know the applet's
address on the web that is referred to as Uniform Recourse Locator(URL).
Difference Between Local Applet and Remote Applet
Local Applet Remote Applet
There is no need to define the Applet's URL We need to define the Applet's URL in
in Local Applet. Remote Applet.
Local Applet is available on our computer. Remote Applet is not available on our
computer.
In order to use it or access it, we don't need In order to use it or access it on our
Internet Connection. computer, we need an Internet Connection.
It is written on our own and then embedded It was written by another developer.
into the web pages.
Syntax
public String getParameter(String parameterName)
myapplet.html
import java.applet.Applet;
import java.awt.Graphics;
<html>
public class UseParam extends Applet{ <body>
<applet code="UseParam.class" width="300" height="300>
public void paint(Graphics g){ <param name="msg" value="Welcome to applet">
String str=getParameter("msg"); </applet>
g.drawString(str,50, 50); </body>
} </html>
}
limitations of AWT
MVC Architecture
The Model-View-Controller (MVC) is a well-known design pattern in the web development field. It is way to organize
our code. It specifies that a program or application shall consist of data model, presentation information and control
information. The MVC pattern needs all these components to be separated as different objects.
• Model: It represents the business layer of application. It is an object to carry the data that can also contain
the logic to update controller if data is changed.
• View: It represents the presentation layer of application. It is used to visualize the data that the model
contains.
• Controller: It works on both the model and view. It is used to manage the flow of application, i.e. data flow
in the model object and to update the view whenever data is changed.
1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.
Model Layer
The Model in the MVC design pattern acts as a data layer for the application. It represents the business logic
for application and also the state of application. The model object fetch and store the model state in the
database. Using the model layer, rules are applied to the data that represents the concepts of application.
View Layer
As the name depicts, view represents the visualization of data received from the model. The view layer
consists of output of application or user interface. It sends the requested data to the client, that is fetched
from model layer by controller.
Controller Layer
The controller layer gets the user requests from the view layer and processes them, with the necessary
validations. It acts as an interface between Model and View. The requests are then sent to model for data
processing. Once they are processed, the data is sent back to the controller and then displayed on the view.
Some of the most popular and extensively used MVC
frameworks are listed below.
• Ruby on Rails
• Django
• CherryPy
• Spring MVC
• Catalyst
• Rails
• Zend Framework
• Fuel PHP
• Laravel
• Symphony
Difference between AWT and Swing in Java
AWT Swing
Java AWT is an API to develop GUI applications in Swing is a part of Java Foundation Classes and is
Java used to create various applications.
Java AWT has comparatively less functionality as Java Swing has more functionality as compared to
compared to Swing. AWT.
The execution time of AWT is more than Swing. The execution time of Swing is less than AWT.
The components of Java AWT are platform The components of Java Swing are platform
dependent. independent.
MVC pattern is not supported by AWT. MVC pattern is supported by Swing.
AWT is a thin layer of code on top of the Swing is much larger swing also has very much
operating system. richer functionality.
import javax.swing.*;
public class TabbedPaneExample {
JFrame f;
TabbedPaneExample(){
f=new JFrame();
JTextArea ta=new JTextArea(200,200);
JPanel p1=new JPanel();
p1.add(ta);
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JTabbedPane tp=new JTabbedPane();
tp.setBounds(50,50,200,200);
tp.add("main",p1);
tp.add("visit",p2);
tp.add("help",p3);
f.add(tp);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new TabbedPaneExample();
}}
JTree
The JTree class is used to display the tree structured data or hierarchical data. JTree is a complex component. It has a
'root node' at the top most which is a parent for all nodes in the tree. It inherits JComponent class.
declaration
public class JTree extends JComponent implements Scrollable, Accessible
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample {
JFrame f;
TreeExample(){
f=new JFrame();
DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");
DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");
DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
color.add(red); color.add(blue); color.add(black); color.add(green);
JTree jt=new JTree(style);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeExample();
}}
JTable
The JTable class is used to display data in tabular form. It is composed of rows and columns.
import javax.swing.*;
public class TableExample {
JFrame f;
TableExample(){
f=new JFrame();
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args) {
new TableExample();
}
}
Applet Life Cycle in Java
In Java, an applet is a special type of program embedded in the web page to generate
dynamic content. Applet is a class in Java.
The applet life cycle can be defined as the process of how the object is created, started,
stopped, and destroyed during the entire execution of its application. It basically has five
core methods namely init(), start(), stop(), paint() and destroy().These methods are invoked
by the browser to execute.
Along with the browser, the applet also works on the client side, thus having less
processing time.
Methods of Applet Life Cycle
There are five methods of an applet life cycle, and they are:
Methods of Applet Life Cycle
init(): The init() method is the first method to run that initializes the applet. It can be invoked
only once at the time of initialization. The web browser creates the initialized objects, i.e.,
the web browser (after checking the security settings) runs the init() method within the
applet.
start(): The start() method contains the actual code of the applet and starts the applet. It is
invoked immediately after the init() method is invoked. Every time the browser is loaded or
refreshed, the start() method is invoked. It is also invoked whenever the applet is maximized,
restored, or moving from one tab to another in the browser. It is in an inactive state until the
init() method is invoked.
stop(): The stop() method stops the execution of the applet. The stop () method is invoked
whenever the applet is stopped, minimized, or moving from one tab to another in the
browser, the stop() method is invoked. When we go back to that page, the start() method is
invoked again.
destroy(): The destroy() method destroys the applet after its work is done. It is invoked when
the applet window is closed or when the tab containing the webpage is closed. It removes
the applet object from memory and is executed only once. We cannot start the applet once
it is destroyed.
paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes
like circle, square, trapezium, etc., in the applet. It is executed after the start() method and
when the browser or applet windows are resized.
JApplet class in Applet
As we prefer Swing to AWT. Now we can use JApplet that can
have all the controls of swing. The JApplet class extends the
Applet class.
Java JComponent
JComponent and other Swing components are lighter than
their AWT equivalents. The Java runtime renders lightweight
components directly, independent of the host operating
system.