13
CSE470 Software Engineering Fall 2000 1 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

CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

Embed Size (px)

Citation preview

Page 1: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 1

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

Page 2: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 2

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.javaThis creates a file with .class extension, programfile.class

• Create .html file for appletviewer, run appletviewer– appletviewer programfile.htmlA sample programfile.html is given on the next slide

Page 3: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 3

Sample .html file for Appletviewer

<body><html><applet code = “programfile.class” width=200 height=200>

</applet></html></body>

Page 4: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 4

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

Page 5: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 5

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

Page 6: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 6

Delegation Event Model

Event Source Event Listener

Ex: Button, Canvas, Text Box

An object of a class that will implement methods which will respond to events

Register with source

Event Object e

Page 7: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 7

Delegation Event Model …

• Key Components of the delegation event model:– Event Source (usually GUI object) – Event Listener object– Event object– Event registration

Page 8: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 8

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 ……*/ }

}

Page 9: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 9

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) { …..}

}

Page 10: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 10

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)

Page 11: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 11

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

Page 12: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 12

Example -- Simple Action Eventpublic 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"); } }

}

Page 13: CSE470 Software Engineering Fall 2000 1 Introduction to Java Programming An object-oriented, platform independent language Two types of programs in Java

CSE470 Software Engineering Fall 2000 13

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.