Project Just when you thought it was safe! Event Driven - - PDF document

project
SMART_READER_LITE
LIVE PREVIEW

Project Just when you thought it was safe! Event Driven - - PDF document

Project Just when you thought it was safe! Event Driven Programming Project 2 has been released. Chat Room Threads Introduction GUIs Networking Minimum Submission due 11/3 CS3 - AWT/Swing 1 CS3 - AWT/Swing 2


slide-1
SLIDE 1

CS3 - AWT/Swing 1

Event Driven Programming

Introduction

CS3 - AWT/Swing 2

Project

  • Just when you thought it was safe!

– Project 2 has been released. – Chat Room

  • Threads
  • GUIs
  • Networking

– Minimum Submission due 11/3

CS3 - AWT/Swing 3

Plan for this week

  • Today: Intro to Events
  • Tomorrow: Examples
  • Thursday: GUI Design Tips

CS3 - AWT/Swing 4

Swing

  • So far we have covered how to build GUIs.
  • The GUI will communicate with

applications via Event Driven Programming.

CS3 - AWT/Swing 5

GUI Program Design

  • The GUI provides a view of the program, it is

clearly not the program

  • Making the GUI code as independent of the

program code is a good strategy

– Changes in the program do not necessarily change the GUI – Different GUIs can be developed for the same program – Debugging and maintaining both the GUI and the program code is easier

CS3 - AWT/Swing 6

Model-View-Controller

  • The MVC pattern is commonly used to develop

applications that have a GUI component

  • Consists of three parts

– Model

  • The program

– View

  • The GUI

– Controller

  • The event handling mechanism
slide-2
SLIDE 2

CS3 - AWT/Swing 7

MVC

Model View Controller

The model passes its data to the view for rendering The view determines which events Are passed to the controller The controller updates the model Based on the events received CS3 - AWT/Swing 8

MVC in Swing

Model View Controller

The GUI Program Logic The Application

CS3 - AWT/Swing 9

Event Driven Programming

  • Programs respond to events that are generated
  • utside the control of the program

– User types a key – The left mouse button is pressed – A CD is removed from the CD drive

  • When an event occurs, it is handled by an event

handler

  • Event driven programming involves writing the

handlers and arranging for the handler to be notified when certain events occur

CS3 - AWT/Swing 10

Event Handling

  • Events are represented by objects that gives

information about the event and identifies the event source

– Event sources are typically components, but other kinds

  • f objects can also be event sources
  • A listener is an object that wants to be notified

when a particular event occurs

– An event source can have multiple listeners registered

  • n it

– A single listener can register with multiple event sources

CS3 - AWT/Swing 11

Listeners

  • In order for an object to be notified when a

particular event occurs, the object

– Must implement the appropriate Listener interface – Be registered as an event listener on the appropriate event source

CS3 - AWT/Swing 12

Events and Listeners

slide-3
SLIDE 3

CS3 - AWT/Swing 13

MVC in Swing

myClass View Controller

The GUI Program Logic The Application

anotherClass

CS3 - AWT/Swing 14

Example

  • Buttons

– When pressed buttons will generate an ActionEvent – This event generated will get broadcast to all ActionListeners registered with the button

  • void actionPerformed (ActionEvent event)

– Listener will respond appropriately.

CS3 - AWT/Swing 15

Example

public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent e); }

CS3 - AWT/Swing 16

PressMe

public class PressMe implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println ("I've been pressed"); } public static void main (String args[]) { JFrame win = new JFrame("Button Example"); JButton but = new JButton ("Press Me"); PressMe listener = new PressMe(); but.addActionListener (listener); win.getContentPane().add (but); win.pack(); win.show(); } } CS3 - AWT/Swing 17

PressMe

CS3 - AWT/Swing 18

Events & Event Listeners

  • The event generated is passed to each

listener

  • Listener can perform action based on the

event.

slide-4
SLIDE 4

CS3 - AWT/Swing 19

PressMe2

public class PressMe2 implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println ("I've been pressed. My action is “ + e.getActionCommand()); } public static void main (String args[]) { JFrame win = new JFrame("Button Example"); JButton but; PressMe listener = new PressMe2(); for (int i = 0; i < 5; i++) { but = new Jbutton (“Button “ + i); but.addActionListener (listener); win.getContentPane().add (but); } win.pack(); win.show(); } } CS3 - AWT/Swing 20

PressMe2

CS3 - AWT/Swing 21

Questions?

CS3 - AWT/Swing 22

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 that combines object creation with the declaration of the class.

  • This avoids naming a class, at the cost of only

ever being able to create one instance of that anonymous class.

  • This is handy in the AWT.

CS3 - AWT/Swing 23

Anonymous Class Syntax

  • An anonymous class is defined as part of a new

expression and must be a subclass or implement an interface

  • The class body can define methods but cannot

define any constructors.

  • The restrictions imposed on local classes also

apply

new className( argumentList ) { classBody } new interfaceName() { classBody }

CS3 - AWT/Swing 24

Using Anonymous Classes

public class Dog { private String breed; private String name; public Dog( String theBreed, String theName ) { breed = theBreed; name = theName; } public String getBreed() { return breed; } public String getName() { return name; } public int compareTo( Object o ) throws ClassCastException { Dog other = (Dog)o; int retVal = breed.compareTo( other.getBreed() ); if ( retVal == 0 ) retVal = name.compareTo( other.getName() ); return retVal; } } // Dog

slide-5
SLIDE 5

CS3 - AWT/Swing 25

Using Anonymous Classes

public void PrintDogsByName( List dogs ) { List sorted = dogs; Collections.sort( sorted, new Comparator () { public int compare( Object o1, Object o2) { Dog d1 = (Dog)o1; Dog d2 = (Dog)o2; return d1.getName().compareTo( d2.getName() ); } ); Iterator i = sorted.iterator(); while ( i.hasNext() ) System.out.println( i.next() ); }

CS3 - AWT/Swing 26

Anonymous Class and Swing

  • Anonymous classes are often used to define

simple listener behavior.

CS3 - AWT/Swing 27

PressMe3

public class PressMe3 { public static void main (String args[]) { JFrame win = new JFrame("Button Example"); JButton but = new JButton ("Press Me"); but.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println ("I've been pressed: Action " + e.getActionCommand()); } }); win.getContentPane().add (but); win.pack(); win.show(); } } CS3 - AWT/Swing 28

Major AWT Listeners

ListSelectionListener A table of list selection changes FocusListener A component gets the keyboard focus ComponentListener A component becomes visible MouseMotionListener User moves the move over a component MouseListener User presses a mouse button while the cursor is over a component WindowListener Users closes a frame (main window) ActionListener User clicks a button, presses return while typing in a text filed, or chooses a menu item Listener Type Action

CS3 - AWT/Swing 29

More AWT Listeners

MouseWheelListener User moves the move wheel KeyListener User presses a key TextListener For catching text events AdjustmentListener When the value of an adjustable is changed Listener Type Action

CS3 - AWT/Swing 30

Window Closing

  • A very common event directed towards a window

is a close event

– The default behavior is to simply hide the JFrame when the user closes the window

  • Normally we would want the program to terminate

when the user closes the main window

  • Two steps required to accomplish this

– Write an event handler for the close event that will terminate the program – Register the handler with the appropriate event source

slide-6
SLIDE 6

CS3 - AWT/Swing 31

WindowListener

  • The WindowListener interface

– void windowActivated(WindowEvent e); – void windowClosed(WindowEvent e); – void windowClosing(WindowEvent e); – void windowDeactivated(WindowEvent e); – void windowDeiconified(WindowEvent e); – void windowIconified(WindowEvent e); – void windowOpened(WindowEvent e);

  • A class that implements WindowListener

must implement all of these methods!

CS3 - AWT/Swing 32

WindowAdapter

  • A class that implements WindowListener

– The methods in this class are empty. The class exists as convenience for creating listener objects.

  • To use the WindowAdapter class:

– Extend this class to create a WindowEvent listener – Override the methods for the events of interest – Create a listener object using the extended class and then register it with the Window.

  • When an event occurs the appropriate method in

the listener is invoked.

CS3 - AWT/Swing 33

PressMe4

public class PressMe4 { public static void main (String args[]) { JFrame win = new JFrame("Button Example"); JButton but = new JButton ("Press Me"); ...

win.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit ( 0 ); } });

... win.pack(); win.show(); } } CS3 - AWT/Swing 34

Summary

  • MVC
  • Event Driven Programming
  • Events and Swing

– Events – Listeners – Anonymous Classes – ActionListeners / WindowListeners