Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
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.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
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.
Event-Driven Programming Event:  A signal to the program that something has happened.  It can be triggered either by external user actions, such as mouse.
Event-Driven Programming
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
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.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
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.
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.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
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 12 Event-Driven Programming
A First Look at GUI Applications
Ellen Walker Hiram College
Event-driven programming for GUI
Chapter 16 Event-Driven Programming
Graphical User Interface
Presentation transcript:

Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa

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. Student activity 12.1 – Download the ShowActionEvent progem. Compile and run ShowActionEvent.ShowActionEvent

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. User ActionSource ObjectGenerated Event Click a buttonJButtonActionEvent Press return on a text field JTextFieldActionEvent Select an itemJListListSelectionEvent

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 vents 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 ClassListener InterfaceListener Methods (Handlers) ActionEventActionListeneractionPerformed(ActionEvent) WindowEventWindowListenerwindowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) MouseEventMouseListenermousePressed(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);

Student activity 12.2 Explore the effect of registering listener. Modify the ShowActionEvent example by removing the button1.addActionListener(this); button2.addActionListener(this); Compile and run the program again. Now click on Button1 and Button2, observe what happens and try to explain why.

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 12.3: 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 in Version1. See hint

Summery 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.

Form Editor in Netbeans