1 / 20

Object-Oriented Programming Design Topic : Event Handling – GUI Part II

Object-Oriented Programming Design Topic : Event Handling – GUI Part II. Maj Joel Young Joel.Young@afit.edu. Maj Joel Young. Events. Operating system monitors environment for events such as keystrokes or mouse clicks OS reports these events to programs that are running

azizi
Download Presentation

Object-Oriented Programming Design Topic : Event Handling – GUI Part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming DesignTopic : Event Handling – GUI Part II Maj Joel Young Joel.Young@afit.edu Maj Joel Young

  2. 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. 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. 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. 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. 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!"); } } Separate Listener Class

  7. 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(); } } Anonymous Listener Class

  8. 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(); } } Add Listener Methods to Existing Class

  9. Event Inheritance Hierarchy Event Object AWT Event Action Event Adjustment Event Component Event Item Event Text Event Container Event Focus Event Input Event Paint Event Window Event Key Event Mouse Event Programmer can create custom events by extending EventObject or any other event class

  10. 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. 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. 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 AWT Event Handlers

  13. 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. 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. 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. 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. Action Interface • Facilitates implementation of multiple ways for invoking a command • Encapsulates characteristics of action command in a single interface • ActionTest.java

  18. 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. 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. Homework • Keep working on the Arithmetic Evaluator

More Related