Delegation Event Model
Delegation Event Model
Principle:
• Once an event is received, the listener processes the event and then returns.
Advantage:
• The application logic that processes events is cleanly separated from the user interface logic that
generates those events.
• A user interface element is able to “delegate” the processing of an event to a separate piece of
code.
• In the delegation event model, listeners must register with a source in order to receive an event
notification.
• This provides an important benefit: notifications are sent only to listeners that want to receive
them.
KeyEvent and MouseEvent Both these events are generated by objects of type Component class and its
subclasses.
The KeyEvent is generated when the user presses or releases a key on the keyboard.
The MouseEvent is generated when the user presses the mouse or moves the mouse.
Ex1:
import java.awt.*;
import java.awt.event.*;
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b); add(tf);
setSize(300,300);
setLayout(null); setVisible(true);
tf.setText("Welcome");
new AEvent();
Ex2:
import java.awt.*;
import java.awt.event.*;
TextField t;
Button bLower,bUpper;
public ButtonClick(){
t=new TextField(20);
bLower=new Button(lowerText);
bLower.addActionListener(this);
add(t);
add(bLower);
setVisible(true);
setLayout(new FlowLayout());
setSize(200,200);
}
String tmp=t.getText();
String s=tmp.toLowerCase();
t.setText(s);
new ButtonClick();
} }
Ex3:
import java.awt.* ;
import java.awt.event.*;
implements ActionListener {
TextField t1;
Button b1;
Button b2;
A(){
setLayout(null);
setVisible(true);
t1=new TextField();
t1.setBounds(75,50,100,50);
b1.setBounds(50,200,50,50);
//b1.setSize(10);
b2.setBounds(100,200,50,50);
//b2.setSize(10);
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
add(t1);
{ String tmp;
if(obj.getSource()==b1)
{ tmp=t1.getText();
String t=tmp.toLowerCase() ;
t1.setText(t); }
if(obj.getSource()==b2)
tmp=t1.getText();
String t=tmp.toUpperCase();
t1.setText(t);}
class B
{ A obj=new A();
Ex 4:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
Label l0;
Label l1;
Label l2;
Label l3;
TextField t1;
Cal(){
l3=new Label("Result");
t1=new TextField();
setBackground(Color.BLACK);
l0.setForeground(Color.GREEN);
l1.setBounds(50,100,100,20);
l1.setFont(cfont);
l1.setForeground(Color.blue);
l2.setBounds(50,140,100,20);
l2.setFont(cfont);
l2.setForeground(Color.blue);
l3.setBounds(50,180,100,20);
l3.setFont(cfont);
l3.setForeground(Color.blue);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(50,250,50,20);
b1.setFont(bfont);
b1.setBackground(Color.GREEN);
b2.setBounds(110,250,50,20);
b2.setFont(bfont);
b2.setBackground(Color.GREEN);
b3.setBounds(170,250,50,20);
b3.setFont(bfont); b3.setBackground(Color.GREEN);
b4.setBounds(230,250,50,20);
b4.setFont(bfont);
b4.setBackground(Color.GREEN);
b5.setBounds(290,250,50,20); b5.setFont(bfont);
b5.setBackground(Color.GREEN);
add(l0);
add(l1);
add(l2);
add(l3);
add(t1);
add(t2);
add(t3);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
setLayout(null);
setVisible(true);
setSize(400,350);
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1) {
if(e.getSource()==b2){
t3.setText(String.valueOf(n1-n2));
if(e.getSource()==b3){
t3.setText(String.valueOf(n1*n2));
if(e.getSource()==b4){
t3.setText(String.valueOf(n1/n2));
if(e.getSource()==b5){
System.exit(0);
}}
new Cal(); } }
hw:
try:
Item Event
ItemEvent is generated when an item is selected or deselected.
Ex1:
import java.awt.*;
import java.awt.event.*;
Choice c; Label l;
public Itemlistener1(){
setSize(400,400);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
c=new Choice();
l=new Label();
c.add("Apple");
c.add("Mango");
c.add("Guava");
c.add("Orange");
c.add("Pineapple");
c.add("Grapes");
add(c);
add(l);
c.addItemListener(this); }
public static void main(String args[])
{
new Itemlistener1 ();
}
public void itemStateChanged(ItemEvent ie)
{
l.setText("You selected "+c.getSelectedItem());
}
}
Choice Methods:
if(ie.getStateChange()==ItemEvent.SELECTED)
l.setForeground(Color.red);
l.setBackground(Color.green);
3. KeyListener Interface
• Key event is generated whenever user presses/releases or types a key at the keyboard.
• Key events are fired by the component with the keyboard focus when the user presses or
releases keyboard keys.
keyTyped(KeyEvent) Called just after the user types a Unicode character into the listened-to component.
keyPressed(KeyEvent) Called just after the user presses a key while the listened-to component has the focus.
keyReleased(KeyEvent) Called just after the user releases a key while the listened-to component has the focus.
KeyEvent class
Method Purpose
int getKeyChar() Obtains the Unicode character associated with this event.
int getKeyCode() Obtains the key code associated with this event. The key code identifies the particular key on
the keyboard that the user pressed or released. For example, VK_A specifies the key labeled A,
boolean Returns true if the key firing the event is an action key. Examples of action keys include Cut,
isActionKey()
Copy, Paste, Page Up, Caps Lock, the arrow and function keys.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
TextField t1;
addKeyListener(this);
setBackground(Color.red);
t1 = new TextField(10);
t1.addKeyListener(this);
add(t1);
showStatus("KeyPressed");
setBackground(Color.orange);
showStatus("KeyRealesed");
setBackground(Color.green);
{ }}
4. MouseListener Interface
• Mouse events notify when the user uses the mouse (or similar input device) to interact with a
component.
• Mouse events occur when the cursor enters or exits a component's onscreen area and when the
user presses or releases one of the mouse buttons.
Method Purpose
mouseClicked(MouseEvent) Called just after the user clicks the listened-to component.
mouseEntered(MouseEvent) Called just after the cursor enters the bounds of the listened-to component.
mouseExited(MouseEvent) Called just after the cursor exits the bounds of the listened-to component.
mousePressed(MouseEvent) Called just after the user presses a mouse button while the cursor is over the listened-to component.
mouseReleased(MouseEvent) Called just after the user releases a mouse button after a mouse press over the listened-to component.
5. MouseMotionListener Interface
• Mouse-motion events notify when the user uses the mouse to move the onscreen cursor.
mouseDragged(MouseEvent) Called in response to the user moving the mouse while holding a mouse button down.
This event is fired by the component that fired the most recent mouse-pressed event, even if the
mouseMoved(MouseEvent) Called in response to the user moving the mouse with no mouse buttons pressed. This event is
MouseEvent class
Method Purpose
int getClickCount() Returns the number of quick, consecutive clicks the user has made (including this event).
int getButton() Returns which mouse button, if any, has a changed state. One of the following constants is
int getX() Return the (x,y) position at which event occurred, relative to the component that fired event.
int getY()
Mouse Entered
Mouse is
moving(397,65)
Mouse is
moving(372,36)
Mouse Exited
Mouse Entered
Mouse Pressed
Mouse is
dragging(492,368)
Mouse is
dragging(493,369)
import java.awt.*;
Mouse is
import java.awt.event.*;
dragging(493,370)
Mouse Released
class MyMouse implements MouseListener,MouseMotionListener {
Mouse is clicked
Frame fr;
Mouse is
Button btn;
moving(511,399)
Label lbl;
Mouse is
TextField tf;
moving(545,421)
public TestMouseAndMouseMotionListener(){
fr = new Frame();
fr.setLayout(null);
fr.setSize(500,300);
fr.setVisible(true);
fr.setBackground(Color.RED);
lbl.setSize(300,20);
lbl.setLocation(50,50);
fr.add(lbl);
fr.addMouseListener(mm);
fr.addMouseMotionListener(mm);
new TestMouseAndMouseMotionListener();}
fr.setBackground(Color.yellow);
System.out.println("Mouse Entered");
fr.setBackground(Color.black);
System.out.println("Mouse Exited");
fr.setBackground(Color.pink);
System.out.println("Mouse Pressed");
fr.setBackground(Color.blue);
System.out.println("Mouse Released");
fr.setBackground(Color.gray);
System.out.println("Mouse is clicked");
}
System.out.println("Mouse is moving"+"("+m.getX()+","+m.getY()+")");
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with given horizontal and vertical gap.
public void next(Container parent): is used to flip to the next card of the given container.
public void previous(Container parent): is used to flip to previous card of given container.
public void first(Container parent): is used to flip to the first card of the given container.
public void last(Container parent): is used to flip to the last card of the given container.
public void show(Container parent, String name): is used to flip to specified card with the given name.
Note: show method is valid for any card, not limited to first, last, next and previous card.
Note: Always use next, previous, first, last and show methods with CardLayout obect
public void add(Container parent, String name): Flips to component that was added to this layout with
the specified name
public void add (String CardName, Container card)(can interchange arguments)
//Flips to component that was added to this layout with the specified name
panel.add("Button", buttonPanel);
panel.add(textBoxPanel, "Text");
/* CardLayout: Pressing one of three buttons will cause a different "card" to be displayed.*/
Panel cardPanel; // the container that will hold the various "cards"
Panel firstP, secondP, thirdP; // each of these panels will constitute the "cards"
cardPanel.setLayout (ourLayout);
firstP.setBackground(Color.red);
secondP.setBackground(Color.blue);
thirdP.setBackground(Color.green);
first.addActionListener(this);
second.addActionListener(this);
third.addActionListener(this);
buttonP.add(first);
buttonP.add(second);
buttonP.add(third);
this.setLayout(new BorderLayout());
//button Panel goes South, card panels go Center
this.add(buttonP, BorderLayout.SOUTH);
this.add(cardPanel, BorderLayout.CENTER);
// method takes 1.) an Object (the card) 2.) an identifying String(card name)
if (e.getSource() == second)
ourLayout.show(cardPanel, "Second");
// ourLayout.previous(cardPanel);
if (e.getSource() == third)
ourLayout.show(cardPanel, "Third");
// ourLayout.next(cardPanel);
} } // end class
/*<applet code="carddemo.class“
height="500" , width="600"> </applet>*/