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,

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

Algorithmic Programming II
Java Graphical User Interface (GUI) using Visual Editor in eclipse CSI 1390 – Java Programming Instructor: Saeid Nourian University.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
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. 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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
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.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
UID – Event Handling and Listeners Boriana Koleva
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.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
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.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 4 Interface Types and Polymorphism Part 2.
GUI Programming using Java - Event Handling
Modular Event Handling
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Chapter 4 Interface Types and Polymorphism Part 2
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Introduction to Event Handling
Event loops 17-Jan-19.
Constructors, GUI’s(Using Swing) and ActionListner
Tonga Institute of Higher Education
Presentation transcript:

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, move the mouse, press a key — And NOTHING happens  So let’s make something happen…

3 Java's Event Model  Java lets the operating system notify graphical components of user interaction — JButton objects know when a user clicks it — JTextField objects with focus know when the user presses the enter (return) key — Event driven programs respond to many things mouse clicks and movements clicks on hyperlinks or menu items Users pressing any key, selecting a list item, moving a slider bar, checking a radio button, …

4 Example: Action Events  Buttons and text fields don’t execute code — Instead JButton and JTextField objects send actionPerformed messages to their list of “listeners”  We write the code we want to execute in actionPerformed methods — Write a class that implements an interface — Register an instance of this class to listen

5 ActionEvent / ActionListener  When a JButton object is clicked, it constructs an ActionEvent object and sends it to the actionPerformed method of its listeners — We can usually ignore that parameter, other times we can't  To register a listener to a JButton, send an addActionListener message to button public void addActionListener(ActionListener al) — We need an ActionListener object — There is no ActionListener class! — What can we do?????

6 Implement an interface Then your object can be treated as if it were an ActionListener Polymorphism in action: We can have any number of actionPerformed methods that do whatever they are supposed to.

7 Inner class  Add an inner class — inner classes have access to the enclosing classes' instance variables  Make it private since no one else needs to know about it  Java added inner classes for the very purpose: to have listener objects respond to user interactions with GUI components

8 And register the listener with addActionListener // 6. Register the instance of the listener so the // component can later send messages to that object ButtonListener aListener = new ButtonListener(); clickMeButton.addActionListener(aListener); } // End constructor // inner class to listen to events private class ButtonListener implements ActionListener { // No constructor needed here. // Must have this method to implement ActionListener public void actionPerformed(ActionEvent anActionEvent) { System.out.println("Button was clicked."); } Caution: this is easy to forget. It is an error no one will tell you about

9 Another benefit of interfaces  Can have many ActionListener objects — Any class that implements ActionListener — may need a different class for every button and text field in the GUI But they all can be treated as ActionListener objects They can be passed as arguments to this method public void addActionListener(ActionListener aL) Adds an action listener to receive action events from JButtons, TextFields,... aL - an instance of a class that implements the ActionListener interface

10 Assignment Compatible  Can pass instances of classes implementing an interface to the interface type parameter addActionListener(ActionListener anyListener) addActionListener(new ButtonListener()); addActionListener(new TextFieldListener());  ButtonListener and TextFieldListener must implement interface ActionListener

11 Listen to a JTextField  Add to the current GUI — Have the button click toggle the JTextField form upper to lower case — Need methods getText and setText See ShowListener.java