3. Applet Component
Label :-
Constructor:-
new Label( ) throws HeadLessException
new Label(String str) throws HeadlessException
new Label(String str,int how) throws HeadlessException
how=Label.LEFT or Label.RIGHT or Label.CENTER
Methods:-
void setText(String str)
String getText();
void setAlignment(int how)
int getAlignment();
4. Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
Label L1,L2;
public void init()
{
L1=new Label();
L1.setText(“ABCD”);
L2=new Label();
add(L1);
add(L2);
}
}
5. Applet Component
Button :-
Constructor:-
new Button() throws HeadlessException
new Button(String str) throws
HeadlessException
Methods:-
void setLabel(String str); //label for the
buttons.
String getLabel();
6. Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
Button b1,b2;
public void init()
{
b1=new Button();
b1.setText(“PUSH”);
L2=new Button();
add(b1);
add(b2);
}
}
7. Applet Component
Check Boxes
Constructor :-
new Checkbox()throws HeadlessException // blank checkbox unchecked
new Checkbox(String )throws
HeadlessException// create a named checkbox
new Checkbox(String ,boolean ) throws
HeadlessException // create a named checkbox if true checkbox is
checked othewise it is false
new Checkbox(String,boolean,CheckboxGroup) throws
HeadlessException
new Checkbox(String,CheckboxGroup,boolean) throws
HeadlessException
//create a named checkbox with initial stateeither true or false depend on boolean value
, and
also declare that check box is a part of checkbox group or not.
Method:-
boolean getState();
void setState(String str);
String getLabel();
9. Applet Component
CheckboxGroup:- radiobutton only one checkboc at a time .
Constructor:-
new CheckboxGroup();
Method:-
Checkbox getSelectedCheckbox();
void setSelectedCheckbox(Checkbox which)
11. Applet Component
Choice Controls:- Popup list of item which the
user may choose.
Constructor :-
new Choice();
Methods:-
void add(String name)
String getSelectedItem();
int getSelectedIndex();// index start from 0.
void select(int index);
void select (String name) ;
String getItem(int index) ;
12. Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
Choice c1;
public void init()
{
Choice c1=new Choice();
c1.add(“WIN95”);
c1.add(“WIN98”);
c1.add(“WINXP”);
add(c1);
}
}
13. Applet Component
Lists :- Multiple choice scroll selection list.
Constructor :-
new List();
new List(int numrows);
new List(int numrows,boolean multiselect)
Method:-
void add(String name);
void add(String name,int index); // index start from 0.
index=-1 it means add the
item at the end of the list.
String getSelectedItem(); // index of selected item.
int getSelectedIndex(); //index start from 0. if more then
one item is selected or no
selection is made -1 is returned.
String [ ] getSelectedItems() ;//names of selcted items
int [ ] getSelectedIndexes() ; //indexes of selected items.
14. Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
List l1;
public void init()
{
l1=new List(5, true);
l1.add(“WIN95”);
l1.add(“WIN98”);
l1.add(“WINXP”);
l1.add(“WINVISTA”);
l1.add(“WIN&”);
add(l1);
}
}
15. Applet Component
Scroll Bar :- Used to select continuous values between
a specified minimum and maximum .
Constructor:-
new Scrollbar() // horiozontal scrollbar
new Scrollbar(int style) //style=Scrollbar.Horizontal
Scrollbar.Vertical
new Scrollbar(int style,int initialvalue,int
thumbsize,int min,int max);
//initialvalue of the scroolbar, thumbsize represent the no of
represent by the height of the thumb . minimumn and maximum
value of scrollbar is represented by min and max .
16. Applet Component
Scrollbar:-
Methods :-
void setValues(int initval,int thumbsize,int max,int min);
int getValue();// To retrieve the current value
void setValue(int newvalue);// To set the new value of
scrollbar.
int getMinimum();
int getMaximum();
void setUnitIncrement(int newincr);
void setBlockedIncrement(int newincr);
// each time the 1 is incremented from the scroll bar each time it is scrolled
up or down one line. By default pageup and pagedown value are 10 we
can change it by above two methods.
17. Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
Scrollbar hsb,vsb;
public void init()
{
hsb=new Scrollbar(Scrollbar.Horizontal,0,1,0,50);
vsb=new Scrollbar(Scrollbar.Vertical,0,1,0,50)
add(hsb);
add(vsb);
}
}
18. Applet Component
TextField:-
Costructor:-
new TextField();
new TextField(int numchar);// create a textfield numchar
wide
new TextField(String str); // create a text field with str.
new TextField(String str,int numchar); //create a textfield
sets with str and numchar set its width;
Methods:-
String getText();
void setText(String str);
String getSelectedText();
void select(int startindex,int endindex);//select at startindex and ending at endindex-
1
void setEditable();
boolean isEditable();
20. Applet Component
TextField:-
Costructor:-
new TextArea();
new TextArea(int numline,int numchar);
new TextArea(String str);
new TextField(String str,int numline,int numchar);
new TextField(String str,int numline,int numchar,int sbars);// bars= SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Methods:-
String getText();
void setText(String str);
String getSelectedText();
void select(int startindex,int endindex);//select at startindex and ending at endindex-1
void setEditable();
boolean isEditable();
21. Applet Component
TextField:-
Methods:-
void append(String str); // append the string
specified by str
void insert(String str,int index);//pass the string
str at specified
by the index
void replaceRange(String str,int
startind,int end ind); // replace the
characters from
startindex to
endIndex-1
22. Applet Component
( MenuBar,Menu,MenuItem)
Constructors:-
MenuBar :-
new MenuBar();
Menu:-
new Menu();
new Menu(String optionname);
MenuItem:-
new MenuItem();
new MenuItem(String str);
new MenuItem( String str,MenuShortcut keyAccel);
25. Applet Component
Container:-
1) Inherit the Component class .
2) It has additional method that allow other component object to
be nested within it.
3)Container object contain other Container object. This makes
multilevel containment system.
4)Conatianer is responsible for laying out(positioning) any
component that it contain.It does this through the
layout managers.
methods:-
void add(Component cmp);
void remove(Component cmp);
26. Layout Manager
Every Component that we place in the Container
has a size and location in Container.This size and
location of the component in the Container is
managed by the LayoutManager. Following are the
types of LayoutManagers
FlowLayoutManager
GridLayoutManager
BorderLayoutManager
CardLayoutManager
27. Layout Manager
Methods:-
void setLayout(LayoutManager layoutobj);
// layoutobj is the object of LayoutManager
class.if we pass null then we disable the
layout manager and determine the shape and
position manually using setBound() defined
by Component class.
28. Layout Manager
FlowLayoutManager :-
new FlowLayout(); // maintain the space of 5 pixels between each
component.
new FlowLayout(int how) // how specify
alignment of component
how=FlowLayout.LEFT
FlowLayout.RIGHT
FlowLayout.CENTER
new FlowLayout(int how,int horz, int vert )
// horz & vert specify horizontal and vertical space left between each component .
30. Layout Manager
GridLayoutManager:-
new GridLayoutManager( ); //create a grid of
single row
and single
column .
new GridLayoutManager(int row, int col);
new GridLayoutManager(int row, int col,int
horz,int col);
32. Layout Manager
BorderLayoutManager :-
It has four narrow fixed width components at the edges and one large area in the
center ,The four sides is referred to as north, south,east,west and middle are is referred as center.
BorderLayout reffers the following constant that specify the regions.
BorderLayout.CENTER
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.SOUTH
BorderLayout.NORTH
All the regions specify with the Component when we add that component in that
Container.
void add(Component compobj , Object region);
36. Event Handling
Event:-
It can be generated as a consequence of
person interacting with the elements in a gui.
Some of the activities that cause events to be
generated are pressing a button ,entring a
character via a keyboard , selecting a list etc.
“ In the Event Delegation model,an Event is an
object that describes a state change in a source.
”
37. Event Handling
To process the events we need to identify this
events and then take some action. This is known as event
handling. The modern approach to handling an event is
based on “Event Delegation Model”. In this model we
have 3 following entities .
Source
Event
Handler or Listenr
38. Event Handling
Event Delegation Model :-
(Delegation Event Model)
Source generates Events
Handeled by
register
Handler
39. Event Handling
Source :- Source is used to generate the event and it
is mainly a Component.Examples Button,TextField
etc. Every source and component has the
predefined .
Event :- An Event can be generated by the source.It
can possible that the same events are generated by
different sources. Events are represented by classes .
Handler or Listener:- To handle the event handlers
are used . Every event is a predefined handler. The
event can be generated by any source but the
handler is always be same. Handlers are represented
by or interfaces.
40. Event Handling
ActionEvent is an event is generated when we
click on button ar press the enter key in TextField
. And this ActionEvent is handled by
ActionListener interface.
Button onclick ActionEvent handled by ActionListener
on enter
TextField
41. Event Handling
ActionListener is an interface which contains
the following method.
public void actionPerformed(ActionEvent ae)
When the ActionEvent is generated the
ActionListener’s actionPerformed method get
executed . The operation that we want to
perform on the click of Button should be put
within the actionPerformed method.
42. Event Handling
For the event handling we need to create the handler class. The class will be
handler class when it implement the appropriate listener and override the
methods of interface.
Ex:-
public class A implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
--------------
--------------
--------------
--------------
}
}
43. Event Handling
Handler 1:-
public class A implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
--------------
--------------
--------------
--------------
}
}
Handler 2:-
public class B implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
--------------
--------------
--------------
--------------
}
}
44. Event Handling(ActionEvent)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApp extends Applet
{
public TextField t1,t2,t3;
Button b1;
FlowLayout fl;
public void init()
48. Event Handling(ActionEvent)
public void actionPerformed(ActionEvent ae)
{
int i=Integer.parseInt(th1.getText());
int j=Integer.parseInt(th2.getText());
String s1=i+j+ "";
th3.setText(s1);
}
}
49. Event Handling
Once the handler is created it should be registered with source without
which eventhandling is not possible it is necessary otherwise we don’t
know which handler handle the event.
To register the handler with the source th methods are provided in the
source.
Registration methods of Button and Textfield source is following .
void addActionListener(ActionListner al)
// al represent the object of that class which implement the
ActionListener interface. Method belonging to Button and Textfield
class.
50. Event Handling
Action Event- 1)button is clicked.
2) list item is double clicked.
3)menu item is selected.
4) txt field is entered.
ActionListener :-
public void actionPerformed(ActionEvent ae)
51. Event Handling
ItemEvent:- 1) checkbox or list item is
selected .
2) choice selection is made
3)Checkable menu item is
selected or deselected.
ItemListener:-
public void itemStateChanged(ItemEvent ie)
52. Event Handling
AdjustmentEvent :-Scroll bar is manipulated .
AdjustementListener:-
void adjustmentValueChanged(
AdjustmentEvent e);
53. Event Handling
ComponentEvent:- when a component is
hidden moved resized or
become visible.
ComponentListener:-
void ComponentResized(ComponentEvevnt ce);
void ComponentMoved(ComponentEvevnt ce);
void ComponentShown(ComponentEvevnt ce);
void ComponentHidden(ComponentEvevnt ce);
63. Inner class
Classes which is defined within another class.
class A
{
Class B
{
}
}
A is container class or top level class. B is inner class.
One class can contain any no of inner class./Inner
classs can contain inner class and this can go upto
any level.
64. Inner class
Inner classes are of 4 types.
1)Static inner class
2)Non static inner class
3)Local inner class
4) Anonymous inner class
65. Inner class
Non static inner class:- This class ork as the
member of class like
static inner class they can have any type of
accessibility (public/private/protected/default)
,but like static inner class they can not have static members .
Because the non static class is the member of top level class and
non static member exist when its object is created .The non static
inner class doesn’t exist until the object of top level class created.
A non static inner class is always associated within the object of
top level class and therefore it has the special privilege it can
access any member(static/non static) of its top level class . So
that non static inner class is used for event handling.
66. Panel
A Component which is used as a Container. It
is used for physically grouping of Component.
Constructor:-
new Panel(); // Create the panel with Flow
layout Manager.
new Panel(LayoutManager lm); // Create
the panel with
same
layoutManager by
which is pass as the parameter
in the panel.
68. Standalone application
To cretae a stand alone application we ned to create a class which will
inherit Frame class . To run it we don’t need html ,this type of
application has it ‘s own window. And this type of application created
just like console based application.
Features of stand alone application.
1) Standalone application is also called
Frame.
2)Frame provide the outlaw of window we only need to place the
components on window.
3)Frame has the same applarcchitecture and structure as the applet and
therefore GUI application and the event handling will be same as applet.
4)Like applet it doesn’t have init,start,stop,destroy,paint methods and
therefore instead of initialize the applet we need we need to define the
constructor of that class which inherit the Frame class.
5)To display standalone applicatio we need main method.just like console
based application
69. Standalone application
Import java.awt.*;
Impoort java.applet.*;
Import java.awt.event.*;
public class Hobby extends Frame implements ItemListener
{
---------------
--------------
public Hobbbby( )
{
-----------------
------------------
------------------ same as init
}
74. Standalone application
public void actionPerformed(ActionEvent ae)
{
int i=Integer.parseInt(t1.getText());
int j=Integer.parseInt(t2.getText());
t3.setText(i+j+"");
}
}
75. Standalone application
public class MyWindow
{
public static void main(String arg[])
{
MyFrame mf=new MyFrame();
mf.setSize(300,300);
mf.setVisible(true);
}
}
76. Standalone application
Some issues on Window:- When w click on crossbar th window
doesn’t terminate and still on RAM to close it we have to close the
DOS prompt because the event is generated by Frame we have
to handle the event of Frame. There are 7 state of Frame when it
generate event. In all 7 state it generate the WindowEvent .
To handle the 7 state of Frame WindowListener
interface provide 7 different methods.When we implement the
WindowListener we need to override the all 7 methods.
void windowActivated(WindowEvent we);
void windowClosed(WindowEvent we);
void windowClosing(WindowEvent we);
78. Standalone application
1) windowOpened is called after the application
display on the screen . It called only once .It is used to initialize the
application.
2)windowIconified is called when window is minimized .
3) windowDeiconified is called when window is maximized .
4) winowActivate is called when the application get the focus.
5)windowDeactvate is called when the application lost the focus.
6)windowClosed is called when we click on cross button .
7)windowClosing
dispose():- is called to terminate the application
System.exit(0) :-is called to terminate the dos prompt or to
terminate the JVM.
79. Standalone application
windowClosing is called when the application
is dispose.It is used to save the state of
application.This is called only once but closed
is not called untill dispose is called .
Registration method :-
void addindowListner(WindowEvent we)
89. Adapter class
---------------------
}
public class MyWindowAdapter
{
public static void main(String arg[])
{
-------------------
-------------------
---------------------
}
}
90. Adapter class
All listener those contain more than one
methods have Adapter classs.
WindowListener .
KeyListener
MouseListener
MouseMotionListener.