0% found this document useful (0 votes)
7 views

OOPs Through Java Unit5 Notes

Uploaded by

vejelol840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OOPs Through Java Unit5 Notes

Uploaded by

vejelol840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

OOPS THROUGH JAVA

SUBJECT CODE: 22PC0AM12

II B. tech -II Semester

DEPARTMENT OF SPECIAL BATCH

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

Applets are small Java programs that are


Applications are just like a Java program
designed to be included with the HTML
Definition that can be executed independently
web document. They require a Java-
without using the web browser.
enabled web browser for execution.

The applet does not require the main()


The application program requires a
main () method method for its execution instead init()
main() method for its execution.
method is required.

Applet programs are compiled with the


The “javac” command is used to compile
“javac” command and run using either
Compilation application programs, which are then
the “appletviewer” command or the
executed using the “java” command.
web browser.

Java application programs have full


Applets don’t have local disk and
File access access to the local file system and
network access.
network.
Applets can only access browser-
Applications can access all kinds of
Access level specific services. They don’t have
resources available on the system.
access to the local system.

First and foremost, the installation of a


The Java applet does not need to
Installation Java application on the local computer is
be installed beforehand.
required.

Applications can execute the programs Applets cannot execute programs


Execution
from the local system. from the local machine.

An application program is needed to An applet program is needed to


Program
perform some tasks directly for the user. perform small tasks or part of them.
It cannot start on its own, but it can
It cannot run on its own; it needs JRE
Run be executed using a Java-enabled web
to execute.
browser.

Connectivity with other servers is It is unable to connect to other


Connection with servers
possible. servers.

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.

Executed in a more restricted


Application can access the system’s
environment with tighter security.
Security data and resources without any
They can only use services that are
security limitations.
exclusive to their browser.

Java applications are self-contained Applet programs cannot run on their


Restrictions and require no additional security own, necessitating the maximum level
because they are trusted. of security.
Types of Applets in Java

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.

We don't need to download it. It is available on a remote computer, so we


need to download it to our system.
Parameter in Applet
We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named
getParameter()

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

• The buttons of AWT does not support pictures.


• It is heavyweight in nature.
• Two very important components trees and tables are not present.
• Extensibility is not possible as it is platform dependent

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.

Advantages of MVC Architecture

The advantages of MVC architecture are as follows:


• MVC has the feature of scalability that in turn helps the growth of application.
• The components are easy to maintain because there is less dependency.
• A model can be reused by multiple views that provides reusability of code.
• The developers can work with the three layers (Model, View, and Controller) simultaneously.
• Using MVC, the application becomes more understandable.
• Using MVC, each layer is maintained separately therefore we do not require to deal with massive code.
• The extending and testing of application is easier.
MVC Architecture Layers

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.

The components of Java AWT are heavy


The components of Java Swing are light weighted.
weighted.

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 provides comparatively less powerful


Swing provides more powerful components.
components.

Swing components requires javax.swing


AWT components require java.awt package
package

AWT is a thin layer of code on top of the Swing is much larger swing also has very much
operating system. richer functionality.

Swing is also called as JFC(java Foundation


AWT stands for Abstract windows toolkit .
classes). It is part of oracle’s JFC.
Java JRadioButton Example
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
Java JCheckBox
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
JTabbedPane
The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or
icon. It inherits JComponent class.

JTabbedPane class declaration

public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants

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.

You might also like