09-Event-Handling

09-Event-Handling - 2006 Marty Hall Handling Mouse and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
3 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall Handling Mouse and Keyboard Events 4 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 J2EE training: http://courses.coreservlets.com Agenda General event-handling strategy Handling events with separate listeners Handling events by implementing interfaces Handling events with named inner classes Handling events with anonymous inner classes The standard AWT listener types Subtleties with mouse events Examples 6 J2EE training: http://courses.coreservlets.com General Strategy Determine what type of listener is of interest 11 standard AWT listener types, described on later slide. ActionListener, AdjustmentListener, ComponentListener, ContainerListener, FocusListener, ItemListener, KeyListener, MouseListener, MouseMotionListener, TextListener, WindowListener Define a class of that type Implement interface (KeyListener, MouseListener, etc.) Extend class (KeyAdapter, MouseAdapter, etc.) Register an object of your listener class with the window w.add Xxx Listener(new MyListenerClass()); E.g., addKeyListener, addMouseListener
Background image of page 2
7 J2EE training: http://courses.coreservlets.com Handling Events with a Separate Listener: Simple Case Listener does not need to call any methods of the window to which it is attached import java.applet.Applet; import java.awt.*; public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } } 8 J2EE training: http://courses.coreservlets.com Separate Listener: Simple Case (Continued) import java.awt.event.*; public class ClickListener extends MouseAdapter { public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed at (" + event.getX() + "," + event.getY() + ")."); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9 J2EE training: http://courses.coreservlets.com Generalizing Simple Case What if ClickListener wants to draw a circle wherever mouse is clicked? Why can’t it just call getGraphics to get a Graphics object with which to draw? General solution: Call event.getSource to obtain a reference to window or GUI component from which event originated Cast result to type of interest Call methods on that reference 10 J2EE training: http://courses.coreservlets.com Handling Events with Separate Listener: General Case import java.applet.Applet; import java.awt.*; public class CircleDrawer1 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } }
Background image of page 4
11 J2EE training: http://courses.coreservlets.com
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

09-Event-Handling - 2006 Marty Hall Handling Mouse and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online