Introduction to Event Handling

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.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
Io package as Java’s basic I/O system continue’d.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Javax.swing Class JOptionPane Java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JOptionPane.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Java GUI with Swing (Part I) Java – How to Program By Deitel & Deitel and iswing/index.html
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
1 Chapter 13 – More GUI Components Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
GUIs & Event-Driven Programming Chapter 11 Review.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
Modular Event Handling
GUIs and Events Rick Mercer.
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
JButton Chapter 11 Lecture 3.
Introduction to Graphics
Web Design & Development Lecture 11
Appendix I GUI Components and Event Handling
Graphical User Interface (pronounced "gooey")
Programming in Java Event Handling
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Event-driven programming for GUI
MSIS670: Object-Oriented Software Engineering
IFS410: Advanced Analysis and Design
COP 4610L: Applications in the Enterprise Spring 2005
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 5 Processing Input with Applets
Presentation transcript:

Introduction to Event Handling

registerObserver(){…} 《interface》 Subject registerObserver() removeObserver() notifyObserver() 《interface》 Observer update() ConcreteSubject registerObserver(){…} removeObserver(){…} notifyObserver(){…} getState() setState() ConcreteObserver update(){…} // other methods

Java Event Handling Example Name in Design Pattern Actual Name in JButton Event Handling Subject JButton Observer ActionListener ConcreteObserver The class that implements ActionListener interface Attach() addActionListener Notify() actionPerformed

Basic concepts Event Event source Event handler (listener) When users interact with a GUI component, the interaction is called event Events drive the program to perform a task Event source the GUI component on which the event occurs Event handler (listener) The code that performs a task in response to an event Event set up The process of creating event handler class/object and registering the handler with event source Event handling The overall process of setting up event handling and responding to events

Event handling process Event set up Programmer write code to implement the event handler and register the event handler with a GUI component Event handling Java VM and GUI component works together responding to events

Set up Event Handling Create an event handler (listener) class The event handler class implements an appropriate event-listener interface. 2. Create an object of the above event handler class 3. Registering the event handler object with the event source (GUI component) i.e., when event occurs, a registered object of the event handler class will be notified.

(plainJButton=new JButton();) Event handler Interface (ActionListener) GUI object (plainJButton=new JButton();) Event Type (ActionEvent) Event handler class Class ButtonHandler implements ActionListener{ …. Public void actionPerformed(…){ } listenerList Event handler object Add handler object to the event listener list of GUI object handler = new ButtonHandler(); plainJButton.addActionListener (handler);

Event Handling (delegation event model) When an event occurs, Java VM sent event object to GUI component The event object contains - event source, event type, and event id, etc 3. When GUI component receives event object Identify the registered handler based on event type Identify the specific method for the registered handler based on event id Call the specific method to handle the event

Event Types & Listener Interfaces Many different types of events They are specified in java.awt.event Event types specific to Swing are specified in javax.swing.event For each event type, there is one or more corresponding event-listener interface For each listener interface, there is one or more event handling methods.

Action Event and Action Listener You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. - clicks a button, - chooses a menu item, - presses Enter in a text field. When an action event occurs, JMV sends an ActionEvent class object to event source.

ActionEvent Class String getActionCommand() Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommand that lets you set this string. int getModifiers() Returns an integer representing the modifier keys the user was pressing when the action event occurred. You can use the ActionEvent-defined constants SHIFT_MASK, CTRL_MASK, META_MASK, and ALT_MASK to determine which keys were pressed. For example, if the user Shift-selects a menu item, then the following expression is nonzero: actionEvent.getModifiers() & ActionEvent.SHIFT_MASK Object getSource() (in java.util.EventObject) Returns the object that fires the event.

ActionListener Interface public void actionPerformed(ActionEvent e) { ... //code that reacts to the action... }

Write an Action Listener: Declare an event handler class class either implements an ActionListener interface or extends a class that implements an ActionListener interface. public class MyClass implements ActionListener { … … public void actionPerformed(ActionEvent e) { ... //code that reacts to the action... } Register an instance of the event handler class on one or more components. someComponent.addActionListener(instanceOfMyClass);

Event handling with Nested Classes

// Fig. 11.9: TextFieldFrame.java // Demonstrating the JTextField class. import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JOptionPane; public class TextFieldFrame extends JFrame { private JTextField textField1; // text field with set size private JTextField textField2; // text field constructed with text private JTextField textField3; // text field with text and size private JPasswordField passwordField; // password field with text

// TextFieldFrame constructor adds JTextFields to JFrame public TextFieldFrame() { super( "Testing JTextField and JPasswordField" ); setLayout( new FlowLayout() ); // set frame layout // construct textfield with 10 columns textField1 = new JTextField( 10 ); add( textField1 ); // add textField1 to JFrame // construct textfield with default text textField2 = new JTextField( "Enter text here" ); add( textField2 ); // add textField2 to JFrame // construct textfield with default text and 21 columns textField3 = new JTextField( "Uneditable text field", 21 ); textField3.setEditable( false ); // disable editing add( textField3 ); // add textField3 to JFrame // construct passwordfield with default text passwordField = new JPasswordField( "Hidden text" ); add(passwordField ); // add passwordField to JFrame

// create and register event handlers TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener( handler ); textField2.addActionListener( handler ); textField3.addActionListener( handler ); passwordField.addActionListener( handler ); } // end TextFieldFrame constructor

// private inner class for event handling private class TextFieldHandler implements ActionListener { // process textfield events public void actionPerformed( ActionEvent event ) String string = ""; // declare string to display // user pressed Enter in JTextField textField1 if ( event.getSource() == textField1 ) string = String.format( "textField1: %s", event.getActionCommand() ); // user pressed Enter in JTextField textField2 else if ( event.getSource() == textField2 ) string = String.format( "textField2: %s", // user pressed Enter in JTextField textField3 else if ( event.getSource() == textField3 ) string = String.format( "textField3: %s", // user pressed Enter in JTextField passwordField else if ( event.getSource() == passwordField ) string = String.format( "passwordField: %s", new String( passwordField.getPassword() ) ); // display JTextField content JOptionPane.showMessageDialog( null, string ); } // end method actionPerformed } // end private inner class TextFieldHandler

} // end class TextFieldFrame // Fig. 11.10: TextFieldTest.java // Testing TextFieldFrame. import javax.swing.JFrame; public class TextFieldTest { public static void main( String args[] ) TextFieldFrame textFieldFrame = new TextFieldFrame(); textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); textFieldFrame.setSize( 325, 100 ); // set frame size textFieldFrame.setVisible( true ); // display frame } // end main } // end class TextFieldTest

Event handling with Anonymous Inner Class

public class ButtonFrame extends JFrame { private JButton plainJButton; // button with just text // ButtonFrame adds JButtons to JFrame public ButtonFrame() super( "Testing Buttons" ); setLayout( new FlowLayout() ); // set frame layout plainJButton = new JButton( "Plain Button" ); // button with text add( plainJButton ); // add plainJButton to JFrame // create new ButtonHandler for button event handling ButtonHandler handler = new ButtonHandler(); plainJButton.addActionListener( handler ); } // end ButtonFrame constructor // inner class for button event handling private class ButtonHandler implements ActionListener // handle button event public void actionPerformed( ActionEvent event ) JOptionPane.showMessageDialog( ButtonFrame.this, String.format( "You pressed: %s", event.getActionCommand() ) ); } // end method actionPerformed } // end private inner class ButtonHandler } // end class ButtonFrame

public class ButtonFrame extends JFrame { private JButton plainJButton; // button with just text // ButtonFrame adds JButtons to JFrame public ButtonFrame() super( "Testing Buttons" ); setLayout( new FlowLayout() ); // set frame layout plainJButton = new JButton( "Plain Button" ); // button with text add( plainJButton ); // add plainJButton to JFrame //Use anonymous inner class plainJButton.addActionListener( new ActionListener () // anonymous inner class // handle button event public void actionPerformed( ActionEvent event ) JOptionPane.showMessageDialog( ButtonFrame.this, String.format("You pressed: %s", event.getActionCommand() ) ); } // end method actionPerformed } // end of anonymous inner class ); // end of add } // end ButtonFrame constructor } // end class ButtonFrame