EVENTS, EVENT SOURCES AND LISTENERS

Similar documents
CS111: PROGRAMMING LANGUAGE II

Java Event Handling -- 1

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Introduction. Introduction

Graphical User Interfaces. Comp 152

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

GUI Event Handlers (Part II)

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Dr. Hikmat A. M. AbdelJaber

Chapter 17 Creating User Interfaces

Swing/GUI Cheat Sheet

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Chapter 6: Graphical User Interfaces

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Java - Applets. public class Buttons extends Applet implements ActionListener

To gain experience using GUI components and listeners.

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

Chapter 12 Advanced GUIs and Graphics

Contents Chapter 1 Introduction to Programming and the Java Language

YEAH session #6. 6 March 2014, 5:30p-6:30p Miles Seiver

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

Agenda. Container and Component

SD Module-1 Advanced JAVA

CoSc Lab # 5 (The Controller)

SD Module-1 Advanced JAVA. Assignment No. 4

Swing Programming Example Number 2

Chapter 18. Advanced graphics programming

MIT AITI Swing Event Model Lecture 17

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Part I: Learn Common Graphics Components

Chapter 4. Swing 18 marks

navlakhi.com / navlakhi.education / navlakhi.mobi / navlakhi.org 1

Programming graphics

GUI in Java TalentHome Solutions

Part 3: Graphical User Interface (GUI) & Java Applets

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

GUI Event Handlers (Part I)

Graphical User Interface (GUI)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Solution register itself

Contents Introduction 1

CS 106A, Lecture 23 Interactors and GCanvas

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Event Driven Programming

Chapter 7: A First Look at GUI Applications

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Swing. By Iqtidar Ali

Abstract Factory Pattern

PART 22. Java GUI Library SWT GUI Library SWING GUI Library Swing First Program

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java Applets This is not a Java course! (You re supposed to know about Java.)

Graphical User Interfaces in Java - SWING

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

GUI Software Architecture

The AWT Event Model 9

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

CS 106A, Lecture 24 Interactors and NameSurfer

Java Applets / Flash

CHAPTER 2. Java Overview

Class 16: The Swing Event Model

Abstract Factory Pattern (Creational Pattern) Week 05 (2nd Half) Report 05 A. Firuza Aibara P11119

Chapter 14. Exception Handling and Event Handling

Graphical Applications

IPM 12/13 P4 Handling Events in AWT

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

Java. GUI building with the AWT

Graphical User Interfaces

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Adding Buttons to StyleOptions.java

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Index SELF-STUDY. Symbols

Introduction to concurrency and GUIs

MODEL UPDATES MANIPULATES USER

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

CS 251 Intermediate Programming GUIs: Event Listeners

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

CSC 1214: Object-Oriented Programming

Java for Interfaces and Networks (DT3010, HT10)

Java IDE Programming-I

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Transcription:

Java Programming EVENT HANDLING Arash Habibi Lashkari Ph.D. Candidate of UTM University Kuala Lumpur, Malaysia All Rights Reserved 2010, www.ahlashkari.com

EVENTS, EVENT SOURCES AND LISTENERS Important components involved in event- handling: Events Events are represented as Event objects. For example, a button-click event is represented as an ActionEvent object. Event sources An event source is a component (e.g. a JButton object) closely associated with the event which sends notifications to event listeners. For example, the JButton object associated with a button-click event.

EVENTS, EVENT SOURCES AND LISTENERS Event listeners Event listeners are objects which will be notified by event sources about the occurrence of events.

HANDLING BUTTON-CLICK EVENTS Suppose we would like the applet to display a Suppose we would like the applet to display a message when the user clicks on the button.

HANDLING BUTTON-CLICK EVENTS The following steps can be used for handling events: 1. Declare class as an event listener 2. Register event listener with event source 3. Write event-handling code

HANDLING BUTTON-CLICK EVENTS Declare class as an event listener : Public class TestApplet exteds JApplet implements ActionListener { }

HANDLING BUTTON-CLICK EVENTS Register event listener with event source : : JTextField teks = new JTextField(15); btnok = new JButton("Click me please..."); public void init() { setlayout(new t( FlowLayout(FlowLayout.CENTER,5,10)); L t add(btnok); add(teks); btnok.addactionlistener(bh); :

HANDLING BUTTON-CLICK EVENTS Write event-handling code : Public class TestApplet exteds JApplet implements ActionListener { public void actionperformed(actionevent e) { Object obj = e.getsource(); if(obj==btnok) teks.settext("thank e you!!!"); } }

HANDLING BUTTON-CLICK EVENTS The components involved: Event The ActionEvent object Event source The Click me please JButton object When the user clicks on the button, the JButton object sends an ActionEvent object to all event listeners. Event listener This object will receive an ActionEvent object from the JButton object when a button-click event occurs.

4. the event listener executes actionperformed() method actionperformed(e) 3. the event source sends a notification to the event listener e : ActionEvent 2. An ActionEvent object is created 1. User clicks button : ActionListener : JApplet button-click event listener : JButton : JTextField button-click event source 2. a button-click event has occurred!

User Action Event Source Event Object Type of Listener Created Click on a JButton ActionEvent ActionListener button Select a new item in a combo box JComboBox ItemEvent, ActionEvent ItemListener, ActionListener Select an item from a list JList ListSelectionEvent ListSelectionListener Click on a checkbox JCheckBox ItemEvent, ActionEvent ItemListener, ActionListener Click on a radio JRadioButton ItemEvent, ItemListener, button ActionEvent ActionListener Drag slider knob Press enter in a textfield JSlider ChangeEvent ChangeListener JTextField ActionEvent ActionListener

Example Event source Method for registering as listener 1 JButton addactionlistener() 2 JTextField addactionlistener() 3 JCheckBox additemlistener() 4 JSlider addchangelistener() Example Type of Listener Method that must be implemented by listener 1, 2 ActionListener public void actionperformed(actionevent e) 3 ItemListener public void itemstatechanged(itemevent e) 4 ChangeListener public void statechanged(changeevent e)

Questions www.ahlashkari.com 13

THANK YOU Arash Habibi Lashkari PHD. Candidate of UTM Kuala Lumpur, Malaysia Feb, 2010 THE END www.ahlashkari.com 14