3 Event Handling Basics Event handling always has the following pattern Involves 3 distinct rolesSource object which is object of interestGenerates events under certain conditionsIn Swing these are user activitiesEvent object that describes what happenedEncapsulates the event contextWho did what, on which object, etcListeners which receive eventsReceiving an event is a method call on the listenerDifferent categories of event listeners
4 Event Handling Basics Listeners are registered with source objects Ties them togetherInforms the source object about who to deliver events toThis is a many-many relationshipA source object may have multiple listenersA listener may listen to multiple source objects
5 Event Handling in Robocode Think back to the Robocode examples…The Arena was the source of interesting eventsHas a bullet hit, have I seen another robot?There were different event objectsBulletHitEvent, RobotScannedEventThe Robot could listen for events by implementing specific methodsonBulletHit, onRobotScannedRobot base class handled registration for youAnd provided “do nothing” versions of the above methods
6 Event Handling Conventions Listeners are described using interfacesNaming convention: XXXListenerWhere XXX denotes the category of listener E.g. WindowListenerCreating a listener means implementing that interfaceRegister a listener with the addXXXListener method on the source objectGenerally a removeXXXListener alsoSource object will be a Swing component, i.e. a sub-class of java.awt.ComponentThe Component class provides the add/remove methods
7 Event Handling Conventions Event object typically extend java.awt.EventSome of the ‘newer’ ones don’tEvents share some common attributesa timestamp, source object, etc
9 Mouse Events Natural for Swing to expose mouse-related events It’s how the user interacts with the GUIMouseListener interface describes the basic eventsEach method accepts a MouseEvent object parameterjava.awt.Component has add/remove listener methods
11 Mouse Events So, capturing basic mouse events involves: Example code… Creating an implementation of MouseListenerCalling addMouseListener on one or more Components to register itExample code…
12 Action EventsVery tedious implementation if all activities were dealt with as individual clicksSwing provides higher level ‘action’ eventMeaning of event depends on componentE.g. button click, menu selection, etcBasic classes:ActionEvent – identifies action, key modifiers, etcActionListener – single actionPerformed methodaddActionListener, removeActionListener methods on Component
13 Window Events Swing allows the capturing of window related events Activate, deactivate, minimise, open, close etcsetDefaultCloseOperation is only useful if you don’t want to do anything complexBasic classes/methodsWindowEvent – identifies WindowWindowListener – range of methodsaddWindowListener, removeWindowListener methods on JFrame, JWindow, JDialog
14 Other Swing Events More Mouse events Item events Key Events Mouse motion, mouse wheelsItem eventsSelecting/deselecting items in lists, checkboxes, etcKey EventsRaw keyboard inputTree EventsOpening/closing nodes in a tree componentDrag and drop…and many more. See javax.swing.event and java.awt.event packages.
15 Developing the Calculator To add functionality to the Calculator GUI we need to:Implement the ActionListener interface to respond to button clicksCan do this on the ButtonPanel classAssociate this with each button on the calculator
16 The Calculator Logic Check which button has been clicked Use ActionEvent.getObject to get the buttonThen ask the button for its action command, getActionCommandClicked a number button?Then update the JTextField to add digitsUse getText and setText methodsClicked the point button?Then add a decimal point, but only once!
17 The Calculator Logic Clicked an operator (+, -, etc)? Then store current value of JTextField (first number)And, remember which operatorAnd, reset the field (for the second number)Finally set a flag to indicate we’re in a calculationClicked the equals button?Then take the first number and the current value of the JTextField, and ask a Calculator object to do the mathDisplay the resultAnd reset the calculation flag
18 The Calculator Logic The Calculator object can be very simple Adapt our earlier implementation…Use double for extra precision