Unit 4, 5
Unit 4, 5
List Panels:-
The List Class in Java extends the Component class and also implements the ItemSelectable and
Accessible intergaves. This allows to representation of a selectable list of some options in UI form
and also the feature for accessing the options.
Syntax:
public class List extends Component implements ItemSelectable, Accessible
Scroll pane:-
A ScrollPane is a Container with built-in scrollbars that can be used to scroll its contents. In the
current implementation, a ScrollPane can hold only one Component and has no layout manager.
The component within a ScrollPane is always given its preferred size.
Syntax:-
Menubar:-
The MenuBar class provides menu bar bound to a frame and is platform specific.
Graphics:-
Graphics is an abstract class provided by Java AWT which is used to draw or paint on
the components. It consists of various fields which hold information like components to
be painted, font, color, XOR mode, etc., and methods that allow drawing various shapes
on the GUI components. Graphics is an abstract class and thus cannot be initialized
directly. Objects of its child classes can be obtained in the following two ways.
1. Inside paint() or update() method
It is passed as an argument to paint and update methods and therefore can be accessed
inside these methods. paint() and update() methods are present in the Component class
and thus can be overridden for the component to be painted.
void paint(Graphics g)
void update(Graphics g)
2. getGraphics() method
This method is present in the Component class and thus can be called on any Component
in order to get the Graphics object for the component.
public Graphics getGraphics(): Creates a graphics context for the component. This
method will return null if the component is currently not displayable.
UNIT-5:-
Concept of Applets:-
Applets:-
Applet is a Java Program that can be embedded into a Web Page. It runs inside the
Web Browser and works at Client Side. Applets are designed to make the Web Sites more Dynamic
and Entertaining. It generates dynamic content on Web Pages. Applet is a Container Class like
Frame. An Applet is a Java class that extends Java.applet.Applet Class. Applets are designed to
be Embedded within an Html Page. A JVM is required to view an Applet.
Advantages of Applet:-
Disadvantages :-
Creating Applets:-
Applets can be created by importing 2 packages named java.applet.* and
java.awt.*
Applets require aPlug in code of HTML which should be enclosed in Comment
Lines.
Example Of Applet:-
import java.applet.*;
import java.awt.*;
</applet>*/
{
public void init()
setBackground(Color.blue);
setForeground(Color.green);
g.drawString("Welcome to Applet",100,160);
1) Initialization
2) Start
3) Paint
4) Stop
5) Destroy
1) Initialization:-
This Stage is used to Initialize the Applet. It is Executed only Once. The Code for
Initialization of Applet is Written by Using Public Void init () method.
By Using Initialization in the code of Applet ther is no need of Using main Method in Java Program
for Applets.
2) Start:-
This Stage is used to Start the Applet. It is Executed after the init () method after the browser
is Maximised. This method automatically invokes when the Applet browser is Maximised.
The method used for this stage is: Public Void Start().
3) Paint:-
This Stage is used to Paint the Applet. It displays the Content on Applet Browser. Paint
method requires Graphics class as a Parameter. Graphics class Objects can be Used for drawing
Oval ,Rectangle ,Arc , String etc........
The Method used for this Stage is: Public Void Paint (Graphics g).
4) Stop:-
This Stage is Used to Stop the Applet. It is Executed when Applet Browser is Stopped. This
Stage invokes automatically when the Applet Browser is minimized.
The method used for this Stage is : Public Void Stop ().
5) Destroy:-
This Stage is Used to Destroy the Applet Window. It is Executed only Once. It invokes
automatically when the Applet Browser is Closed.
The method used for this Stage is: Public Void destroy().
Life Cycle Of Applet
init ()
start ()
paint ()
stop()
destroy()
To work with init() ,start() ,stop() and destroy() methods Java.applet.*; Package should be imported.
To Work with paint() method Java.awt.*; Package should be imported.
Types of Applets:-
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.
Syntax:-
<applet
codebase = "tictactoe"
code = "FaceApplet.class"
width = 120
height = 120>
</applet>
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).
Syntax:-
<applet
codebase = "http://www.myconnect.com/applets/"
code = "FaceApplet.class"
width = 120
height =120>
</applet>
The getParameter() method of the Applet class can be used to retrieve the parameters
passed from the HTML page. The syntax of getParameter() method is as follows:
String getParameter(String param-name)
Example Program:-
import java.applet.*;
import java.awt.*;
</applet>*/
setBackground(Color.blue);
setForeground(Color.green);
g.drawString("Welcome to Applet",100,160);
g.String getParameter(“msg”);
Java Applet
The Java applet works on the client side, and runs on the web browser. It is a Java
application that the user can easily embed on a web page.
Differnces:-
Java Application Java Applet
A Java Application also known as The Java applet works on the client side,
application program is a type of and runs on the browser and makes use
program that independently executes of another application program so that
on the computer. we can execute it.
Its execution starts with the main( ) It does not require the use of any main()
method only. The use of the main( ) is method. Java applet initializes through
mandatory. init( ) method.
It cannot run independently, but It cannot start independently but requires
requires JRE to run. APIs for use (Example. APIs like Web API).
We need to install the Java Java applet does not need to be pre-
application first and obviously on the installed.
local computer.
It is possible to establish connections It cannot establish connection to other
with other servers. servers.
It performs read and write tasks on a It cannot run the applications on any
variety of files located on a local local computer.
computer.
It can easily access a file or data It cannot access the file or data found on
available on a computer system or any local system or computer.
device.
Java applications are pretty trusted, Java applets are less reliable. So, they
and thus, come with no security need to be safe.
concerns.
Introduction to Swing:-
Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract
Window Toolkit [AWT]. Java Swing offers much-improved functionality over AWT,
new components, expanded components features, and excellent event handling with
drag-and-drop support.
Swing has about four times the number of User Interface [UI] components as AWT and
is part of the standard Java distribution.
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.
The model designs based on the MVC architecture follow MVC design pattern. The
application logic is separated from the user interface while designing the software using
model designs.
In Java Programming, the Model contains the simple Java classes, the View used to
display the data and the Controller contains the servlets. Due to this separation the user
requests are processed as follows:
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.
o MVC has the feature of scalability that in turn helps the growth of application.
o The components are easy to maintain because there is less dependency.
o A model can be reused by multiple views that provides reusability of code.
o The developers can work with the three layers (Model, View, and Controller)
simultaneously.
o Using MVC, the application becomes more understandable.
o Using MVC, each layer is maintained separately therefore we do not require to deal
with massive code.
o The extending and testing of application is easier.
Exploring Swing:-
The hierarchy of java swing API is given below.
Method Description
public void add(Component c) add a component on another component.
public void setSize(int width,int sets size of the component.
height)
public void setLayout(LayoutManager sets the layout manager for the component.
m)
public void setVisible(boolean b) sets the visibility of the component. It is by
default false.
import javax.swing.*;
class FirstSwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame();//creating instance of JFrame
JApplet is a java swing public class designed for developers usually written in Java.
JApplet is generally in the form of Java bytecode that runs with the help of a Java virtual
machine (JVM) or Applet viewer from Sun Microsystems. It was first introduced in 1995.
JApplet extends the class in the form of java.applet.Applet. JApplets are executed in a
tightly-controlled set of resources referred to as sandboxes. This prevents the JApplets
from accessing local data like the clipboard or file system.
JFrame:-
The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame
class. JFrame works like the main window where components like labels, buttons,
textfields are added to create a GUI.
Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.
Thе Java JFramе is an еssеntial componеnt of Java Swing, which is a part of thе Java
SWT(Standard Widgеt Toolkit). JFrame in Java is a class that allows you to crеatе and
manage a top-lеvеl window in a Java application. It sеrvеs as thе main window for GUI-
basеd Java applications and providеs a platform-indеpеndеnt way to crеatе graphical
usеr intеrfacеs. In Java JFrame is a part of javax.swing package.
JComponent:-
The JComponent class is the base class of all Swing components except top-level
containers. Swing components whose names begin with "J" are descendants of the
JComponent class. For example, JButton, JScrollPane, JPanel, JTable etc. But, JFrame
and JDialog don't inherit JComponent class because they are the child of top-level
containers.
The JComponent class extends the Container class which itself extends Component. The
Container class has support for adding components to the container.
All Swing components, with the exception of top-level containers, are included in this
core class. With the exception of JFrame and JDialog, which are top-level containers
by definition and cannot be put under JComponent, all swing components with the
letter “J” in their names are classified as JComponent.
JComponent offers event handling, helping developers respond to user events like
mouse clicks, key presses, and other inputs.
Icons of Swing:-
Many Swing components, such as labels, buttons, and tabbed panes, can be decorated with
an icon — a fixed-sized picture. An icon is an object that adheres to the Icon interface.
Swing provides a particularly useful implementation of the Icon interface: ImageIcon,
which paints an icon from a GIF, JPEG, or PNG image.
The Icon interface is very simple, specifying just three methods used to determine the size
of the Icon and to display it. Implementors of this interface are free to store and display
the image in any way, providing a great deal of flexibility. In other words, icons don’t
have to be bitmaps or GIF images.
Labels:-
Swing allows you to create labels that can contain text, images, or both.
JLabel objects may consist of both text and graphics (icons), but for simple text-only
labels, the interface with JLabel is very similar to that of java.awt.Label.
Syntax:-
Text Fields:-
JButton class:-
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It
inherits AbstractButton class.
Check Boxes:-
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or
off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to
"on ".It inherits JToggleButton class.
The object of Choice class is used to show popup menu of choices. Choice selected by
user is shown on the top of a menu. It inherits JComponent class.
Tabbed Panes:-
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.
Constructors
Constructor Purpose
JScrollPane() It creates a scroll pane. The Component parameter, when
JScrollPane(Component) present, sets the scroll pane's client. The two int
JScrollPane(int, int) parameters, when present, set the vertical and horizontal
JScrollPane(Component, scroll bar policies (respectively).
int, int)
Trees:-
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.
Tables:-
The JTable class is used to display data in tabular form. It is composed of rows and
columns.