Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 Event Handling

2 CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event i.e. Event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen.

3 CSI 3125, Preliminaries, page 3 Event Handling Types of Event The events can be broadly classified into two categories: Foreground Events - Those events which require the direct interaction of user. A person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page etc. Background Events - Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.

4 CSI 3125, Preliminaries, page 4 Event Handling Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs.

5 CSI 3125, Preliminaries, page 5 Event Handling 3 Components of event handling Event :- Change of state of an object Source :- Object that generate an event Listener :- Object that listen to the event A listener get notified when an event occurs A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return. Events are supported by a number of Java packages like Java.util, java.awt,java.awt.event

6 CSI 3125, Preliminaries, page 6 Event Handling Steps involved in event handling The User clicks the button and the event is generated. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. Event object is forwarded to the method of registered listener class. The method is now get executed and returns.

7 CSI 3125, Preliminaries, page 7 Event Handling Following steps are required to perform event handling: Implement the Listener interface and overrides its methods Register the component with the Listener

8 CSI 3125, Preliminaries, page 8 Event Handling Some of the event classes are ActionEvent  Generated when a Button is pressed, or menu is selected, or doubled clicked in a list item… AdjustmentEvent  Generated when a scroll bar is manipulated ComponenetEvent  when a component is moved, or hiddenor resize.. FocusEvent  when a component gain or lost fosus ItemEvent  when check box or list item is clicked KeyEvent  when an input received through keyboard MouseEvent  when mouse clicked, dragged, doubled clicked…. WindowEvent  when a window activated, closed, deactivated….

9 CSI 3125, Preliminaries, page 9 Mouse Event Handling prog The interface MouseListener contains 5 methods, should override these meths First register the Listener addMouseListener(this); in init() public void mouseClicked(MouseEvent m)  when button clicked public void mouseExited(MouseEvent m)  when exited from window public void mouseEntered(MouseEvent m)  when entered in the window public void mouseReleased(MouseEvent m)  when button released public void mousePressed(MouseEvent m)--> when button pressed Methods of MouseEvent class getX()  return x co-ordinate getY()  return y co-ordinate getClickCount()  return mouse click count getButton()  return the mouse Button

10 CSI 3125, Preliminaries, page 10 Mouse Event Handling prog import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class mouse extends Applet implements MouseListener { int x,y; String str; public void init() {addMouseListener(this);} public void mouseClicked(MouseEvent m) {str="Clicked";x=m.getX();y=m.get Y(); repaint();} public void mouseExited(MouseEvent m) {str="Exited";x=m.getX();y=m.getY (); repaint();} public void mouseEntered(MouseEvent m) {str="Entered";x=m.getX();y= m.getY(); repaint();} public void mouseReleased(MouseEvent m) {str="Released";x=m.getX();y =m.getY(); repaint();} public void mousePressed(MouseEvent m) {str="Pressed";x=m.getX();y= m.getY(); repaint();} public void paint(Graphics g) {g.drawString(str+" coodinates "+x+" "+y,50,50);} }

11 CSI 3125, Preliminaries, page 11 Mouse motion Event Handling prog This interface defines 2 methods public void mouseDragged(MouseEvent m)  when dragged public void mouseMoved(MouseEvent m)  when moved

12 CSI 3125, Preliminaries, page 12 Mouse motion Event Handling prog import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class mouse1 extends Applet implements MouseMotionListener { int x,y,z; String str; public void init() {addMouseMotionListener(this);} public void mouseDragged(MouseEventm) {str="dragged";x=m.getX();y=m.getY(); repaint();} public void mouseMoved(MouseEvent m) {str="move";x=m.getX();y=m.getY(); repaint();} public void paint(Graphics g) {g.drawString(str+" coodinates "+x+" "+y,50,50);} }

13 CSI 3125, Preliminaries, page 13 Key Event Handling prog This interface defines 3 methods should override the methods Public void keyPressed(KeyEvent k)  when a key is pressed Public void keyReleased(KeyEvent k)  when a key is released Public void keyTyped(KeyEvent k)  when character typed When a user pressed and released a key, 3 events are generated

14 CSI 3125, Preliminaries, page 14 Key Event Handling import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class key extends Applet implements KeyListener { int x,y,z,a,b; String str; public void init() {addKeyListener(this);} public void keyTyped(KeyEvent m) {str="Typed";repaint();} public void keyReleased(KeyEvent m) {str="Released";repaint();} public void keyPressed(KeyEvent m) {str="Pressed";repaint();} public void paint(Graphics g) {g.drawString("Key "+str,50,50);} }

15 CSI 3125, Preliminaries, page 15 Scrollbar Event Handling import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class M extends Applet implements AdjustmentListener { Scrollbar slider; int sliderValue = 0; public void init() { slider=new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, 101); add(slider); slider.addAdjustmentListener(this); } public void paint(Graphics g) { slider.setLocation(20,30); slider.setSize(150,20); g.setFont(new Font("TimesRoman", Font.BOLD, 20)); g.drawString("Current value is " + sliderValue, 50,100); } public void adjustmentValueChanged(Adjustme ntEvent e) { sliderValue = slider.getValue(); repaint(); }

16 CSI 3125, Preliminaries, page 16 The WindowListener Interface This interface defines seven methods. void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we)

17 CSI 3125, Preliminaries, page 17 The WindowListener Interface void windowActivated(WindowEvent e) Invoked when the Window is set to be the active Window. void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window. void windowDeiconified(WindowEvent e) Invoked when a window is changed from a minimized to a normal state. void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowOpened(WindowEvent e) Invoked the first time a window is made visible.

18 CSI 3125, Preliminaries, page 18 WindowListener Handling import java.awt.*; import java.awt.event.*; public class w1 extends Frame implements WindowListener { public static void main(String qrg[]) {w1 f=new w1(); } public w1() { addWindowListener(this); Frame f=new Frame(); setTitle("POPO Frame"); setSize(500,500); setVisible(true); } public void windowActivated(WindowEvent we){System.out.println("Activated");} public void windowClosed(WindowEvent we){System.out.println("Closed");} public void windowClosing(WindowEvent we){System.out.println("Closing");} public void windowDeactivated(WindowEvent we){System.out.println("Deactivated");} public void windowDeiconified(WindowEvent we){System.out.println("Deicnified");} public void windowIconified(WindowEvent we){System.out.println("Iconified");} public void windowOpened(WindowEvent we){System.out.println("Opened");} }

19 CSI 3125, Preliminaries, page 19 Event Handling


Download ppt "CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event."

Similar presentations


Ads by Google