Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Design & Development Lecture 12

Similar presentations


Presentation on theme: "Web Design & Development Lecture 12"— Presentation transcript:

1 Web Design & Development Lecture 12

2 Event Handling part 2

3 Important Points Revisited

4 Important Points Revisited
To handle a particular kind of event, we have to implement a corresponding interface For example Button generates action event, we have to implement ActionListener interface to handle action events Window generates window event, we have to implement WindowListener interface to handle window evetns

5 Important Points Revisited
Event Handling Steps Step 1 Create components which can generate events Step 2 Build component (objects) that can handle events (Event Handlers) Step 3 Register handlers with generators

6 More Examples

7 Handling Mouse Events

8 Handling Mouse Events Mouse events can be trapped for any GUI component that inherit from Component class. For example, JPanel, JFrame & JButton etc. To handle Mouse events, two types of listener interfaces are available MouseMotionListener MouseListener

9 Handling Mouse Events MouseMotionListener
For processing mouse motion events Mouse motion event is generated when mouse is moved or dragged public interface MouseMotionListener { public void mouseDragged (MouseEvent me); public void mouseMoved (MouseEvent me); }

10 Handling Mouse Events MouseListener
For processing “interesting” mouse events Mouse event is generated when mouse is Pressed Released clicked (pressed & released without moving the cursor) Enter (mouse cursor enters the bounds of component) Exit (mouse cursor leaves the bounds of component)

11 Handling Mouse Events Interface MouseListener
public interface MouseListener { public void mousePressed (MouseEvent me); public void mouseClicked (MouseEvent me); public void mouseReleased (MouseEvent me); public void mouseEntered (MouseEvent me); public void mouseExited (MouseEvent me); }

12 Example Code Handling Mouse Motion Events

13 Steps for Handling Mouse Motion Events
? Create mouse motion events generating components JFrame myFrame = new JFrame(); ? Create Event Handler Implement MouseMotionListener Interface Provide implementaion for the required method public void mouseMoved (MouseEvent e) { // write your code here } provide empty bodies for remaining methods in the interface.

14 Steps for Handling Mouse Motion Events
Register event generator with event handler myFrame.addMouseMotionListener (this); Object of the Event Hanlder class

15 Example Code (cont.) Handling Mouse Motion Events
// File EventsEx.java, Code listed in Handout section 12.1 ……… // import required packages public class EventsEx implements MouseMotionListener { JFrame f; JLabel coord; public void initGUI ( ){ ….. // set layout ….. // add label to container // registration f.addMouseMotionListener(this) ….. } // end of initGui

16 Example Code (cont.) Handling Mouse Motion Events
// MouseMotionListener event handlers public void mouseDragged (MouseEvent me) { int x = me.getX(); int y = me.getY(); coord.setText("Dragged at [" + x + "," + y + "]” ); } public void mouseMoved (MouseEvent me) { coord.setText(“Moved at [" + x + "," + y + "]” );

17 Example Code (cont.) Handling Mouse Motion Events
public static void main (String args[ ]){ EventsEx ex = new EventsEx(); } } // end of EventsEx class

18 Handling Mouse Motion Events
Live Output

19 Another Example Handling Window Events

20 Handling Window Events
Want to handle Window Exit event only Why ? When window is closed, control should return back to command prompt But we have already achieved this functionality through following line of code frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); But, what if we want to display some message (Good Bye ) before exiting

21 Handling Window Events
How ? To handle window events, we need to implement “WindowListner” interface. “WindowListner” interface contains 7 methods We require only one i.e. windowClosing We have to provide definitions of all methods to make concrete class

22 Interface WindowListener
public interface WindowListener { public void windowActivated (WindowEvent we); public void windowClosed (WindowEvent we); public void windowClosing (WindowEvent we); public void windowDeactivated (WindowEvent we); public void windowDeiconified (WindowEvent we); public void windowIconified (WindowEvent we); public void windowOpened (WindowEvent we); }

23 Example Code: Window Exit Handler Modification of EventsTest.java
// File EventsEx.java, Code listed in Handout section 12.2 …….. public class EventsEx implements MouseMotionListener , WindowListener, { JFrame f; JLabel coord; public void initGUI () { ……. // set layouts window.addMouseMotionListener(this); f.addWindowListener(this); ……. }//end initGUI // Event Handlers for MouseMotionListener public void mouseDragged(MouseEvent me) {…..} public void mouseMoved(MouseEvent) {….}

24 Example Code: Window Exit Handler Modification of EventsTest.java
// Event Handlers for WindowListener public void windowActivated (WindowEvent we) { } public void windowClosed (WindowEvent we) { } public void windowClosing (WindowEvent we) { JOptionPane.showMessageDialog(null, “Good Bye”); System.exit(0) } public void windowDeactivated (WindowEvent we) { } public void windowDeiconified (WindowEvent we) { } public void windowIconified (WindowEvent we) { } public void windowOpened (WindowEvent we) { }

25 Example Code: Window Exit Handler Modification of EventsTest.java
public static void main (String args[ ]){ EventsEx ex = new EventsEx(); } } // end of EventsEx class

26 Example Code: Window Exit Handler Modification of EventsTest
Example Code: Window Exit Handler Modification of EventsTest.java Output When user closes the window, Message would be displayed After pressing Ok button Program will exit


Download ppt "Web Design & Development Lecture 12"

Similar presentations


Ads by Google