Unit -5 - OOPS through Java
Unit -5 - OOPS through Java
Syllabus
Event handling: event delegation model, sources of event, Event Listeners,
adapter classes, inner classes. AWT: introduction, components and containers,
Button, Label, Checkbox, Radio Buttons, List Boxes, Choice Boxes, Container
class, Layouts, Menu and Scrollbar.
1
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
java.awt.AWTEvent
• The java.awt.AWTEvent class is the root class for all AWT Events.
• java.awt.event packages includes the definition of events classes, listeners interfaces, and
adapters classes, which from the basics for event handling.
Event classes
• Java has a predefined hierarchy of event-related classes, at the root of which is
EventObject.
• It is actually a member of java.util package. This class has constructors and methods
defined as its members.
• One such constructor is EventObject(Object src_obj)
– where, src_obj is the object, which generates the event.
• EventObject has methods like getSource() and toString().
– getSource() – returns the source of the event
– toString() – returns the string equivalent of the event
2
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
ActionEvent
• The event is passed to an ActionListener object which is registered to receive the event
notification using the component’s addActionListener method.
• The object that inherits the ActionListener interface is passed to the ActionEvent when an
event occurs. Some of the fields, constructors, and methods associated with the
ActionEvent class
3
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Constructor of ActionEvent
Methods of ActionEvent
AdjustmentEvent
• The adjustment events are generated by Adjustable objects like scroll bar.
4
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Constructor of AdjustmentEvent
Methods of AdjustmentEvent
KeyEvent
• is generated by component object (such as a text field, Applet, Frame) when a key is
pressed, released, or typed.
• The event is passed to a KeyListener object which is registered to receive the event
notification using the component’s addKeyListener method.
• There can be three types of key events, which are identified by integer constants.
5
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Constructor
Methods in KeyEvent
6
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
MouseEvent
• A mouse action occurs in a particular component if and only if the mouse cursor is over the
defined part of the component’s bounds when the action happens.
• There are eight types of mouse events defined in the MouseEvent class.
• The MouseEvent class defines them as integer constants to identify each of these events.
Fields of MouseEvent
Constructors of MouseEvent
7
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Methods of MouseEvent
FocusEvent
– Permanent focus event occurs when the user explicitly changes focus from one
component to other, e.g. by pressing tab key.
– Temporary focus event occurs when the focus is lost due to operations like Window
deactivated. In this case, when the window will again be activated, the focus will
be on same component.
Fields of FocusEvent
8
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Constructor of FocusEvent
Methods of FocusEvent
ItemEvent
• This event is generated by an ItemSelectable object (such as a List), where the event is
generated when an item of the list is either selected or de-selected.
• The event generated is passed to every ItemListener object which is registered to receive
such events.
Fields of ItemEvent
9
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
TextEvent
Source of Events
• Button
• Choice
10
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
• MenuItem
• List
• Checkbox
• Window
• Scrollbar
• Text Components
• Event Listeners are created by implementing one or more interfaces defined by the
java.awt.event package.
Listeners
KeyListener
MouseListener
– void mouseClicked(MouseEvent e)
11
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
– void mouseEntered(MouseEvent e)
– void mousePressed(MouseEvent e)
– void mouseReleased(MouseEvent e)
– void mouseExited(MouseEvent e)
MouseMotionListener
– void mouseMoved(MouseEvent e)
– void mouseDragged(MouseEvent e)
• mouseMoved() is invoked when the mouse is moved from one place to another and
mouseDragged() is used when the mouse is dragged.
12
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
WindowListener
– void windowActivated(WindowEvent e)
– void windowClosed(WindowEvent e)
– void windowClosing(WindowEvent e)
– void windowOpened(WindowEvent e)
– void windowDeactivated(WindowEvent e)
– void windowIconified(WindowEvent e)
– void windowDeiconified(WindowEvent e)
Model
• Each source, generating the events must register event listeners to itself, so that listeners
get the license for receiving the events from the respective source.
• Each type of event has its own registration method, having the form,
• These listeners, once registered for events from a particular source, can get unregistered
also using
• Once the listener objects are registered, they must implement the methods to receive and
process the event notifications sent by source.
import java.awt.event.*;
g.drawString(“(“+xcord+”,”+ycord+”)”,xcord,ycord);}
xcord = me.getX();
ycord = me.getY();
repaint();}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
}}
MouseMotionEx m;
Demo(MouseMotionEx m)
14
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
this.m=m;
m.xcord = me.getX();
m.ycord = me.getY();
m.repaint();}
Adapter classes
• In listener interfaces, you have to implement all the methods defined in that interface
• This can be annoying at times, particularly when you need to implement methods of the
interface, which might not actually be used.
• In order to simplify things, Java came up with the concept of adapter classes.
• JDK defines corresponding adapter classes for listener interfaces containing more than
one methods e.g. for MouseMotionListener, MouseMotionAdapter class has been defined.
• Adapter classes provide empty definitions for all the methods of their corresponding
Listener interface.
15
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
import java.applet.*;
int xcord,ycord;
addMouseMotionListener(new MouseDemo(this));
g.drawString(“(“+xcord+”,”+ycord+”)”,xcord,ycord);
}}
AdapterDemo d;
MouseDemo(AdapterDemo d)
this.d = d;
16
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
d.xcord = me.getX();
d.ycord = me.getY();
d.repaint();
}}
Inner classes
• An inner class can be defined and instantiated all inside a class, or even within an
expression.
– member classes,
– annoymous classes.
• Member classes
– are included in the class definition just like fields and methods.
– Static member class: A member class can be static with access only to the static
members of the class to which it belongs.
17
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
– Instance member class: A member class can be instance with access to both the
static and instance members of the class that contains it.
• Local classes
– Such classes are created on the fly i.e. they are created, instantiated, used and
garbage collected when they are done.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
addKeyListener(new InnerClass()); }
showStatus(“key down”); }
18
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
addKeyListener(new KeyAdapter(){
showStatus(“Key Pressed”);}
showStatus(“Key Released”); }
}); }}
AWT: Introduction
• The Java Foundation Classes (JFC) provide two frameworks for building GUI-based
application and interestingly both rely on the same event handling model:
– AWT
– Swing
19
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
• AWT relies on the underlying operating system on a specific platform to represent its GUI
components (i.e components in AWT are called Heavyweight),
• Swing implements a new set of lightweight GUI components that are written in Java and
has a pluggable look and feel.
• These lightweight components are not dependent on the underlying window system.
• A graphical user interface is developed with the help of graphical elements like
• Actually, containers are themselves components, thus they can be placed inside other
containers.
• In AWT, all containers are objects of class Container or one of its subtypes.
20
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
21
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Component
– Button,
– Label,
– CheckBox,
22
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
23
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Button
• This class creates a button which when pushed or pressed generates an event.
• To create a button
– ‘buttonname’ is the name you give to the button object and ‘Str’ is the text you
want to appear on the button.
• Once the object for Button is created, it needs to be added to the applet or any other
container using
– add(buttonname);
Button Example
red.addActionListener(this);
white.addActionListener(this);
blue.addActionListener(this);}
if (str.equals(“Red”)) {
setBackground(Color.red);}
else if (str.equals(“white”)) {
24
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
setBackground(Color.white);}
else if (str.equals(“blue”)){
setBackground(Color.blue);}
repaint();}}
Label
• Labels consist of a text string for display only and they never call an action method.
25
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Label Example
import java.applet.*;
import java.awt.*;
add(firstLabel);
add(secLabel);
add(thirdLabel);}}
26
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Checkbox
• if you click on an unchecked checkbox, it will get checked and vice versa.
• Constructors of Checkbox
– Checkbox()
– Checkbox(String str)
27
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Checkbox Example
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Button submit;
Checkbox name1;
Checkbox name2;
Checkbox name3;
submit.addActionListener(this); }
if (str.equals(“SUBMIT”)) repaint();}
g.setFont(f);
g.setColor(Color.blue);
if (name1.getState())
28
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
g.drawString(“Ram”,50,60);
if (name2.getState())
g.drawString(“Ramesh”,50,80);
if (name3.getState())
g.drawString(“Naresh”,50,100); }}
Radio Buttons
• are special kind of checkboxes where only one box can be selected at a time.
• After creating checkbox group, the individual checkboxes are added to that group.
import java.awt.event.*;
29
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
add(new Label(“Notice that you can only select one radio button.”));
add(red);
add(white); add(green);
red.addItemListener(this);
white.addItemListener(this);
green.addItemListener(this); }
if (str.equals(“Red”)) {
setBackground(Color.red);}
else if (str.equals(“White”)) {
setBackground(Color.white);}
else if (str.equals(“Green”)){
setBackground(Color.green);}
repaint();}}
30
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
List
• provides a multiple choice, scrolling list of values that may be selected alone or together
– List()
31
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
– anyList.add(“apple”);
– anyList.add(“mango”);
– anyList.add(“guava”);
Example
populateList();
32
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
add(original);
add(b1); add(copy);
add(b2);
add(new Label(“Select an item from the list on the left and hit >>>> to place it in the
other list”));
b1.addActionListener(this); b2.addActionListener(this);}
original.add(“Grocery”);
original.add(“Fruits”);
original.add (“Ice-cream”);
original.add(“Vegetables”);
original.add(“Garments”);
original.add(“Baby Food”);}
copy.add(original.getSelectedItem());
original.remove(original.getSelectedIndex());}
else if(str.equals(“Clear”)) {
original.removeAll();
copy.removeAll();
populateList(); }
repaint(); }}
33
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Choice box
– c.add(“Red”);
– c.add(“Green”);
– c.select(“Red”);
– c.select(0);
34
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Methods of TextField
TextArea
35
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Container classes
• Window
• Frame
– Frame is a top-level window with a border and title. An instance of the Frame class
may have a menu bar, title bar and borders. It is otherwise like an object of the
Window class.
• Dialog
– Dialog is top-level display surface (a window) with a border and title. An object of
the Dialog class cannot exist without an associated object of the Frame class.
• Panel
– Panel is generic container for holding components. An instance of the Panel class
provides a container to which components can be added. It does not add any new
method; it simply implements the Container.
36
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Layouts
• FlowLayout
• BorderLayout
• GridLayout
• GridbagLayout
FlowLayout
• There is five pixel gap between the components arranged in this layout.
Fields of FlowLayout
37
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Constructors of FlowLayout
Example
LayoutManager flowLayout;
Button [] Buttons;
public FlowLayoutDemo() {
int i;
setLayout (flowLayout);
add (Buttons[i]);}}}
38
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Output
BorderLayout
• There can be only one component in each region and the regions are identified as
constants:
• Any of these five constant names can be used while adding a component to a container.
– pnl.setLayout(new BorderLayout());
39
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Fields of BorderLayout
Example
import java.awt.*;
super(title);
add(new Button(“North”),BorderLayout.NORTH);
add(new Button(“South”),BorderLayout.SOUTH);
add(new Button(“East”),BorderLayout.EAST);
add(new Button(“West”),BorderLayout.WEST);
add(new Button(“Center”),BorderLayout.CENTER);
setSize(400, 270);
setVisible(true);}
40
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Output
CardLayout
• Each card kept on another like a stack and only one card can be visible at a time.
• When the container is displayed after adding the first component, then the first component
is visible.
• The ordering of cards is determined by the container’s own internal ordering of its
component objects.
• CardLayout defines a set of methods that allow an application to flip through these cards
sequentially, or to show a specified card.
Example
import java.awt.*;
import java.awt.event.*;
41
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
cardPanel.setLayout(cLayout);
p1 = new Panel();
p1.setBackground(Color.red);
p2 = new Panel();
p2.setBackground(Color.yellow);
p3 = new Panel();
p3.setBackground(Color.green);
B1 = new Button(“Red”);
B1.addActionListener(this);
B2 = new Button(“Yellow”);
B2.addActionListener(this);
B3 = new Button(“Green”);
B3.addActionListener(this);
buttonP.add(B1);
buttonP.add(B2);
buttonP.add(B3);
cardPanel.add(p1, “B1”);
cardPanel.add(p2, “B2”);
cardPanel.add(p3, “B3”);
setLayout(new BorderLayout());
42
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
add(buttonP, BorderLayout.SOUTH);
add(cardPanel, BorderLayout.CENTER);
setVisible(true); setSize(300,200);
setTitle(“DemoCard”);
addWindowListener(new WindowAdapter(){
System.exit(0);}});}
if (e.getSource() == B1)
cLayout.show(cardPanel, “B1”);
if (e.getSource() == B2)
cLayout.show(cardPanel, “B2”);
if (e.getSource() == B3)
cLayout.show(cardPanel, “B3”);}
demo.cardDemo();} }
GridLayout
Example
import java.awt.event.*;
import java.awt.*;
43
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
public GridLayoutDemo() {
p.add(new Label(“Name”));
p.add(new TextField(5));
p.add(new TextField(3));
p.add(new Label(“Class”));
p.add(new TextField(3));
p.add(new TextField(3));
p.add(new Button(“Submit”));
p.add(new Button(“Cancel”));
add(p);
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
System.exit(0);}});}
}}
Output
44
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
GridBagLayout
• Component can occupy one or more cells, a.k.a its display area.
45
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
• fill
• Insets
• used for spacing between the component and the edges of its display area
• anchor
Example
import java.awt.*;
public GBLayoutDemo1(){
46
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
setLayout(new GridBagLayout());
add(l);
add(t);
add(b);
add(b1);
setSize(200,200); setVisible(true);}
Output
Menu
47
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
– MenuContainer and
– Accessible.
– MenuBar,
– Menu, and
– MenuItem.
Example
import java.awt.event.*;
import java.awt.*;
setTitle(“MenuDemo”);
setSize(250,150);
setMenuBar(menuBar);
48
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
newAction.addActionListener(this);
openAction.addActionListener(this);
exitAction.addActionListener(this);
fileMenu.addSeparator();
fileMenu.add(newAction);
fileMenu.addSeparator();
fileMenu.add(openAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);
menuBar.add(fileMenu);
cutAction.addActionListener(this);
copyAction.addActionListener(this);
pasteAction.addActionListener(this);
editMenu.add(cutAction);
editMenu.addSeparator();
editMenu.add(copyAction);
editMenu.addSeparator();
editMenu.add(pasteAction);
editMenu.addSeparator();
menuBar.add(editMenu);
setVisible(true);
49
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
addWindowListener(new WindowAdapter(){
System.exit(0);}}); }
String action=e.getActionCommand();
if(action.equals(“New”)){
System.out.println(“New”);}
else if(action.equals(“Open”)){
System.out.println(“File”);}
else if(action.equals(“Exit”)){
System.exit(0);}
else if(action.equals(“Cut”)){
System.out.println(“Cut”);}
else if(action.equals(“Copy”)){
System.out.println(“Copy”);}
else if(action.equals(“Paste”)){
System.out.println(“Paste”);}}
demo.demoMenu();} }
Output
50
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Scrollbar
• Scrollbars are used to select continuous values through a range of integer values (the
range set between maximum and minimum).
• The scrollbar’s maximum and minimum values can be set along with line increments and
page increments.
– Scrollbar(int direction, int initValue, int pageSize, int min, int max) throws
HeadlessException
Example
import java.awt.*;
import java.awt.event.*;
Label lbl;
int X=100,Y=150;
public ScrollbarDemo () {
51
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
HScroll.setMaximum (400);
VScroll.setMaximum (400);
setBackground (Color.cyan);
add (lbl,BorderLayout.NORTH);
add (HScroll,BorderLayout.SOUTH);
HScroll.addAdjustmentListener (this);
VScroll.addAdjustmentListener (this);
setSize(500,500); setVisible(true);
addWindowListener(new WindowAdapter(){
repaint();}
52
Dr. P.M.Manohar, Dept. of CSE, Raghu Engineering College(A), Visakhapatnam OOPS
through
Java –
Unit 5
Output
53