Download presentation
Presentation is loading. Please wait.
Published byMoris Hunter Modified over 9 years ago
1
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II Maj Joel Young Joel.Young@afit.edu Maj Joel Young
2
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-152 Object-Oriented Programming Design Events Operating system monitors environment for events such as keystrokes or mouse clicks OS reports these events to programs that are running Each program decides what to do in response to these events Essential for graphical user interfaces where user controls program flow Event Source –The object that generates events –Or more precisely, the object that captures user generated events and notifies other objects when events occur –An event source registers listener objects that are notified when events occur –Examples -- buttons, windows, text boxes
3
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-153 Object-Oriented Programming Design Events Listener Object –An instance of a class that implements a special interface called a listener interface –Listener objects react to specified events –A listener object must be registered with an event source to be notified of events that are generated in that source Listener Interface –Describes the methods associated with a particular class of events –Implemented in a listener object that reacts to a specific class of events –Not all methods in the listener interface may be appropriate for a particular listener object
4
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-154 Object-Oriented Programming Design Listener Classes – More Help Adapter classes –Implements all methods of a listener interface but does nothing with them –A convenience for listener objects to implement a subset of the methods of a listener interface Anonymous inner class –Requires no name –Makes code more compact, but more difficult to understand at first blush –Some people recommend against use
5
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-155 Object-Oriented Programming Design Event Listeners There are three main ways to implement an event listener: – Option 1: Create a new class (separate or inner class) – Option 2: Create an anonymous class – Option 3: Add required methods to an existing class
6
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-156 Object-Oriented Programming Design Separate Listener Class public class TestFrame extends JFrame { public TestFrame() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener( new MyActionListener()); p.add(b); this.getContentPane().add(p); } public static void main(String[] args) { TestFrame testFrame = new TestFrame(); testFrame.setSize(200,200); testFrame.show(); } // The Listener class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane. showMessageDialog( null, "Button clicked!"); }
7
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-157 Object-Oriented Programming Design Anonymous Listener Class public class TestFrame2 extends JFrame { public TestFrame2() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); // Add anonymous listener b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e) { JOptionPane. showMessageDialog( null, "Button clicked!"); } ); p.add(b); this. getContentPane().add(p); } public static void main(String[] args) { TestFrame2 TestFrame2 = new TestFrame2(); TestFrame2.setSize( 200,200); TestFrame2.show(); }
8
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-158 Object-Oriented Programming Design Add Listener Methods to Existing Class public class TestFrame extends Jframe implements ActionListener { public TestFrame() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); // Add a listener b.addActionListener(this); p.add(b); this.getContentPane(). add(p); } public void actionPerformed(ActionEvent e) { JOptionPane. showMessageDialog( null, "Button clicked!"); } public static void main( String[] args) { TestFrame testFrame = new TestFrame(); testFrame.setSize(200,200); testFrame.show(); }
9
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-159 Object-Oriented Programming Design Event Inheritance Hierarchy Event Object Window Event Container Event Input Event Focus Event Paint Event Key Event Mouse Event AWT Event Adjustment Event Component Event Item Event Text Event Action Event Programmer can create custom events by extending EventObject or any other event class
10
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1510 Object-Oriented Programming Design Types of Events EventObject –Encapsulates the event –This is what is passed from event source object to listener objects via the listener interface –Accessor methods allow listener objects to analyze the event objects Semantic Events – associated with user manipulation of GUI –ActionEvent (button click, menu selection, double click on list box item, clicking [ENTER] in a text field) –AdjustmentEvent (user adjusts scroll bar) –ItemEvent( user makes a selection from a set of checkbox or list items) –TextEvent (contents of text box or text area are changed) Low-Level Events – associated with GUI objects –ComponentEvent (component resized, moved, shown, or hidden); base class for all low-level events –KeyEvent (key pressed or released) –MouseEvent (mouse button depressed, released, moved, or dragged) –FocusEvent (component got focus, lost focus) –WindowEvent (window activated, deactivated, iconified, deiconified, or closed) –ContainerEvent (component has been added or removed)
11
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1511 Object-Oriented Programming Design Event Classes Other event classes –PaintEvent –For convenience of AWT implementors –Not intended for programming –ContainerEvent –When a component is added or removed –Makes user interface generators easier to program –Only necessary for dynamically changing user interface Component -- user interface object such as a button, text field, or scroll bar Container -- a screen area or component that can contain components, for example a window or an applet
12
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1512 Object-Oriented Programming Design AWT Event Handlers Listener Interfaces –ActionListener –AdjustmentListener –ComponentListener –ContainerListener –FocusListener –ItemListener –KeyListener –MouseListener –MouseMotionListener –TextListener –WindowListener Listener Adaptor Classes –ComponentAdapter –ContainerAdapter –FocusAdapter –KeyAdapter –MouseAdapter –MouseMotionAdapter –WindowAdapter Swing User Interface components have additional listeners
13
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1513 Object-Oriented Programming Design Focus events A component has the focus if it can receive keystrokes Only one component can have the focus at a time A component can lose focus if the user selects another component, which then gains focus A component gains focus if the user clicks the mouse inside it Usually a visual cue that a component is selected FocusListener interface methods –focusGained –focusLost FocusEvent class methods –getComponent -- returns component that lost or gained the focus –isTemporary -- returns true if the focus change was temporary (e.g. user selects a different window; when current window becomes active again, same component has the focus) Uses –Error checking -- when user done editing a field and moves to another field, trap the lost focus event to check the value of the field –User developed components -- trap the focus events to redraw the component with an "active" or "inactive" look.
14
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1514 Object-Oriented Programming Design Window events A window (frame) is active if it can currently receive keystrokes from the operating system; –Usually indicated by highlighted title bar –Only one window active at a time Types of window events –Window has opened –Window has closed –Window becomes active –Window becomes inactive –Window becomes iconified –Window becomes deiconified –User wants to close window Uses –CloseableFrame –Stop animation when iconified or inactive
15
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1515 Object-Oriented Programming Design Keyboard events Types of events –keyPressed -- virtual key codes –keyReleased -- virtual key codes –keyTyped -- characters Virtual key codes -- actual key on the keyboard, e.g. VK_A, VK_SHIFT For hold down keys –isShiftDown –isControlDown –isAltDown –isMetaDown Example -- Sketch.java
16
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1516 Object-Oriented Programming Design Mouse events Not for buttons or menus, these are handled through action events Event accessor methods –getX –getY –getClickCount MouseListener interface –mouseClicked –And others –Separated from motion to avoid overhead of frequent motion events where motion is not important MouseMotionListener interface –mouseMoved –mouseDragged Example -- MouseTest.java
17
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1517 Object-Oriented Programming Design Action Interface Facilitates implementation of multiple ways for invoking a command Encapsulates characteristics of action command in a single interface ActionTest.java
18
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1518 Object-Oriented Programming Design Multicasting Send the same event to more than one listener object Implementation –Send event to all objects registered as listeners for the event –This is default behavior; the key is to make sure you're application registers all potential listeners Example -- MulticastTest.java
19
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1519 Object-Oriented Programming Design Advanced event handling Consuming events –Prevents other listeners from receiving an event –evt.consume(); Manipulating the event queue –Inserting a new event into the event queue -- evtq.postEvent() –Enables custom event handling –Removing an event from the event queue -- getNextEvent() –Modal processing (e.g. create line segment by clicking two endpoints -- not interested in any events other than mouse clicks) –Secondary event loop removes events from event queue and discards those that are not relevant to the current mode –Example -- EventQueueTest.java
20
Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1520 Object-Oriented Programming Design Homework Keep working on the Arithmetic Evaluator
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.