Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.

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.
Introduction to Java 2 Programming
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
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.
Event-Driven Programming
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.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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,
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
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.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
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.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Introduction to Java Programming
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Javax.swing Class JOptionPane Java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JOptionPane.
Graphical User Interface Components: Part 1 Chapter 11.
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,
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Dale Roberts GUI Programming using Java - Windowing Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
© 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
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
GUI Programming using Java - Key Events
11 GUI Components: Part 1.
Web Design & Development Lecture 11
Appendix I GUI Components and Event Handling
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Event-driven programming for GUI
GUI Programming using Java - Mouse Events
Introduction to Event Handling
Presentation transcript:

Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts 2 Text Fields and an Introduction to Event Handling with Nested Classes GUIs are event-driven A user interaction creates an event Common events are clicking a button, typing in a text field, selecting an item from a menu, closing and window and moving the mouse The event causes a call to a method called an event handler

Dale Roberts 3 Text Fields and an Introduction to Event Handling with Nested Classes Class JTextComponent Superclass of JTextField Superclass of JPasswordField Adds echo character to hide text input in component Allows user to enter text in the component when component has the application’s focus

Dale Roberts 4Outline TextFie ldFram e.java (1 of 3) Create a new JTextField

Dale Roberts 5Outline TextFieldFr ame.java (2 of 3) Create a new JTextField Make this JTextField uneditable Create a new JPasswordField Create event handlerRegister event handler Create event handler class by implementing the ActionListener interface Declare actionPerformed method

Dale Roberts 6Outline TextFieldFr ame.java (3 of 3) Test if the source of the event is the first text field Get text from text field Get password from password field Test if the source of the event is the second text field Test if the source of the event is the third text field Test if the source of the event is the password field

Dale Roberts 7Outline TextFie ldTest.java (1 of 2)

Dale Roberts 8Outline TextFie ldTest.java (2 of 2)

Dale Roberts 9 Steps Required to Set Up Event Handling for a GUI Component Several coding steps are required for an application to respond to events Create a class for the event handler Implement an appropriate event-listener interface Register the event handler

Dale Roberts 10 Using a Nested Class to Implement an Event Handler Top-level classes Not declared within another class Nested classes Declared within another class Non- static nested classes are called inner classes Frequently used for event handling

Dale Roberts 11 Using a Nested Class to Implement an Event Handler JTextField s and JPasswordField s Pressing enter within either of these fields causes an ActionEvent Processed by objects that implement the ActionListener interface

Dale Roberts 12 Registering the Event Handler for Each Text Field Registering an event handler Call method addActionListener to register an ActionListener object ActionListener listens for events on the object

Dale Roberts 13 Details of Class TextFieldHandler ’s actionPerformed Method Event source Component from which event originates Can be determined using method getSource Text from a JTextField can be acquired using getActionCommand Text from a JPasswordField can be acquired using getPassword

Dale Roberts 14 Common GUI Event Types and Listener Interfaces Event types All are subclasses of AWTEvent Some declared in package java.awt.event Those specific to Swing components declared in javax.swing.event

Dale Roberts 15 Common GUI Event Types and Listener Interfaces Delegation event model Event source is the component with which user interacts Event object is created and contains information about the event that happened Event listener is notified when an event happens

Dale Roberts 16 Fig | Some event classes of package java.awt.event.

Dale Roberts 17 Fig | Some common event-listener interfaces of package java.awt.event.

Dale Roberts 18 How Event Handling Works Remaining questions How did the event handler get registered? How does the GUI component know to call actionPerformed rather than some other event- handling method?

Dale Roberts 19 Registering Events Every JComponent has instance variable listenerList Object of type EventListenerList Maintains references to all its registered listeners

Dale Roberts 20 Fig | Event registration for JTextField textField1.

Dale Roberts 21 Event-Handler Invocation Events are dispatched to only the event listeners that match the event type Events have a unique event ID specifying the event type MouseEvent s are handled by MouseListener s and MouseMotionsListener s KeyEvent s are handled by KeyListener s

Dale Roberts Acknowledgements Deitel, Java How to Program