Event-driven programming for GUI

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.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
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
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
Java 212: Interfaces Intro to UML Diagrams. UML Class Diagram: class Rectangle +/- refers to visibility Color coding is not part of the UML diagram.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
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.
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.
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.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
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.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
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.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
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.
Event Handling and Listeners in SWING The practice of event handling.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Object-Orientated Analysis, Design and Programming
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Web Design & Development Lecture 11
A First Look at GUI Applications
GUI III IS
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Chapter 16 Event-Driven Programming
Graphical User Interfaces in Java Event-driven programming
Making Java GUIs Functional
Graphical User Interface
Presentation transcript:

Event-driven programming for GUI

The objectives of this unit are: To explain the concept of event-driven programming for GUI applications To understand event, event source, and event classes To know how to register listener objects with source objects; and to implement listener interfaces To understand how an event is handled

Java GUI programming is event-driven In Java GUI components allow users to interact with them, which generate events. Actions can be taken to respond to the events.

Show Action Event

Events and event source Events: mouse movements, mouse clicks, and keystrokes, or by the operating system, such as a timer. Java has pre-defined classes to represent different kinds of events. Each kind of event is defined as a class. For example, ActionEvent defines events such as pressing a button; WindowEvent defines events such as closing or opening a window;

Source object of an event The object in which an event is generated is called the source object. The two button objects are the source objects of the events generated when the buttons are pressed To identify the source object of an event, we use the getSource() method in the event class.

User actions, source objects and related events. Generated Event Click a button JButton ActionEvent Press return on a text field JTextField Select an item JList ListSelectionEvent

Listeners, listener interfaces and event handlers Once an event is generated, it needs to be listened by an object: the listener. The listener object receives information about these events and takes some actions to respond to these events, i.e. to handle the events. The actions for handling the events are defined in the methods specified in the relevant listener interface. The listener object’s class must implement the corresponding event-listener interface. The listener object must register with the source object.

Event and corresponding Listener interface Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent)

Registering with source objects Implement event listener For ShowActionEvent, listener is “this” object ShowActionEvent implements ActionListener //Register this object as the listener for //events generated by the two buttons button1.addActionListener(this); button2.addActionListener(this);

Handling events // implement the actionPerformed method // as specified in the // ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+ " is pressed"); }

ShowActionEvent- explained 1 The class has two buttons and one label. private JButton button1 = new JButton("Button1"); private JButton button2 = new JButton("Button2"); private JLabel msg = new JLabel();   It uses the default BorderLayoutManager. It places button1 at North, button2 at South and the label msg in the Center // Get the content pane of the frame Container container = getContentPane(); // Add buttons to the frame container.add(button1,BorderLayout.NORTH); container.add(button2,BorderLayout.SOUTH); container.add(msg,BorderLayout.CENTER);

ShowActionEvent- explained 2 “this” object is registered with the two buttons to listen to events generated by these two buttons. // Register this object as the listener for // events generated by the two buttons button1.addActionListener(this); button2.addActionListener(this); The class of “this” object, i.e. ShowActionEvent, implements ActionListener.  

public class ShowActionEvent extends JFrame implements ActionListener { … … } ShowActionEvent implements the method in ActionListener, i.e. the actionPerformed() method.   // implement the actionPerformed method as // specified in the ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+" is pressed");

ShowActionEvent- explained 3 ShowActionEvent implements the method in ActionListener, i.e. the actionPerformed() method. // implement the actionPerformed method as // specified in the ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+" is pressed"); }

Student activities Write a java program that displays a frame. In the frame there is one button labeled “Red” and another button labeled “Blue”. When the “Red” button is clicked, the background of the frame is changed to red. When the “Blue” button is clicked, the background colour of the frame is changed to green.

A more complicated example Write a java program to implement the simple calculator to produce a GUI as shown

Summary understand the concepts of event, event source, and event classes know how to register listener objects with source objects; and to implement listener interfaces understand how an event is handled be able to write simple Java event-driven GUI applications.