Unit 1 Advanced Java Programming TE SPPU
Unit 1 Advanced Java Programming TE SPPU
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
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
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
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
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,
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
client side.
method of applet.
public void paint(Graphics g): is used to
import java.applet.*;
Import java.awt.*;
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
<html>
<body>
<applet code=“Appletcode.class" width="300" height="300">
</applet>
</body>
</html>
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
References:
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
image){}
public abstract boolean
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
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
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,
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
platform dependent
10/12/2025 67
SWING
10/12/2025 70
Components
10/12/2025 71
Swings
JTree Encapsulates a tree-based control.
The Swing-related classes are contained in
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
10/12/2025 73
Swing Features
Easy mouseless operation
It is easy to connect keystrokes to
components.
Tooltips
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
10/12/2025 76
Class Description
10/12/2025 77
JApplet
JApplet.
Swing applets use the same four lifecycle : init(
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 object.
The content pane can be obtained via the
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
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
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
JLabel(Icon i)
Label(String s)
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.
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)
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
10/12/2025 93
JButton Class
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
10/12/2025 99
JCheckBox constructor
JCheckBox(Icon i)
JCheckBox(Icon i, boolean state)
JCheckBox(String s)
JCheckBox(String s, Icon i)
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,
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
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
editable or not?
public boolean isEditable()
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.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)
10/12/2025 11
8
Scroll Panes
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.
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
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
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:
mouse is clicked.
The TreePath class encapsulates information
10/12/2025 12
7
TREES
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:
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
position.
Tables are implemented by the JTable
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
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.
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
10/12/2025 14
8
FAQs
ThanK You