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

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Algorithmic Programming II
Java Graphical User Interface (GUI) using Visual Editor in eclipse CSI 1390 – Java Programming Instructor: Saeid Nourian University.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 19 Graphics User Interfaces (GUIs)
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.
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.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Event Handling CSIS 3701: Advanced Object Oriented 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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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
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.
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.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
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.
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.
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.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
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-driven programming for GUI
Introduction to Event Handling
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, mouse 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  The buttons and text fields don’t execute code — Instead JButton and JTextField objects send actionPerformed messages to other objects that have been registered to “listen”  We write the code we want to execute in actionPerformed methods — This requires a class that implements an interface, for example — Registering an instance of that class to listen

5 But no one is "Listening"  Wanted: An object to listen to the button that understands a message such as — actionPerformed  Also need to tell the button who it can send the actionPerformed message to  Register the listener with this method addActionListener(ActionListener al)

6 Handling Events Add a private inner class that can listen to the event that the graphical component will generate Your class must implement a listener interface to guarantee that it has the expected methods: First up: ActionListener Register the listener object to the GUI component (JButton JTextField) so it can later send actionPerformed messages to that listener when the use clicks the button or presses enter. events occur anytime in the future--the listener is listening (waiting for user generated events such as clicking a button or entering text into a text field)

7 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) — You need an ActionListener object There is no ActionListener class! What can we do?????

8 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. The Button does not care what happened

9 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

10 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 // 5. 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

11 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

12 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

13 Listen to JTextField  Add to the current GUI — Have the button click toggle the JTextField form upper to lower case Need methods getText and setText

14 JList and DefaultListModel  Code demo: — Show a JList GUI component with a DefaultListModel set as the model will contain a list of strings  Respond to selections by showing the selected string in a dialog box

15 public class ShowJListListModel extends JFrame { public static void main(String[] args) { JFrame window = new ShowJListListModel(); window.setVisible(true); } private JList guiList; private DefaultListModel allStrings; public ShowJListListModel() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250, 300); setLocation(100, 100); setLayout(null); initializeAllStrings(); guiList = new JList(allStrings); guiList.setSelectedIndex(allStrings.getSize() - 1); guiList.setSize(150, 250); guiList.setLocation(20, 10); guiList.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 14)); add(guiList); registerListeners(); } private void registerListeners() { ListSelectionListener listener = new GuiListListListener(); guiList.addListSelectionListener(listener); } private class GuiListListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent arg0) { String str = allStrings.get(guiList.getSelectedIndex()).toString(); System.out.println(str + " selected"); } private void initializeAllStrings() { allStrings = new DefaultListModel(); allStrings.addElement("Riley"); allStrings.addElement("Dakota"); allStrings.addElement("Casey"); allStrings.addElement("Angel"); allStrings.addElement("Reese"); }