Introduction to Java Programming

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 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
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.
Intermediate Java1 An example that uses inner classes Week Four Continued.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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.
Event Handling in Java CSE Software Engineering Spring 2000 By Prasad.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
Io package as Java’s basic I/O system continue’d.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
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.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
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.
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.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Welcome To java
CompSci 230 S Programming Techniques
Applets.
Web Design & Development Lecture 11
Lecture 09 Applets.
GUI III IS
CSE 331 Software Design and Implementation
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
CSE 331 Software Design and Implementation
Events, Event Handlers, and Threads
Presentation transcript:

Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java Applets (run from web browsers) Command line programs Primary focus of this lab Event Handling mechanisms of Java Developing GUI prototypes in Java These slides were created by S.P.N.Durga Prasad for CSE470, Spring 2000 and updated by Chirantana Sriram for CSE470, Fall 2000. CSE470 Software Engineering Fall 2000

To Compile and run Java Applets Include path setenv PATH {$PATH}:/usr/java1.2/bin in your .personal file Create java program file programfile.java using any editor Compile Java program javac programfile.java This creates a file with .class extension, programfile.class Create .html file for appletviewer, run appletviewer appletviewer programfile.html A sample programfile.html is given on the next slide CSE470 Software Engineering Fall 2000

Sample .html file for Appletviewer <body> <html> <applet code = “programfile.class” width=200 height=200> </applet> </html> </body> CSE470 Software Engineering Fall 2000

CSE470 Software Engineering Fall 2000 Event Handling in Java Graphical User Interfaces are event-driven To process a GUI event, a programmer should: register an event listener implement an event handler Number of different event classes and event-listener interfaces defined in the package java.awt.event CSE470 Software Engineering Fall 2000

Abstract Interfaces, Delegation Event-listener interfaces define only abstract methods Programmer must define all methods of an interface in a class that implements the interface Event-listener objects for an event are created from the class that implements the required interface Event handling model of Java is known as delegation event model because event processing is delegated to particular objects CSE470 Software Engineering Fall 2000

Delegation Event Model Register with source Event Source Event Listener Event Object e Ex: Button, Canvas, Text Box An object of a class that will implement methods which will respond to events CSE470 Software Engineering Fall 2000

Delegation Event Model … Key Components of the delegation event model: Event Source (usually GUI object) Event Listener object Event object Event registration CSE470 Software Engineering Fall 2000

CSE470 Software Engineering Fall 2000 Event Listeners Some Event Listener interfaces of java.awt.event: ActionListener MouseListener MouseMotionListener Examples of classes to implement interfaces: Class mylistener implements ActionListener { /* The following method is called when a Button is pressed. */ public void actionPerformed ( ActionEvent e ) { /*event handler code goes here ……*/ } } CSE470 Software Engineering Fall 2000

Event Listeners (contd.) … Example of a listener for multiple events: Class mylistener implements MouseListener { /* Following method is called when the mouse button is clicked */ public void mouseClicked(MouseEvent evt) { ….} /* Called when the mouse button is depressed */ public void mousePressed(MouseEvent evt) { …..} /* Called when the mouse button is released */ public void mouseReleased(MouseEvent evt) { …..} } CSE470 Software Engineering Fall 2000

CSE470 Software Engineering Fall 2000 Event Objects Event Objects carry information about the event Event Listeners obtain information from Event Sources through Event Objects Example: MouseEvent Evt Evt.getX() --- gets the X-coordinate of mouse click Evt.getY() --- gets the Y-coordinate of mouse click Evt.getSource() --- gets the source object of the mouse click event (like a button) CSE470 Software Engineering Fall 2000

CSE470 Software Engineering Fall 2000 Event Registration A Listener object has to register itself with an Event Source to catch events on the source Example: Button1.addActionListener( new mylistener1() ) Button1.addMouseListener( new mylistener2() ) Multiple Listeners can listen to a single event source (for example, a KeyListener and a MouseListener could both listen to the same source) A single Listener can listen to multiple event sources CSE470 Software Engineering Fall 2000

Example -- Simple Action Event public class SimpleEvent extends Applet { Button button1 = new Button("Press me"); public void init() { this.add(button1); button1.addActionListener(new mylistener()); } Class mylistener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) button1.setLabel("Again"); CSE470 Software Engineering Fall 2000

CSE470 Software Engineering Fall 2000 References G. Cornell and C. Horstmann, “Core Java”, The Sunsoft Press Java Series, Second Edition, 1997 D. Joshi, L. Lemay, and C. Perkins, “Teach yourself Java in Café in 21 days”, Sams.net publishing, 1996 The Java Tutorial http://java.sun.com/docs/books/tutorial/index.htm D. Geary and A. McClellan, “Graphic Java”, The Sunsoft Press Java Series, 1997 Deitel & Deitel, “Java How To Program” Third Edition, Prentice Hall Inc. CSE470 Software Engineering Fall 2000