Chapter 6 Events in Java 1 0 Model

































- Slides: 33

Chapter 6: Events in Java • 1. 0 Model – Event class – selecting component to handle – handle. Event method – helper methods (action, key. Up, mouse. Up, etc. ) • 1. 1 Model – Listeners - connected to Event generators. Event types: Action. Event, Item. Event, etc. – corresponding Listeners: Action. Listener, etc. – connecting generator to listener – Interfaces and Adapters

Abstract Classes • Class may be abstract by adding modifier in class header public abstract class My. Class • Resulting class cannot be instantiated • Used to create general classes (Car) that you expect to specialize

Interfaces • A class may only inherit from one parent • Sometimes want to create specialized class, combines several capabilities • Interface is a list of methods to instantiate public interface Drawable { public void draw() {} // empty body } • Class header, indicate implements interface public class Rect extend GObj implements Drawable; • must implement all methods of interface • may have more than one interface

Event-Driven Programming • Input “events” by the user caught by Java – move mouse, click button, scroll window, type key • For each, Java creates an Event object • Object added to queue, your program is then expected to “handle” the events

1. 0 Event Model Event object: instance vars: Object target - which object generated the event id - a value indicating the type of event Object arg - useful info about the event (dependent) long when - time of event int x - x location of event int y - y location of event int click. Count - number of times button clicked int key - key pressed (for key events) int modifiers - for key and mouse events

Event id Constants ACTION_EVENT - Buttons, Checkboxes, Choices, Lists, Menu. Items, Text. Fields KEY_PRESS, KEY_RELEASE, KEY_ACTION_RELEASE MOUSE_DOWN, MOUSE_UP, MOUSE_DRAG, MOUSE_MOVE, MOUSE_ENTER, MOUSE_EXIT - mouse LIST_SELECT, LIST_DESELECT - List events SCROLL_ABSOLUTE, SCROLL_LINE_UP, SCROLL_LINE_DOWN, SCROLL_PAGE_UP, SCROLL_PAGE_DOWN WINDOW_EXPOSE, WINDOW_ICONIFY, WINDOW_DESTROY, WINDOW_MOVED LOAD_FILE, SAVE_FILE GOT_FOCUS, LOST_FOCUS

Handling Events • Java does lots of stuff for you (keys in Text objects, window moves, etc. ) • You add handling events for things you want handled a particular way, or when no obvious method (what to do when button pushed)

Source of the Event • Java sends event to Component it thinks is responsible • Component should – handle event and stop – pass event its parent class – say it can’t handle event (pass to parent in GUI) – handle event and pass on (rare) • Java will automatically pass event to parent in GUI hierarchy if not handled – for Button, Comp (e. g. Panel) contains Button

Event-Handling Methods • Every component has handle. Event method public boolean handle. Event(Event e) { if (e. id == Event. WINDOW_DESTROY) { System. exit(0); return true; }} • Return value indicates if handled – true - event handled, terminate – false - event not completely handled, try someone else (parent in GUI hierarchy)

Passing On Events • Sometimes we handle some events, want to pass it on to others: public boolean handle. Event(Event e) { if (e. id == Event. WINDOW_DESTROY) { … } else return super. handle. Event(e); } • Give the next Component up in the class hierarchy the chance to handle event

Helper Methods • handle. Event rarely used • Java provides “helper” methods specific to particular types of events – can be implemented instead of handle. Event – called after handle. Event tried – action events - actions – key events - key. Down, key. Up – mouse events - mouse. Down, mouse. Up, mouse. Drag, mouse. Move, mouse. Enter, mouse. Exit

action Method • form: public boolean action (Event e, Object arg) {} • used for many widgets: – Button • e. target is button obj, arg is string name of button – Checkbox • e. target is Cb obj, arg is check value (true or false) – Choice, List • e. target is choice obj, arg is string of choice made

Helper Calling Parent • As with handle. Event, if helper fails to handle event, should call superclass action: – return super. action(e, arg) • Return values have same meaning as in handle. Event

key Methods • form: public boolean key. Up (Event e, int key) {} public boolean key. Down(Event e, int key) {} • key is char pressed – modifiers field indicates if Shift, Alt, Meta, or Control held while pressed – check with constants SHIFT_MASK, META_MASK, CONTROL_MASK (e. modifiers & Event. SHIFT_MASK) or e. shift. Down() also has meta. Down and control. Down, but none for Alt – other keys (Functions like F 1) use Action Key events, Java has constants for such (Event. F 1)

mouse Methods • form: public boolean method(Event e, int x, int y) {} methods: mouse. Up, mouse. Down, mouse. Move, mouse. Drag, mouse. Enter, mouse. Exit • x, y are locations of occurrence – modifiers used to indicate if left, right, center – no modifiers: left – meta set: right – alt set: center

import java. applet. *; import java. awt. *; public class Test. Action extends Applet { Button button = new Button("AButton"); Checkbox cbox 1 = new Checkbox("CB 1"); Checkbox cbox 2 = new Checkbox("CB 2"); Checkbox. Group cbg = new Checkbox. Group(); Choice choice = new Choice(); Panel panel = new Panel(); My. Canvas canvas = new My. Canvas(); public void init () { panel. set. Layout(new Grid. Layout(2, 2)); cbox 1. set. Checkbox. Group(cbg); cbox 2. set. Checkbox. Group(cbg); choice. add. Item("Item 1"); choice. add. Item("Item 2"); choice. add. Item("Item 3"); panel. add(cbox 1); panel. add(cbox 2); panel. add(button); panel. add(choice); panel. layout(); set. Layout(new Grid. Layout(2, 1)); add(panel); add(canvas); }

} public boolean action (Event e, Object arg) { if (e. target == button) { canvas. set. Message(arg + " pressed"); } else if ((e. target == cbox 1) || (e. target == cbox 2)) { Checkbox tempcb = (Checkbox) e. target; canvas. set. Message(tempcb. get. Label() + " checked"); } else if (e. target == choice) { canvas. set. Message(arg + " chosen"); } return true; } class My. Canvas extends Canvas { private String the. Message = “”; public My. Canvas() { resize(500, 40); } public void paint(Graphics g) { g. draw. String(the. Message, 10, 30); } } public void set. Message(String s) { the. Message = s; repaint(); }

1. 0 to the 1. 1 Delegation Model • • Event generators tend to be scattered in 1. 0 Handlers often centralized Difficult to write good general handlers Idea: connect generators with appropriate handlers

The Delegation Model (1. 1) • Any object can be a source of an Event • Any object can listen for events – implement (register) an appropriate listener interface – listeners stay close to generators • Need to import java. awt. event. *

General methods Object get. Source() - generator of event, from Event. Object int get. ID() - constant for type of event (useful for some Mouse or Key events) - from AWTEvent

Handling Events • For different types of events we have – an Event class (with useful methods) – an Event. Listener interface with • required methods that must be implemented as part of listener – after creating instance of class that can generate events, connect to appropriate listener interface • use add. Listener. Type(Object generator) methods

Action Events • click Button, double-click member in List, select Menu. Item, press <enter> in Text. Field • Action. Event methods: – String get. Action. Command() - string for source • button - label • list, menuitems - text of selected item • textfield - content of textfield – int get. Modifiers() - use to check if modifiers set • Listener: Action. Listener – methods: void action. Performed(Action. Event e)

Item Events • Select Checkbox, Checkbox. Menu. Item, Choice item, or single-click List item • Item. Event methods: – Object get. Item() - item selected (usually String) – int get. State. Change() - returns value of state • Item. Event. SELECTED or Item. Event. DESELECTED – Item. Selectable get. Item. Selectable() - return Item. Selectable object that generated event • Listener: Item. Listener – methods: void item. State. Changed(Item. Event e)

Input Events • Abstract class for Key and Mouse Events • Has constants for masks: ALT_MASK, etc. • Also has boolean methods: – boolean is. Alt. Down() – boolean is. Control. Down() – boolean is. Meta. Down() – boolean is. Shift. Down() • Other method: int get. Modifiers()

Mouse Events • Mouse. Event, Mouse. Moved. Event methods: – int get. X() - x location of mouse event – int get. Y() - y location of mouse event – Point get. Point() - x, y location – int get. Click. Count() - number of clicks • Listeners: – Mouse. Listener - for click, press, enter, exits • methods: – void mouse. Clicked(Mouse. Event e) – mouse. Pressed, mouse. Released, mouse. Entered, mouse. Exited – Mouse. Motion. Listener - for drag, move • methods: – void mouse. Moved(Mouse. Moved. Event e) – void mouse. Dragged(Mouse. Moved. Event e)

Key Events • Key. Event methods: – char get. Key. Char() - char of key pressed – boolean is. Action. Key() - true if action key – int get. Key. Code() - code for action keys – void set. Key. Char(char c) - set char in event – void set. Key. Code(int key. Code) - set key. Code • Listener: Key. Listener – Methods: • void key. Pressed(Key. Event e) • void key. Released(Key. Event e) • void key. Typed(Key. Event e)

Text Events • Changes to Text. Field or Text. Area • Listener: Text. Listener – methods: void text. Value. Changed(Text. Event e)

Adapters • To implement an interface you must implement ALL methods of interface – even if only an empty method • Alternate solution: extend an Adapter (interface with empty methods): – Mouse. Adapter – Mouse. Motion. Adapter – Key. Adapter

General Use of Listener in declaring generator: private Widget my. Widget = new Widget(); private Aprop. List. Class my. Listener; in init or constructor: my. Listener = new Aprop. List. Class(my. Widget); my. Widget. add. Aprop. Listener(my. Listener) new class: class Aprop. List. Class implements Aprop. Listener { private Widget copy. Widget; public Aprop. List. Class(Widget w) { copy. Widget = w; } public void aprop. Listener. Method(Item e) { // implement } }

import java. applet. *; import java. awt. event. *; public class Test. Action extends Applet { Button button = new Button("AButton"); private AButton. Listener my. Button. Listener; Checkbox cbox 1 = new Checkbox("CB 1"); private ACheckbox. Listener my. CB 1 Listener; Checkbox cbox 2 = new Checkbox("CB 2"); private ACheckbox. Listener my. CB 2 Listener; Checkbox. Group cbg = new Checkbox. Group(); Choice choice = new Choice(); private AChoice. Listener my. Choice. Listener; Panel panel = new Panel(); My. Canvas canvas = new My. Canvas();

public void init () { my. Button. Listener = new AButton. Listener(button, canvas); button. add. Action. Listener(my. Button. Listener); my. CB 1 Listener = new ACheckbox. Listener(cbox 1, canvas); cbox 1. add. Item. Listener(my. CB 1 Listener); my. CB 2 Listener = new ACheckbox. Listener(cbox 2, canvas); cbox 2. add. Item. Listener(my. CB 2 Listener); my. Choice. Listener = new AChoice. Listener(choice, canvas); choice. add. Item. Listener(my. Choice. Listener); } } panel. set. Layout(new Grid. Layout(2, 2)); cbox 1. set. Checkbox. Group(cbg); cbox 2. set. Checkbox. Group(cbg); choice. add. Item("Item 1"); choice. add. Item("Item 2"); choice. add. Item("Item 3"); panel. add(cbox 1); panel. add(cbox 2); panel. add(button); panel. add(choice); panel. validate(); set. Layout(new Grid. Layout(2, 1)); add(panel); add(canvas);

class AButton. Listener implements Action. Listener { Button button. Copy; My. Canvas canvas. Copy; public AButton. Listener(Button bcopy, My. Canvas ccopy) { button. Copy = bcopy; canvas. Copy = ccopy; } } public void action. Performed(Action. Event e) { canvas. Copy. set. Message(button. Copy. get. Label() + " pressed"); } class ACheckbox. Listener implements Item. Listener { Checkbox checkbox. Copy; My. Canvas canvas. Copy; public ACheckbox. Listener(Checkbox cbcopy, My. Canvas ccopy) { checkbox. Copy = cbcopy; canvas. Copy = ccopy; } } public void item. State. Changed(Item. Event e) { canvas. Copy. set. Message(checkbox. Copy. get. Label() + ” checked"); }

class AChoice. Listener implements Item. Listener { Choice choice. Copy; My. Canvas canvas. Copy; public AChoice. Listener(Choice chcopy, My. Canvas ccopy) { choice. Copy = chcopy; canvas. Copy = ccopy; } public void item. State. Changed(Item. Event e) { canvas. Copy. set. Message(choice. Copy. get. Selected. Item() + ” chosen"); } } class My. Canvas extends Canvas { private String the. Message = ""; public My. Canvas() { set. Size(500, 40); } public void paint(Graphics g) { g. draw. String(the. Message, 10, 30); } public void set. Message(String s) { the. Message = s; repaint(); } }