GUI Programming using Java - 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 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Introduction to Java 2 Programming
TCU CoSc Programming with Java Handling Events.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming
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.
Welcome to CIS 083 ! Events CIS 068.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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 - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
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.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
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.
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.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
GUI Programming using Java - Key Events
Chapter 12 Event-Driven Programming
11 GUI Components: Part 1.
Web Design & Development Lecture 11
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Appendix I GUI Components and Event Handling
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
CSE 331 Software Design and Implementation
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Event-driven programming for GUI
MSIS670: Object-Oriented Software Engineering
CSE 331 Software Design and Implementation
IFS410: Advanced Analysis and Design
GUI Programming using Java - Mouse Events
Introduction to Event Handling
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

GUI Programming using Java - Event Handling Department of Computer and Information Science, School of Science, IUPUI GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

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

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

Outline TextFieldFrame .java (1 of 3) Create a new JTextField

Outline TextFieldFrame.java (2 of 3) Create a new JTextField Make this JTextField uneditable Create a new JPasswordField Create event handler Register event handler Create event handler class by implementing the ActionListener interface Declare actionPerformed method

Outline TextFieldFrame .java (3 of 3) Test if the source of the event is the first text field Outline Get text from text field TextFieldFrame .java (3 of 3) Test if the source of the event is the second text field Get text from text field Test if the source of the event is the third text field Get text from text field Test if the source of the event is the password field Get password from password field

Outline TextFieldTest .java (1 of 2)

Outline TextFieldTest .java (2 of 2)

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

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

Using a Nested Class to Implement an Event Handler JTextFields and JPasswordFields Pressing enter within either of these fields causes an ActionEvent Processed by objects that implement the ActionListener interface

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

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

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

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

Fig. 11.11 | Some event classes of package java.awt.event.

Fig. 11. 12 | Some common event-listener interfaces of package java Fig. 11.12 | Some common event-listener interfaces of package java.awt.event.

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?

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

Fig. 11.13 | Event registration for JTextField textField1 .

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 MouseEvents are handled by MouseListeners and MouseMotionsListeners KeyEvents are handled by KeyListeners

Acknowledgements Deitel, Java How to Program