GUI Programming III: Events

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

Graphic User Interfaces Layout Managers Event Handling.
Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Welcome to CIS 083 ! Events CIS 068.
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 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.
Graphical User Interface Components: Part 1 Chapter 11.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
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.
COSC 3461: Module 2 Control Flow Paradigms: Reacting to the User.
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.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
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.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Creating User Interfaces Event-Driven Programming.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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.
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.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Web Design & Development Lecture 11
Advanced User Interfaces
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Programming Graphical User Interface (GUI)
Making Java GUIs Functional
Presentation transcript:

GUI Programming III: Events 24 GUI Programming III: Events

Header with column labels Previously Hierarchy Overview Swing Components Swing Components Would you like to save the data? Question! Each cell displays a data item Each column displays one type of data Header with column labels Application title

Overview Delegation Event Model Event Object Event Listener Event Control Flow Registration Event Listener Interfaces Listener Methods Process Action Event Example

Delegation Event Model Who started the action is not who execute it The information is transmitted using an event object The model consisted of the following actors User that start the action GUI component generate an object, e.g. JButton Listener Is notified of the request; receive an event object Execute request; contains the business logic, e.g. Save the data

Event Object Created when an event occurs, e.g. user interacts with a GUI component Composed of all necessary information about the When the event has occurred Type of event that has occurred, e.g. keyboard events, mouse event, ... Source of the event, e.g. JButton Of type Event class from java.awt package

Event Listener Need to implement the corresponding event listener interface, e.g. ActionListener Need to register with the source object Wait until an valid event is fired Notified of an event of the type of interest The corresponding method in the implemented interface is called with the generated event Processes the received event

Event Control Flow Event Source Event Listener Register Fire event Process event

Registration Call following method on the source object void add<Type>Listener(<Type>Listener listenerObj) <Type> refers to the type of event source Can be Key, Mouse, Focus, Component, Action and others One event source can register several listeners Registered listener can unregister by calling the following method on the source object void remove<Type>Listener(<Type>Listener listenerObj)

Event Classes EventObject class is part of the java.util package Subclasses follow the naming convention: <Type>Event Base one already defined in package java.awt.event

Event Classes ComponentEvent - Created when the source is moved, resized, made visible or hidden ActionEvent - Created when the source is moved, resized, made visible or hidden ItemEvent - Created when the source is pressed, double clicked or selected, i.e. A button KeyEvent - Created when an item is selected or deselected, e.g. in a list or check box MouseEvent - Created when the mouse is pressed, released, clicked (press and release), on tope or leaving the top of the source WindowEvent - Created when the source (a window) is open, close, activated, deactivated, iconified, ... Just some of the events

Event Listener Interfaces ActionListener must be implement to receive action events (ActionEvent) MouseListener must be implement to receive mouse events (MouseEvent) MouseMotionListener must be implement to receive mouse motion events that include dragging WindowListener must be implement to receive window events (WindowEvent) Defined in package java.awt.event

Listener Methods ActionListener void actionPerformed(ActionEvent evt) Invoked when an action occurs MouseMotionListener void mouseDragged(MouseEvent evt) Invoked when a mouse button is pressed on a component and then dragged void mouseMoved(MouseEvent evt) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed

Listener Methods MuoseListener void mouseClicked(MouseEvent evt) Invoked when the mouse button has been clicked (pressed and released) on a component void mouseEntered(MouseEvent evt) Invoked when the mouse enters a component void mouseExited(MouseEvent evt) Invoked when the mouse exits a component continue ...

Listener Methods MuoseListener (continue) void mousePressed(MouseEvent evt) Invoked when a mouse button has been pressed on a component  void mouseReleased(MouseEvent evt) Invoked when a mouse button has been released on a component. WindowListener  void windowClose(WindowEvent evt) Invoked when a window has been closed as the result of calling dispose on the window. Many more methods.

Process Create Event Listener class; class which implements the appropriate listener interface Implement all methods of the appropriate listener interface Describe in each method how you would like the event to be handled May give empty implementations for methods you don't need

Process public class MyActionListener implements ActionListener { /** * Processes the action event from the "OK" JButton. * @param evt the fired action event. */ public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("OK")) { // TODO: add code for when button is pressed } } // actionPerformed() } // end class MyActionListener

Process Register the listener object with the event source The object is an instantiation of the listener class from previous step Use the add<Type>Listener method of the event source MyActionListener oListener = new MyActionListener(); JButton jb = new JButton("OK"); jb.setActionCommand("OK"); jb.addActionListener(oListener); // register the listener ...

super("Pressing Buttons"); // Default Layout Manager is FlowLayout /** * GUI application with two button than when clicked print a message into * the standard out device, i.e. console. */ public class ButtonsGUI extends JFrame implements ActionListener { * Builds the GUI for the application. public ButtonsGUI() { super("Pressing Buttons"); JPanel contentPane= new JPanel(); // Default Layout Manager is FlowLayout JButton jb1 = new JButton("Press me"); JButton jb2 = new JButton("Or press me"); jb1.setActionCommand("Press me"); jb1.addActionListener(this); contentPane.add(jb1); jb2.setActionCommand("Or press me"); jb2.addActionListener(this); contentPane.add(jb2); setContentPane(contentPane); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } // Constructor () * Processes clicking any of the buttons. * @param evt the event on the button. public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("Press me")) { System.out.println("'Press me' button was pressed"); } else if (strAction.equals("Or press me")) { System.out.println("'Or press me' button was pressed"); } } // actionPerformed() } // end class ButtonsGUI Action Event Example public ButtonsGUI() { super("Pressing Buttons"); // Default Layout Manager is FlowLayout JPanel contentPane= new JPanel(); // Build buttons JButton jb1 = new JButton("Press me"); JButton jb2 = new JButton("Or press me"); jb1.setActionCommand("Press me"); jb1.addActionListener(this); contentPane.add(jb1); jb2.setActionCommand("Or press me"); jb2.addActionListener(this); contentPane.add(jb2); setContentPane(contentPane); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } // Constructor () public void actionPerformed(ActionEvent evt) { String strAction = evt.getActionCommand(); if (strAction.equals("Press me")) { System.out.println("'Press me' button was pressed"); } else if (strAction.equals("Or press me")) { System.out.println("'Or press me' button was pressed"); } } // actionPerformed() Amadeo Ascó, Adam Moore

Action Event Example After pressing both buttons; ‘Press me’ first and ‘Or press me’ the output is as bellow 'Press me' button was pressed 'Or press me' button was pressed