Documente Academic
Documente Profesional
Documente Cultură
Developed and taught by well-known author and developer. At public venues or onsite at your location.
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 Customized Java EE Training: http://courses.coreservlets.com/ Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. organization. Contact hall@coreservlets.com for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
The standard AWT listener types Subtleties with mouse events Examples
5
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
Developed and taught by well-known author and developer. At public venues or onsite at your location.
10
11
13
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Circle class
public class Circle implements Shape { public double getArea() { some real code } }
Note
You can implement many interfaces
public class MyClass implements Foo, Bar, Baz { }
15
public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } }
19
Interfaces
Compile time error
Adapters
No compile time error, but nothing happens at run time when you press the mouse
@Override Example
public class CircleDrawer1 extends Applet { @Override public void init() { setForeground(Color.BLUE); addMouseListener(new CircleListener()); } } public class CircleListener extends MouseAdapter { private int radius = 25; @Override public void mousePressed(MouseEvent event) { Applet app = (Applet)event.getSource(); Graphics g = app.getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } }
21
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Example
23
24
Summary of Approaches
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
27
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Disadvantage
Need extra step to call methods in main window
Disadvantage
Must implement methods you might not care about
28
Disadvantage
A bit harder to understand
Disadvantage
Much harder to understand
29
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Registration Method
addActionListener addAdjustmentListener addComponentListener addContainerListener addFocusListener addItemListener addKeyListener addMouseListener addMouseMotionListener addTextListener addWindowListener
31
AdjustmentListener
Applies to scrolling
adjustmentValueChanged(AdjustmentEvent event)
ComponentListener
Handles moving/resizing/hiding GUI objects
32
FocusListener
Detects when controls get/lose keyboard focus
focusGained(FocusEvent event) focusLost(FocusEvent event)
33
KeyListener
Detects keyboard events
keyPressed(KeyEvent event) -- any key pressed down keyReleased(KeyEvent event) -- any key released keyTyped(KeyEvent event) -- key for printable char released
34
mouseEntered(MouseEvent event) mouseExited(MouseEvent event) mousePressed(MouseEvent event) mouseReleased(MouseEvent event) mouseClicked(MouseEvent event)
Release without drag. Do not use this for mousePressed! Applies on release if no movement since press
MouseMotionListener
Handles mouse movement
MouseInputListener
In javax.swing.event package, not java.awt.event You have to call both addMouseListener and addMouseMotionListener, so it does not save much
35
WindowListener
Handles high-level window events
windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated
windowClosing particularly useful
36
37
38
39
40
41
Whiteboard (Continued)
... private class CharDrawer extends KeyAdapter { // When user types a printable character, // draw it and shift position rightwards. public void keyTyped(KeyEvent event) { String s = String.valueOf(event.getKeyChar()); getGraphics().drawString(s, lastX, lastY); record(lastX + fm.stringWidth(s), lastY); } } }
42
Whiteboard (Results)
43
45
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
46
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
General strategy
Determine what type of listener is of interest
Check table of standard types
Understanding listeners
Methods give specific behavior.
Arguments to methods are of type XxxEvent
Methods in MouseEvent of particular interest
47
Questions?
JSF 2, PrimeFaces, Java 7, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training.
Developed and taught by well-known author and developer. At public venues or onsite at your location.