28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Graphic User Interfaces Layout Managers Event Handling.
Event Handling.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Welcome to CIS 083 ! Events CIS 068.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Graphical User Interface Components: Part 1 Chapter 11.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Event Handling and Listeners in SWING The practice of event handling.
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI Programming in Java Hao Jiang Boston College April, 2009.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Welcome To java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
A Quick Java Swing Tutorial
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
GUI Programming III: Events
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Making Java GUIs Functional
Presentation transcript:

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 Maj Joel Young

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

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

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

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

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!"); }

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(); }

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(); }

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

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)

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

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

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.

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

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

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

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

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

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

Air Force Institute of Technology Electrical and Computer Engineering 28-Aug-1520 Object-Oriented Programming Design Homework Keep working on the Arithmetic Evaluator