0% found this document useful (0 votes)
32 views149 pages

Unit 1 Advanced Java Programming TE SPPU

The document outlines the curriculum for an Advanced JAVA Programming course at Savitribai Phule Pune University, detailing textbooks, reference materials, and additional resources. It covers the fundamentals of applets, including their lifecycle, differences from standalone applications, and types of applets, along with example code. The document serves as a comprehensive guide for students to understand applet programming in Java.

Uploaded by

Makaran Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views149 pages

Unit 1 Advanced Java Programming TE SPPU

The document outlines the curriculum for an Advanced JAVA Programming course at Savitribai Phule Pune University, detailing textbooks, reference materials, and additional resources. It covers the fundamentals of applets, including their lifecycle, differences from standalone applications, and types of applets, along with example code. The document serves as a comprehensive guide for students to understand applet programming in Java.

Uploaded by

Makaran Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 149

Savitribai Phule Pune University

Third Year of E & TC Engineering (2019 Course)

304195 (C): Advanced JAVA Programming


(Elective - II)
Subject Teacher
Mrs. A.A.Yadav
TEXT BOOKS
T1. Herbert Schildt, “Java: The complete reference”, Tata McGraw Hill, 7th Edition
T2. Jim Keogh, “Complete Reference J2EE” , Enterpr
T3. E. Balaguruswamy, “Programming with JAVA: A Primer” McGraw Hill Education,
India, 5th Edition

REFERENCE BOOKS
R1. “Java 6 Programming”, Black Book, Dreamtech
R2. “Java Server Programming, Java EE6 (J2EE 1.6)”, Black Book, Dreamtech
R3. M.T. Savaliya,“Advanced Java Technology”, Dreamtech
ADDITIONAL MATERIAL
MOOC / NPTEL:
1. NPTEL Course “Programming in Java”
https://nptel.ac.in/courses/106/105/106105191/
2. Udemy course “Advanced Java Programming”
https://www.udemy.com/course/advanced-java-programming
Unit 1

Reference / text
Sr. No. Topic
book with
page
no./web
refrence
UNIT 1: Applet

1  Applet Basics – Introduction, limitations of AWT.


T1
 Applet architecture – HTML APPLET tag – Passing parameter to
Applet. Chapter 21
 getDocumentBase() and getCodeBase()

2  Japplet: Icons and Labels Text Fields Buttons, Combo Boxes ,


Checkboxes T1
 Tabbed Panes, Scroll Panes, Trees,Tables Chapter 29-30
Applets
 An applet is a Java program that runs in a
Web browser.
 An applet can be a fully functional Java

application because it has the entire Java


API at its disposal.

10/12/2025 1 13
3
Applets
 Applets based on the AWT(Abstract
Window Toolkit) package by extending
its Applet class.
 Applets based on the Swing package by

extending its JApplet class

10/12/2025 14
Applet Life Cycle in Java

10/12/2025 15
Applet Life Cycle in Java

 init():
The init() method is the first
method to run that initializes the applet.
 Itcan be invoked only once at the time
of initialization.
 The web browser creates the initialized
objects, i.e., the web browser runs the
init() method within the applet.

10/12/2025 16
Applet Life Cycle in Java

 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.
10/12/2025 17
Applet Life Cycle in Java
 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.

10/12/2025 18
Applet Life Cycle in Java
 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.

10/12/2025 19
Applet Life Cycle in Java
 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.

10/12/2025 20
Applet
 Java programs can be
◦ Standalone apps (command line or GUI)
◦ Web browser applets (run inside web browser)
 Applications have main() method, applets
don’t
 Applets can’t make changes to machine on

which they are running


◦ E.g. writing to disk
◦ Hence only small interactive programs
 To make web pages more dynamic and
interactive
 Java applets are used to provide interactive
features to web applications and can be
executed by browsers for many platforms.
 They are small, portable Java programs

embedded in HTML pages and can run


automatically when the pages are viewed.
 It runs inside the browser and works at

client side.

Dept. of E&TC, SITS


Application
They are similar to Java programs.
They can be executed independently without using web browser.
It requires a ’main’ function for it to be executed.
Java applications have full access to local file system and network.
They can access all kinds of resources that are available to the system.
They can execute the programs with the help of the local system.
An application program is required when a task needs to be directly
performed for the user.
Applets
They are small Java programs.
They have been designed to be included with HTML documents.
They need Java enabled web browser to be executed.
It doesn’t need a main function to get executed.
It doesn’t have local disk and network access.
It can access the browser specific services only.
They can’t access the local system.
They can’t execute programs from local machines.
It is required to perform small tasks or it can be used as a part of a task.

Dept. of E&TC, SITS


 Advantage of Applet
 There are many advantages of applet. They are
as follows:
 It works at client side so less response time.
 Secured
 It can be executed by browsers running under
many plateforms, including Linux, Windows,
Mac Os etc.
 Drawback of Applet
 Plugin is required at client browser to execute
applet.

Dept. of E&TC, SITS


Hierarchy

Dept. of E&TC, SITS


Dept. of E&TC, SITS
 Lifecycle methods for Applet:
 The java.applet.Applet class 4 life cycle

methods and java.awt.Component class


provides 1 life cycle methods for an applet.
 java.applet.Applet class
 For creating any applet java.applet.Applet

class must be inherited. It provides 4 life


cycle methods of applet.

Dept. of E&TC, SITS


 public void init(): is used to initialized the
Applet. It is invoked only once.
 public void start(): is invoked after the

init() method or browser is maximized. It is


used to start the Applet.
 public void stop(): is used to stop the

Applet. It is invoked when Applet is stop or


browser is minimized.
 public void destroy(): is used to destroy

the Applet. It is invoked only once.

Dept. of E&TC, SITS


 java.awt.Component class
 The Component class provides 1 life cycle

method of applet.
 public void paint(Graphics g): is used to

paint the Applet. It provides Graphics class


object that can be used for drawing oval,
rectangle, arc etc.

Dept. of E&TC, SITS


Q.Who is responsible to manage the life cycle
of an applet?
 Java Plug-in software.

Q.How to run an Applet?


 There are two ways to run an applet
 By html file.
 By appletViewer tool (for testing purpose).

Dept. of E&TC, SITS


 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.
 Local Applet
 Remote Applet

Dept. of E&TC, SITS


10/12/2025 32
 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.

Dept. of E&TC, SITS


 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).

Dept. of E&TC, SITS


Local Applet Remote Applet
There is no need to define the We need to define the Applet's
Applet's URL in Local Applet. URL in Remote Applet.
Local Applet is available on our Remote Applet is not available
computer. on our computer.
In order to use it or access it, In order to use it or access it
we don't need Internet on our computer, we need an
Connection. Internet Connection.
It is written on our own and It was written by another
then embedded into the web developer.
pages.
We don't need to download it. It is available on a remote
computer, so we need to
download it to our system.

Dept. of E&TC, SITS


// To create simple
//AppletCode.java

import java.applet.*;
Import java.awt.*;

public void AppletCode extends Applet


( AppletCode class is inherited by Applet){

Pubilc void paint(Graphic g){


g.drawString(“ E&TC Department”,100,100)
( 100,,100 coordinates from which displaying starts)
}
}

Output:
E&TC Department Dept. of E&TC, SITS
 Simple example of Applet by html file:
 To execute the applet by html file, create an

applet and compile it. After that create an


html file and place the applet code in html
file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}

Dept. of E&TC, SITS


 myapplet.html

<html>
<body>
<applet code=“Appletcode.class" width="300" height="300">
</applet>
</body>
</html>

Dept. of E&TC, SITS


Hello World Applet
 //Filename: HelloWorldApplet.java
 import java.applet.Applet;
 import java.awt.Graphics;
 public class HelloWorldApplet extends

Applet {
◦ public void paint(Graphics g) {
 g.drawString("Hello World!", 50, 25);
◦}
 }
Running an Applet
 Browser
 AppletViewer
Applets
 class of applet package.
 Must Import java.applet;
 Applets must also import java.awt.
 AWT stands for the Abstract Window

Toolkit, Since all applets run in a


window(Applet Viewer)
Applet Methods
 init()
◦ The init( ) method is the first method to be called. This is
where you should initialize variables. This method is called
only once during the run time of your
 start()
◦ The start( ) method is called after init( ). It is also called to
restart an applet after it has been stopped
 stop()
 destroyed()
 paint()
◦ The paint( ) method is called each time your applet's output
must be redrawn
 Text Books:
 1. E Balagurusamy, “Programming with JAVA”, Tata McGraw Hill, 6th Edition.
 2. Herbert Schildt, “Java: The complete reference”, Tata McGraw Hill, 7th Edition.

Department of Electronics and


Telecommunication October 12, 2025 43
FAQs
 Ds
 What is an Applet ? Write syntax with suitable example.

 Write differences between applets and applications .

 Explain life cycle of an Applet.

 Explain different types of Applet.

 Creating a simple applet and explain its output.

 References:

 E Balagurusamy, “Programming with JAVA”, Tata

McGraw Hill, 6th Edition.


 2. Herbert Schildt, “Java: The complete reference”, Tata

McGraw Hill, 7th Edition.


https://www.javatpoint.com/object-and-class-in-java

Dept. of E&TC, SITS


Applet architecture
 Java provides console based programming language environment and
window based programming environment.
 An applet is a window based programming environment. So applet
architecture is different than console base program.
 Java applets are essentially java window programs that can run within a
web page.
 Applete programs are java classes that extend that java.applet.applet class
and are enabaled by reference with HTML page. You can observed that
when applet are combined with HTML, that can make an interface more
dynamic and powerful than with HTML alone.
While some Applet do not more than scroll text or play movements, but by
incorporating theses basic features in webpage you can make them
dynamic. These dynamic web page can be used in an enterprise
application to view or manipulate data comming from some source on the
server.
The Applet and there class files are distribute through standard HTTP
request and therefore can be sent across firewall with the web page data.
Applet code is referenced automatically each time the user revisit the
hosting website. Therefore keeps full application up to date on each client
desktop on which it is running.
Applet architecture
 When you write a Java application for time
series data, you use the JDBC Driver to
connect to the database,
 The Java application makes calls to the JDBC

driver, which sends queries and other SQL


statements to the database. The database
sends query results to the JDBC driver,
which sends them on to the Java
application.
 You can also use the time series Java

classes in Java applets and servlets


 The database server is connected to the
JDBC driver, which is connected to the
applet. The applet is also connected to a
browser, which is connected to a web server
that communicates with the database.
 A request from an application goes through
a web server, an HTTP servlet subclass, and
the JDBC driver to the database. The
database sends responses back along the
same path.
Applet Architecture
 Applet is window based program (GUI)
 Applets are event driven architecture.
 GUI: Graphical User Interface
 Event Driven : e.g mouse click
import java.awt.*;
import java.applet.*;
Example public class Applet1 extends
Applet
Applet Skeleton: {
public void init()
{System.out.println("Initializing an
applet");}
public void start()
{System.out.println("Starting an
applet");}
public void stop()
{System.out.println("Stopping an
applet");}
public void destroy()
{System.out.println("Destroying an
applet");}
10/12/2025 50
package EXAMPLE;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class PRACAPP extends


Applet{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString("Hello TE IN
APPLETS",50, 50);
g.setColor(Color.blue);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
g.drawLine(21,31,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
}
}
10/12/2025 51
HTML applet Tag

The <applet> tag in HTML was used to embed Java applets into any
HTML document. The <applet> tag was deprecated in HTML 4.01, and
it’s support has been completely discontinued starting from HTML 5.
Alternatives available in HTML 5 are the <embed> and the <object>
tags. There are still some browsers that support the <applet> tag with
the help of some additional plug-ins/installations to work. Internet
Explorer 11 and earlier versions with the help of plug-ins.
Applet Tag is not supported in HTML5. The <applet> tag takes a number
of attributes, with one of the most important being the code attribute.
This code attribute is used to link a Java applet to the concerned HTML
document. It specifies the file name of the Java applet.
 Attributes: This tag accepts the following attributes:
 align: Specifies the alignment of an applet.
 alt: Specifies an alternate text for an applet.
 archive: Specifies the location of an archive file.
 border: Specifies the border around the applet panel.
 codebase: Specifies a relative base URL for applets specified in
the code attribute.
 height: Specifies the height of an applet.
 hspace: Defines the horizontal spacing around an applet.
 mayscript: Indicates whether the Java applet is allowed to
access the scripting objects of the web page.
 name: Defines the name for an applet (to use in scripts)
 vspace: Defines the vertical spacing around an applet.
 width: Specifies the width of an applet.
Syntax:
<applet attribute1 attribute2....>
<param parameter1>
<param parameter2> ....
</applet>
 Example 1: Here, HelloWorld is the class
file, which contains the applet.
The width and height attributes determine
the width and height of the applet in pixels
when it is opened in the browser.
Attributes available to be used in
conjunction with the <applet> tag are as
<!DOCTYPE html>
follows: <html>
<!-- applet code starts here -->
<applet code="HelloWorld">
<!-- applet code ends here -->
</applet>
</html>
 Parameters: Parameters are quite similar
to command-line arguments in the sense
that they provide a way to pass information
to the applet after it has started. All the
information available to the applet before it
starts is said to be hard-coded i.e.
embedded within it. Parameters make it
possible to generate and use data during
run-time of the applet.
<!DOCTYPE html>
<html>
<!-- applet code starts here -->
<applet code="HelloWorld">
<param name="message"
value="HelloWorld">
<!-- applet code ends here -->
</applet>

</html>
HTML Code
 //Filename: HelloWorldApplet.html
 <title>Hello World Applet</title>
 </head>
 <body>
 <applet code="HelloWorldApplet.class"

align="baseline“ width="200"
height="80"</applet>
 </body>
 </html>
getCodeBase() -
getDocumentBase()
 The getCodebase() method is also commonly
used to establish a path to other files or
folders that are in the same location as the
class being run.
 The getDocumentBase() method is used to
return the URL of the directory in which the
document is resides.
 URL getCodeBase(): Gets the base URL.
 URL getDocumentBase(): Gets the URL of
the document in which the applet is
embedded
10/12/2025 58
Example
import java.applet.Applet;
import java.awt.Graphics;
public class MyAppletClass extends Applet
{
public void paint(Graphics g) {
g.drawString("getCodeBase :
"+getCodeBase(), 20, 20);

g.drawString("getDocumentBase:"+getDocume
ntBase(),20,40);
}
}
10/12/2025 59
Example getCodeBase() -
getDocumentBase()

10/12/2025 60
 Showdocument():
 Defined by applet context interace to allow

your applet to transfer control to another


url.
How to get the object of
Image
 The java.applet.Applet class provides
getImage() method that returns the object
of Image.
 Syntax:
 public Image getImage(URL u, String

image){}
 public abstract boolean

drawImage(Image img, int x, int y,


ImageObserver observer): is used draw
the specified image.

10/12/2025 62
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class PRACAPP extends Applet{
Image img1;
public void init()
{
img1=getImage(getDocumentBase(),“Mi
ckey.jpg");}
public void paint(Graphics g)

{g.drawImage(img1,100,30,this);}
}

10/12/2025 63
Explanation
 In the above example, drawImage() method
of Graphics class is used to display the
image.
 The 4th argument of drawImage() method of

is ImageObserver object.
 The Component class implements
ImageObserver interface.
 So current class object would also be treated

as ImageObserver because Applet class


indirectly extends the Component class.

10/12/2025 64
AWT - 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 Microsystem 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.

10/12/2025 65
Characteristics
 It is a set of native user interface
components.
 It is very robust in nature.
 It includes various editing tools like
graphics tool and imaging tools.
 It uses native window-system controls.
 It provides functionality to include shapes,

colors and font classes.

10/12/2025 66
Disadvantages 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

10/12/2025 67
SWING

 Swing is a set of classes this provides more powerful


and flexible components than are possible with the
AWT.
 A Swing GUI consists of two key items: components
and containers.
 In addition to the familiar components, such as
buttons, check boxes, and labels, Swing supplies
several exciting additions, including tabbed panes,
scroll panes, trees, and tables.
 Even familiar components such as buttons have more
capabilities in Swing.
 For example, a button may have both an image and
a text string associated with it.
10/12/2025 68
SWING

 Also, the image can be changed as the state of the


button changes.
 Unlike AWT components, Swing components are
not implemented by platform-specific code.
 Instead, they are written entirely in Java and,
therefore, are platform-independent.
 The term lightweight is used to describe such
elements.
 Swing is the set of packages built on top of the
AWT that provide us with a great number of pre-
built classes that is, over 250 classes and 40 UI
components.
10/12/2025 69
Swings
 Swing defines two types of containers.
 The first are top-level containers: JFrame,
JApplet, JWindow, and JDialog.
 These containers do not inherit JComponent.

 They do, however, inherit the AWT classes

Component and Container.


 The second type of containers supported by

Swing are lightweight containers.


 Lightweight containers do inherit JComponent.

 An example of a lightweight container is JPanel,

which is a general-purpose container

10/12/2025 70
Components

10/12/2025 71
Swings
 JTree Encapsulates a tree-based control.
 The Swing-related classes are contained in

javax.swing and its subpackages.

10/12/2025 72
Swing Features
 Besides the large array of components in Swing
and the fact that they are lightweight, Swing
introduces many other innovations.
 Borders

We can draw borders in many different styles


around components using the setborder( )
method.
 Graphics Debugging

We can use setDebugging GraphicsOptions


method to set up graphics debugging which
means among the other things, that you can
watch each line as its drawn and make it flash.

10/12/2025 73
Swing Features
 Easy mouseless operation
It is easy to connect keystrokes to
components.
 Tooltips

We can use the setToolTipText method of


JComponent to give components a
tooltip, one of those small windows that
appear when the mouse hovers over a
component and gives explanatory text.

10/12/2025 74
Swing Features

 Easy Scrolling
We can connect scrolling to various
components-something that was impossible
in AWT.
 Pluggable look and feel

We can set the appearance of applets and


applications to one of three standard looks.
 Windows, Motif (Unix) or Metal (Standard
swing look).
 New Layout Managers

Swing introduces the BoxLayout and


OverlayLayout layout managers. 10/12/2025 75
Class Description
 AbstractButton -Abstract super-class for
Swing buttons.
 ButtonGroup - Encapsulates a mutually

exclusive set of buttons.


 ImageIcon - Encapsulates an icon.
 JApplet -The Swing version of Applet.
 JButton- The Swing push button class.
 JCheckBox -The Swing check box class.

10/12/2025 76
Class Description

 JComboBox -Encapsulates a combo box (a


combination of a dropdown list and text field).
 JLabel -The Swing version of a label.
 JRadioButton -The Swing version of a radio
button.
 JScrollPane- Encapsulates a scrollable window.
 JTabbedPane -Encapsulates a tabbed window.
 JTable -Encapsulates a table-based control.
 JTextField -The Swing version of a text field.
 Jtree- Encapsulates a tree-based control.

10/12/2025 77
JApplet

 Fundamental to Swing is the JApplet class,


which extends Applet.
 Applets that use Swing must be subclasses of

JApplet.
 Swing applets use the same four lifecycle : init(

), start( ), stop( ), and destroy( ).


 JApplet is a top-level Swing container, which

means that it is not derived from Jcomponent.


 JApplet is derived from Applet.

 JApplet is rich with functionality that is not

found in Applet.

10/12/2025 78
JApplet
 For example, JApplet supports various
“panes,” such as the content pane, the glass
pane, and the root pane.
 When adding a component to an instance of

JApplet, do not invoke the add( ) method of


the applet.
 Instead, call add( ) for the content pane of the

JApplet object.
 The content pane can be obtained via the

method shown here:


 Container getContentPane( )

10/12/2025 79
JApplet
 The add( ) method of Container can be
used to add a component to a content
pane.
 Its form is shown here:
 void add(comp)
 Here, comp is the component to be added

to the content pane.

10/12/2025 80
Icons and Labels
 InSwing, icons are encapsulated by the
ImageIcon class, which paints an icon from
an image.
 Two of its constructors are shown here:

ImageIcon(String filename)
ImageIcon(URL url)
The first form uses the image in the file
named filename.
The second form uses the image in the
resource identified by url.

10/12/2025 81
Icons and Labels

 The ImageIcon class implements the Icon interface


that declares the methods shown here:
 int getIconHeight( )

 Returns the height of the icon in pixels.


 int getIconWidth( )

 Returns the width of the icon in pixels.


 void paintIcon(Component comp, Graphics g, int x,

int y)
 Paints the icon at position x,y on the graphics
context g. Additional information about the paint
operation can be provided in comp.

10/12/2025 82
Icons and Labels
 Swing labels are instances of the JLabel
class, which extends JComponent. It can
display text and/or an icon.
 It is a passive component in that it does not

respond to user input.


 Some of its constructors are shown here:

 JLabel(Icon i)

 Label(String s)

 JLabel(String s, Icon i, int align)

 Here, s and i are the text and icon used for

the label.
 The align argument is either LEFT,
RIGHT,CENTER, LEADING, or TRAILING 10/12/2025 83
Icons and Labels
 The icon and text associated with the label
can be read and written by the following
methods:
 Icon getIcon( )
 String getText( )
 void setIcon(Icon i)
 void setText(String s)
 Here, i and s are the icon and text,

respectively

10/12/2025 84
Example
import java.awt.*;
import javax.swing.*;
public class JLabelDemo extends JApplet
{
public void init()
{
Container contentPane =
getContentPane();
ImageIcon ii = new ImageIcon("IC.jpg");
JLabel jl = new JLabel("IC", ii,
JLabel.CENTER);
contentPane.add(jl);
}
}

10/12/2025 85
getContentPane()
 A container has several layers in it.
 In Java Swing, the layer that is used to hold
objects is called the content pane.
 Objects are added to the content pane layer
of the container.
 The getContentPane() method retrieves
the content pane layer so that you can add
an object to it.
 When you use getContentPane(),
the content pane object then is substituted
there so that you can apply a method to it.

10/12/2025 86
Text Fields
 The Swing text field is encapsulated by the
JTextComponent class, which extends JComponent.
 It provides functionality that is common to Swing text
components.
 One of its subclasses is JTextField, which allows us to
edit one line of text.
 Some of its constructors are shown here:
 JTextField( )
 JTextField(int cols)
 JTextField(String s, int cols)
 JTextField(String s)
 Here, s is the string to be presented, and cols is the
number of columns in the text field.
10/12/2025 87
Example
public class JTextFieldDemo extends JApplet
{
JTextField jtf;
public void init()
{
Container contentPane =
getContentPane();
contentPane.setLayout(new
FlowLayout());
jtf = new JTextField(15);
contentPane.add(jtf);
}
}
10/12/2025 88
Java FlowLayout
 The Java FlowLayout class is used to arrange the
components in a line, one after another (in a
flow).
 It is the default layout of the applet or panel.

 Fields of FlowLayout class

 public static final int LEFT

 public static final int RIGHT

 public static final int CENTER

 public static final int LEADING

 public static final int TRAILING

10/12/2025 89
JButtons
 Swing buttons provide features that are not found in the
Button class defined by the AWT.
 For example, we can associate an icon with a Swing
button.
 Swing buttons are subclasses of the AbstractButton class,
which extends JComponent.
 AbstractButton contains many methods that allow us to
control the behavior of buttons, check box and radio
buttons.
 For example, we can define different icons that are
displayed for the component when it is disabled, pressed,
or selected.
 Another icon can be used as rollover icon, which is
displayed when the mouse is positioned over that
component. 10/12/2025 90
JButtons
 The following are the methods that control this
behavior:
 void setDisabledIcon(Icon di)

 void setPressedIcon(Icon pi)

 void setSelectedIcon(Icon si)

 Here, di, pi, si, and ri are the icons to be used for
these different
 conditions.

10/12/2025 91
JButtons
 The text associated with a button can be
read and written via the following
methods:
 String getText( )
 void setText(String s)
 Here, s is the text to be associated with

the button.

10/12/2025 92
JButtons
 Concrete subclasses of AbstractButton
generate action events when they are
pressed.
 Listeners register and un-register for these

events via the methods shown here:


 void addActionListener(ActionListener al)
 void removeActionListener(ActionListener al)
 Here, al is the action listener. AbstractButton

is a superclass for push buttons, check


boxes, and radio buttons.

10/12/2025 93
JButton Class

 The JButton class provides the


functionality of a push button.
 Jbutton allows an icon string, or both to
be associated with the push button.
 Some of its constructors are shown here:
 JButton(Icon i)
 JButton(String s)
 JButton(String s, Icon i)
 Here, s and i are the string and icon used
for the button.
10/12/2025 94
JButton
package example;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;

10/12/2025 95
JButton
public class JLabelDemo extends JApplet implements
ActionListener
{
JTextField jtf;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
ImageIcon france = new ImageIcon("green.jpg");
JButton jb = new JButton(france);
jb.setActionCommand("Green");
jb.addActionListener(this);
contentPane.add(jb);
10/12/2025 96
JButton

ImageIcon germany = new


ImageIcon("red.jpg");
jb = new JButton(germany);
jb.setActionCommand("Red");
jb.addActionListener(this);
contentPane.add(jb);
ImageIcon italy = new
ImageIcon("yellow.jpg");
jb = new JButton(italy);
jb.setActionCommand("Yellow");
jb.addActionListener(this);
contentPane.add(jb);
10/12/2025 97
JButton

ImageIcon japan = new ImageIcon("black.jpg");


jb = new JButton(japan);
jb.setActionCommand("Black");
jb.addActionListener(this);
contentPane.add(jb);
jtf = new JTextField(15);
contentPane.add(jtf);
}
public void actionPerformed(ActionEvent ae)
{
jtf.setText(ae.getActionCommand());
}} 10/12/2025 98
checkboxes
 The JCheckBox class, which provides the
functionality of a check box, is a concrete
implementation of AbstractButton.
 It is immediate super-class is
JToggleButton, which provides
support for two-state buttons

10/12/2025 99
JCheckBox constructor
 JCheckBox(Icon i)
 JCheckBox(Icon i, boolean state)

 JCheckBox(String s)

 JCheckBox(String s, boolean state)

 JCheckBox(String s, Icon i)

 JCheckBox(String s, Icon i, boolean state)

 Here, i is the icon for the button. The text is

specified by s.
 If state is true, the check box is initially selected.

Otherwise, it is not

10/12/2025 10
0
Example
public class JCheckBoxDemo
extends JApplet implements cb.addItemListener(this);
ItemListener contentPane.add(cb);
{ jtf = new JTextField(15);
JTextField jtf; contentPane.add(jtf);
public void init() }
{ public void
Container contentPane = itemStateChanged(ItemEven
getContentPane(); t ie)
contentPane.setLayout(new {
FlowLayout()); JCheckBox cb =
JCheckBox cb = new (JCheckBox)ie.getItem();
JCheckBox("C", true); jtf.setText(cb.getText());
cb.addItemListener(this); }
contentPane.add(cb);
cb = new JCheckBox("C++", }
false);

10/12/2025 10
1
Radio Buttons
 Radio buttons are supported by the
JRadioButton class, which is a concrete
implementation of AbstractButton.
 Its immediate super-class is JToggleButton,

which provides support for two-state


buttons.
 Some of its constructors are shown here:
 JRadioButton(Icon i)
 JRadioButton(Icon i, boolean state)
 JRadioButton(String s)
 JRadioButton(String s, boolean state)
 JRadioButton(String s, Icon i) 10/12/2025 10
2
import java.awt.*;
import java.awt.event.*;
Exampleimport javax.swing.*;
public class JRadioButtonDemo extends JApplet
implements ActionListener
{
JTextField tf;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JRadioButton b1 = new JRadioButton("A");
b1.addActionListener(this);
contentPane.add(b1);
10/12/2025 10
3
JRadioButton b2 = new bg.add(b3);
JRadioButton("B"); tf = new JTextField(5);
contentPane.add(tf);
b2.addActionListener(this);
}
contentPane.add(b2); public void
JRadioButton b3 = new actionPerformed(ActionEven
JRadioButton("C"); t ae)
b3.addActionListener(this); {
tf.setText(ae.getActionComm
contentPane.add(b3); and());
ButtonGroup bg = new }
ButtonGroup(); }
bg.add(b1);
bg.add(b2);

10/12/2025 10
4
Comboboxes
 Swing provides a combo box (a
combination of a text field and a dropdown
list) through the JComboBox class, which
extends JComponent.
 A combo box normally displays one entry.
 However, it can also display a drop-down

list that allows a user to select a different


entry.

10/12/2025 10
5
Comboboxes
 We can also type our selection into the text field.
Two of JComboBox’s constructors are shown here:
 JComboBox( )
 JComboBox(Vector v)
 JComboBox(Objects obj[])
 Here, v is a vector that initializes the combo box
and obj is the array of objects. Items are added
to the list of choices via the addItem( ) method,
whose signature is shown here:
 void addItem(Object obj)
 Here, obj is the object to be added to the combo
box.

10/12/2025 10
6
Important methods

 public void setEditable(boolean aFlag)


 It determines whether the JComboBox field is

editable or not?
 public boolean isEditable()

 It returns true if the JComboBox is editable. By

default, a combo box is not editable.


 public void setMaximumRowCount(int count)

 It sets the maximum number of rows the

JComboBox displays. If the number of objects


in the model is greater than ‘count’, the
combo box uses a scrollbar.
10/12/2025 10
7
 public void setSelectedItem(Object anObject)
 It sets the selected item in the combo box display
area to the object in the argument. If anObject is in
the list, the display area shows an Object selected.
 public void insertItemAt(Object anObject, int index)
 It inserts an item ‘anObject’ into the item list at a
given ‘index’.
 public void removeItem(Object anObject)
 It removes an item ‘anObject’ from the item list.
 public void removeItemAt(int anIndex)
 It removes the item at ‘anIndex’.

10/12/2025 10
8
public class Myapplet extends jc.addItem("Yellow");
JApplet implements ItemListener jc.addItemListener(this);
{ contentPane.add(jc);
JLabel jl; jl = new JLabel(new
ImageIcon green, red, black, yellow; ImageIcon("green.jpg"));
@SuppressWarnings("unchecked") contentPane.add(jl);
public void init() }
{ public void
Container contentPane = itemStateChanged(ItemEv
getContentPane(); ent ie)
contentPane.setLayout(new {
FlowLayout()); String s = (String)ie.getItem();
@SuppressWarnings("rawtypes") if(s=="Green")
JComboBox jc = new JComboBox(); jl.setIcon(new ImageIcon(s
jc.addItem("Green"); + ".jpg"));
jc.addItem("Red"); if(s=="Black")
jc.addItem("Black"); jl.setIcon(new ImageIcon(s
+ ".jpg"));
}
}
10/12/2025 10
9
Tabbed Panes
 A tabbed pane is a component that appears
as a group of folders in a file cabinet.
 Each folder has a title.
 When a user selects a folder, its contents

become visible.
 Only one of the folders may be selected at a

time.
 Tabbed panes are commonly used for
setting configuration options.
 Tabbed panes are encapsulated by the

JTabbedPane class, which extends


JComponent. 10/12/2025 11
0
Tabbed Panes
 There are three constructors of JTabbedPane.
 JTabbedPane()
 JTabbedPane(int tabPlacement)
 JTabbedPane(int tabPlacement, int tabLayoutPolicy)
 The first form creates an empty TabbedPane with a
default tab placement of JTabbedPane.TOP.
 Second form creates an empty TabbedPane with the
specified tab placement of any of the following:
 JTabbedPane.TOP
 JTabbedPane.BOTTOM
 JTabbedPane.LEFT
 JTabbedPane.RIGHT
10/12/2025 11
1
Tabbed Pane
 The third form of constructor creates an empty
TabbedPane with the specified tab placement and
tab layout policy.
 Tab layout policy may be either of the following:

 JTabbedPane.WRAP_TAB_LAYOUT

 JTabbedPane.SCROLL_TAB_LAYOUT

10/12/2025 11
2
Tabbed Pane
 Tabs are defined via the following method:
 void addTab(String str, Component comp)
 Here, str is the title for the tab, and comp is the
component that should be added to the tab.
Typically, a JPanel or a subclass of it is added.
 The general procedure to use a tabbed pane in an
applet is outlined here:
 Create a JTabbedPane object.
 Call addTab( ) to add a tab to the pane
 Repeat step 2 for each tab.
 Add the tabbed pane to the content pane of the
applet
10/12/2025 11
3
import javax.swing.*; class ColorsPanel
class LangPanel extends extends JPanel
JPanel
{ {
public LangPanel() public ColorsPanel()
{ {
JButton b1 = new
JButton("Marathi");
JCheckBox cb1 = new
add(b1); JCheckBox("Red");
JButton b2 = new add(cb1);
JButton("Hindi"); JCheckBox cb2 = new
add(b2);
JCheckBox("Green");
JButton b3 = new
JButton("Bengali"); add(cb2);
add(b3); JCheckBox cb3 = new
JButton b4 = new JCheckBox("Blue");
JButton("Tamil");
add(cb3);
add(b4);
} }
} }
10/12/2025 11
4
class FlavorsPanel public class Myapplet
extends JPanel extends JApplet
{ {
public FlavorsPanel() public void init()
{ {
JComboBox<String> jcb = JTabbedPane jtp = new
new JTabbedPane();
JComboBox<String>(); jtp.addTab("Languages", new
jcb.addItem("Vanilla"); LangPanel());
jcb.addItem("Chocolate"); jtp.addTab("Colors", new
jcb.addItem("Strawberry"); ColorsPanel());
add(jcb); jtp.addTab("Flavors", new
} FlavorsPanel());
} getContentPane().add(jtp);
}
}

10/12/2025 11
5
Scroll Panes
 JScrollPane is a lightweight container that
automatically handles the scrolling of another
component.
 If the object being scrolled is larger than the
viewable area, horizontal and/or vertical scroll
bars are automatically provided, and the
component can be scrolled through the pane.
 The viewable area of a scroll pane is called the
viewport.

10/12/2025 11
6
Scroll Panes
 The scroll bars scroll the component through the
viewport.
 In its default behavior, a JScrollPane will
dynamically add or remove a scroll bar as
needed.
 Forexample, if the component is taller than the
viewport, a vertical scroll bar is added.
 If the component will completely fit within the
viewport, the scroll bars are removed.
 https://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html

10/12/2025 11
7
Scroll Panes
 Some of its constructors are shown here:
 JScrollPane()

 JScrollPane(Component comp)

 JScrollPane(int vsb, int hsb)

 JScrollPane(Component comp, int vsb, int hsb)

 Here, comp is the component to be added to the

scroll pane. vsb and hsb are int constants that


define when vertical and horizontal scroll bars for
this scroll pane are shown.
 These constants are defined by the
ScrollPaneConstants interface.

10/12/2025 11
8
Scroll Panes

 Some examples of these constants are described


as follows:
 Constant Description

 HORIZONTAL_SCROLLBAR_ALWAYS Always
provide horizontal scrollbar
 HORIZONTAL_SCROLLBAR_AS_NEEDED Provide
horizontal scroll bar, ifneeded
 VERTICAL_SCROLLBAR_ALWAYS Always provide
vertical scrollbar
 VERTICAL_SCROLLBAR_AS_NEEDED Provide
vertical scroll bar, if needed

10/12/2025 11
9
Scroll Panes
 Here are the steps that you should follow to use a
scroll pane in an applet:
 Create a JComponent object.

 Create a JScrollPane object. (The arguments to

the constructor specify the component and the


policies for vertical and horizontal scroll bars.)
 Add the scroll pane to the content pane of the

applet.

10/12/2025 12
0
 import for(int i = 0; i < 20; i++)
java.awt.BorderLayout; {
 import java.awt.Container; for(int j = 0; j < 20; j++)
 import {
java.awt.GridLayout; jp.add(new
 import javax.swing.*; JButton("Button " + b));
++b;
 public class Myapplet }
extends JApplet }
 { int v = ScrollPaneConstants.
VERTICAL_SCROLLBAR_AS_NEEDED;
 public void init()
int h = ScrollPaneConstants.
 {
HORIZONTAL_SCROLLBAR_AS_NEEDED;
 Container contentPane = JScrollPane jsp = new
getContentPane(); JScrollPane(jp, v, h);
 contentPane.setLayout(new contentPane.add(jsp,
BorderLayout()); BorderLayout.CENTER);
 JPanel jp = new JPanel(); }
 jp.setLayout(new
GridLayout(20, 20)); }
 int b = 0;
10/12/2025 12
1
JTREES
 A tree is a component that presents a hierarchical
view of data.
 A user has the ability to expand or collapse

individual sub-trees in this display.

10/12/2025 12
2
JTREES
 Trees are implemented in Swing by the JTree class,
which extends
 JComponent. Some of its constructors are shown here:
 JTree(Hashtable ht)
 JTree(Object obj[ ])
 JTree(TreeNode tn)
 JTree(Vector v)
 The first form creates a tree in which each element of
the hash table ht is a child node.
 Each element of the array obj is a child node in the
second form.
 The tree node tn is the root of the tree in the third
form.
10/12/2025 12
 3
JTREES

 JTree is packaged in javax.swing, its


support classes and interfaces are
packaged in javax.swing.tree.
 JTree relies on two models:
TreeModel and
TreeSelectionModel.
 A JTree generates a variety of events,

but three relate specifically to trees:


TreeExpansionEvent,
TreeSelectionEvent, and
10/12/2025 12
4
JTREES
 TreeExpansionEvent events occur when a node
is expanded or collapsed.
 A TreeSelectionEvent is generated when the

user selects or deselects a node within the tree.


 ATreeModelEvent is fired when the data or

structure of the tree changes.


 The listeners for these events are
TreeExpansionListener,
TreeSelectionListener, and
TreeModelListener, respectively

10/12/2025 12
5
TREES
 The addTreeExpansionListener( ) and
removeTreeExpansionListener() methods allow
listeners to register and unregister for these
notifications.
 The signatures of these methods are shown here:

 void

addTreeExpansionListener(TreeExpansionListener
tel)
 void

removeTreeExpansionListener(TreeExpansionListe
ner tel)
 Here, tel is the listener object.

10/12/2025 12
6
TREES
 The getPathForLocation( ) method is used to
translate a mouse click on a specific point of the
tree to a tree path.
 Its signature is shown here:

 TreePath getPathForLocation(int x, int y)

 Here, x and y are the coordinates at which the

mouse is clicked.
 The TreePath class encapsulates information

about a path to a particular node in a tree.


 It provides several constructors and methods.

10/12/2025 12
7
TREES

 The TreeNode interface declares methods that


obtain information about a tree node.
 For example, it is possible to obtain a reference
to the parent node or an enumeration of the child
nodes.
 The MutableTreeNode interface extends
TreeNode.
 It declares methods that can insert and remove
child nodes or change the parent node.

 https://www.codejava.net/java-se/swing/jtree-basic-tutorial-and-examples#:~:text=JTree%20is%20
a%20Swing%20component,an%20item%20in%20a%20tree.&text=If%20a%20node%20doesn't,is
%20called%20a%20leaf%20node
.

10/12/2025 12
8
TREES
 The DefaultMutableTreeNode class implements
the MutableTreeNode
 interface. It represents a node in a tree.
 One of its constructors is shown here:

 DefaultMutableTreeNode(Object obj)
 Here, obj is the object to be enclosed in this tree
node
 The new tree node doesn’t have a parent or
children.

10/12/2025 12
9
Tree
 To create a hierarchy of tree nodes, the add( )
method of DefaultMutableTreeNode can be used.
 Its signature is shown here:

 void add(MutableTreeNode child)

 Here, child is a mutable tree node that is to be

added as a child to the current node.

10/12/2025 13
0
TREES
 Here are the steps that we should follow to use a tree in an applet:

10/12/2025 13
1
Example

10/12/2025 13
2
public class Example extends JApplet
{
JTree tree;
JTextField jtf;
public void init()
{
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new
DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a= new
DefaultMutableTreeNode("A");
top.add(a);

10/12/2025 13
3
DefaultMutableTreeNode a1=new
DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2=new
DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b= new
DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1=new
DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2=new
DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3=new
DefaultMutableTreeNode("B3");
b.add(b3);
10/12/2025 13
4
tree=new JTree(top);
int
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEED
ED;
JScrollPane jsp=new JScrollPane(tree,v,h);
contentPane.add(jsp,BorderLayout.CENTER);
jtf=new JTextField("",20);
contentPane.add(jtf,BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
}
10/12/2025 13
5
void doMouseClicked(MouseEvent me)
{
TreePath
tp=tree.getPathForLocation(me.getX(),me.getY());
if(tp!=null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
}

10/12/2025 13
6
Explanation
 The init( ) method gets the content pane
for the applet.
A DefaultMutableTreeNode object
labeled Options is created.
 This is the top node of the tree
hierarchy. Additional tree nodes are then
created, and the add( ) method is called
to connect these nodes to the tree.
A reference to the top node in the tree is
provided as the argument to the JTree
10/12/2025 13
7
Explanation
 To receive mouse events from the tree, the
addMouseListener( ) method of the JTree object is called.
 The argument to this method is an anonymous inner
class that extends MouseAdapter and overrides the
mouseClicked( ) method.
 The doMouseClicked( ) method processes mouse clicks.
 It calls getPathForLocation( ) to translate the coordinates
of the mouse click into a TreePath object.
 If the mouse is clicked at a point that does not cause a
node selection, the return value from this method is null.
 Otherwise, the tree path can be converted to a string
and presented in the text field.
 The string presented in the text field describes the path
from the top tree node to the selected node.

10/12/2025 13
8
JTables
A table is a component that displays rows
and columns of data.
 We can drag the cursor on column

boundaries to resize columns.


 We can also drag a column to a new

position.
 Tables are implemented by the JTable

class, which extends JComponent.

10/12/2025 13
9
JTables
 One of its constructors is shown here:
 JTable(Object data[ ][ ], Object colHeads[ ])
 JTable(int numRows, int numColumns)
 JTable(Vector rowData, Vector columnData)
 Here, data is a two-dimensional array of the information to be
presented, and colHeads is a one-dimensional array with the
column headings.
 The ‘numRows’ and ‘numColumns’ are values with which the
table is to be created.
 The ‘rowData’ and ‘columnData’ are the vector values by
which the table is constructed.

10/12/2025 14
0
JTables
 JTable relies on three models. The first is the
table model, which is defined by the
 TableModel interface. This model defines those

things related to displaying data in a two-


dimensional format.
 TableColumnModel. specifies the
characteristics of a column
 ListSelectionModel.

10/12/2025 14
1
JTables
 AJTable can generate several different events.
 The two most fundamental to a table’s operation are
ListSelectionEvent and TableModelEvent.
 A ListSelectionEvent is generated when the user
selects something in the table.
 By default, JTable allows you to select one or more
complete rows, but you can change this behavior to
allow one or more columns, or one or more individual
cells to be selected.
 A TableModelEvent is fired when that table’s data
changes in some way. Handling these events requires a
bit more work than it does to handle the events
generated by the previously described components
10/12/2025 14
2
JTables
 Here are the steps for using a table in an applet:
 1) Create a JTable object.

 2) Create a JScrollPane object. (The arguments to

the constructor specify the table and the policies


for vertical and horizontal scroll bars.)
 3) Add the table to the scroll pane.

 4) Add the scroll pane to the content pane of the

applet.

10/12/2025 14
3
Example

import java.awt.*;
import javax.swing.*;
public class Example extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final String[] colHeads = { "Name",
"Phone", "Fax" };

10/12/2025 14
4
final Object[][] data = {{ "Pramod", "4567",
"8675" },
{ "Tausif", "7566", "5555" },
{ "Nitin", "5634", "5887" },
{ "Amol", "7345", "9222" },
{ "Vijai", "1237", "3333" },
{ "Ranie", "5656", "3144" },
{ "Mangesh", "5672", "2176" },
{ "Suhail", "6741", "4244" },
{ "Nilofer", "9023", "5159" },
{ "Jinnie", "1134", "5332" },
{ "Heena", "5689", "1212" },
{ "Saurav", "9030", "1313" },
{ "Raman", "6751", "1415" }
};
10/12/2025 14
5
JTable table = new JTable(data, colHeads);
int v =
ScrollPaneConstants.VERTICAL_SCROLLBAR_A
S_NEEDED;
int h =
ScrollPaneConstants.HORIZONTAL_SCROLLBAR
_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
contentPane.add(jsp, BorderLayout.CENTER);
}
}

10/12/2025 14
6
Explanation
 The following example illustrates how to create and
use a table.
 The content pane of the JApplet object is obtained and
a border layout is assigned as its layout manager.
 A one-dimensional array of strings is created for the
column headings.
 This table has three columns.
 A two-dimensional array of strings is created for the
table cells. We can see that each element in the array
is an array of three strings.
 These arrays are passed to the JTable constructor.
 The table is added to a scroll pane and then the scroll
pane is added to the content pane.

10/12/2025 14
7
Assignment
 1) Using java swings component create
login page.(use JApplet)
 Hint: Two Jlabels, two Jtextfields.
 On JButton click event dialog box should

appear saying login successful.

10/12/2025 14
8
FAQs
ThanK You

You might also like