Download presentation
Presentation is loading. Please wait.
1
Introduction to Java 2 Programming
Lecture 9 Java Swing API, Part 2
2
Overview Event Handling Basics Swing Events Examples
Mouse Events Actions Window Events Developing the Calculator
3
Event Handling Basics Event handling always has the following pattern
Involves 3 distinct roles Source object which is object of interest Generates events under certain conditions In Swing these are user activities Event object that describes what happened Encapsulates the event context Who did what, on which object, etc Listeners which receive events Receiving an event is a method call on the listener Different categories of event listeners
4
Event Handling Basics Listeners are registered with source objects
Ties them together Informs the source object about who to deliver events to This is a many-many relationship A source object may have multiple listeners A 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 events Has a bullet hit, have I seen another robot? There were different event objects BulletHitEvent, RobotScannedEvent The Robot could listen for events by implementing specific methods onBulletHit, onRobotScanned Robot base class handled registration for you And provided “do nothing” versions of the above methods
6
Event Handling Conventions
Listeners are described using interfaces Naming convention: XXXListener Where XXX denotes the category of listener E.g. WindowListener Creating a listener means implementing that interface Register a listener with the addXXXListener method on the source object Generally a removeXXXListener also Source object will be a Swing component, i.e. a sub-class of java.awt.Component The Component class provides the add/remove methods
7
Event Handling Conventions
Event object typically extend java.awt.Event Some of the ‘newer’ ones don’t Events share some common attributes a timestamp, source object, etc
8
Event Handling Conventions
9
Mouse Events Natural for Swing to expose mouse-related events
It’s how the user interacts with the GUI MouseListener interface describes the basic events Each method accepts a MouseEvent object parameter java.awt.Component has add/remove listener methods
10
Mouse Events
11
Mouse Events So, capturing basic mouse events involves: Example code…
Creating an implementation of MouseListener Calling addMouseListener on one or more Components to register it Example code…
12
Action Events Very tedious implementation if all activities were dealt with as individual clicks Swing provides higher level ‘action’ event Meaning of event depends on component E.g. button click, menu selection, etc Basic classes: ActionEvent – identifies action, key modifiers, etc ActionListener – single actionPerformed method addActionListener, removeActionListener methods on Component
13
Window Events Swing allows the capturing of window related events
Activate, deactivate, minimise, open, close etc setDefaultCloseOperation is only useful if you don’t want to do anything complex Basic classes/methods WindowEvent – identifies Window WindowListener – range of methods addWindowListener, removeWindowListener methods on JFrame, JWindow, JDialog
14
Other Swing Events More Mouse events Item events Key Events
Mouse motion, mouse wheels Item events Selecting/deselecting items in lists, checkboxes, etc Key Events Raw keyboard input Tree Events Opening/closing nodes in a tree component Drag 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 clicks Can do this on the ButtonPanel class Associate this with each button on the calculator
16
The Calculator Logic Check which button has been clicked
Use ActionEvent.getObject to get the button Then ask the button for its action command, getActionCommand Clicked a number button? Then update the JTextField to add digits Use getText and setText methods Clicked 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 operator And, reset the field (for the second number) Finally set a flag to indicate we’re in a calculation Clicked the equals button? Then take the first number and the current value of the JTextField, and ask a Calculator object to do the math Display the result And reset the calculation flag
18
The Calculator Logic The Calculator object can be very simple
Adapt our earlier implementation… Use double for extra precision
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.