Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -

Similar presentations


Presentation on theme: "OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -"— Presentation transcript:

1 OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -

2 OOP&M - theory lectures2 OOP&M – Event Handling Event Model The Java event model is used by both the AWT and Java Beans (Java Beans will be explained during the 2nd course) Every event is a subclass of java.util.EventObject AWT events, the ones under study here, have been placed in the java.awt.event package (so it is easier to use them) Every event has a source object, which can be obtained with the method: getSource(), and every event has a type value, which can be obtained with getID() The type value is used to distinguish the various types of events that are represented by the same event class. E.g. The event MouseEvent can distinguish different events including the one called MouseEvent.MOUSE_PRESSED or the MouseEvent.MOUSE_DRAGGED between others

3 OOP&M - theory lectures3 OOP&M – Event Handling Event Model II Event classes contain data values that correspond to the particular event type. E.g. MouseEvent has getX(), getY() and getClickCount() methods The Event Handling model is based on the concept of an “event listener”. An object interested in receiving events is an event listener An object that generates events (event source) maintains a list of listeners that are interested in being notified when an event occurs, and provides methods that allow listeners to add themselves and remove themselves from this list of interested objects When the event source generates an event, the event source notifies all the listener objects that the event has occurred

4 OOP&M - theory lectures4 OOP&M – Event Handling Event Model III An event source notifies an event listener object by invoking a method on it and passing it an event object (an instance of a subclass of EventObject ) For this reason, we must implement methods with PREDEFINED names in order to treat the different events E.g. If we want the program to execute an action when we click with the mouse, the event is the MouseEvent, its listener is the MouseListener and the method to implement is called: mouseClicked() One event defines more than one method, as far as the objects affected by that event can interact in different ways. E.g. The event KeyEvent, that controls the keyboard, can be assigned to the methods: keyPressed(), keyReleased() and keyTyped()

5 OOP&M - theory lectures5 OOP&M – Event Handling Event Model IV The following slides show the relationship between events, listeners and methods Event classListener InterfaceListener methods ActionEventActionListeneractionPerformed() AdjustmentEventAdjustmentListeneradjustmentValueChanged() ComponentEventComponentListenercomponentHidden() componentMoved() componentResized() componentShown() ContainerEventContainerListenercomponentAdded() componentRemoved() FocusEventFocusListenerfocusGained() focusLost() ItemEventItemListeneritemStateChanged() KeyEventKeyListenerkeyPressed() keyReleased() keyTyped()

6 OOP&M - theory lectures6 OOP&M – Event Handling Event Model V Event classListener InterfaceListener methods MouseEventMouseListenermouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased() MouseMotionListenermouseDragged() mouseMoved() TextEventTextListenertextValueChanged() WindowEventWindowListenerwindowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() if you implement the listener to one event, you have to declare ALL the methods, so if you are interested in only one of them, you have to declare the heads of the rest.

7 OOP&M - theory lectures7 OOP&M – After the party

8 OOP&M - theory lectures8 OOP&M – After the party BjornLisa Events of the classes put and order Listener: He handles the events: -order the records -put the lyrics into the folders -put the folders in their place But not: -clean the floor -bring the food to the kitchen Event Source: She produces the events: -order the records -put the lyrics into the folders -put the folders in their place -clean the floor -bring the food to the kitchen

9 OOP&M - theory lectures9 Bjorn Listener: Needs to be declared before, therefore Lisa must warn him than he will have to collaborate after the party class Bjorn implements Aspirin { … } OOP&M – After the party The different actions must be declared and the things that he is not going to do, too void orderDisc(LisaEvent e){ … } void putLyrics(LiseEvent e){ … } void cleanFloor(LisaEvent e){;}

10 OOP&M - theory lectures10 OOP&M – Event Handling Event Model VI For each of the event listener interfaces that contains more than one method, java.awt.event defines a simple “adapter” class that provides an empty body for each of the methods in the interface So you have to override each one of those methods when you want to control one event Now we are going to compare different cases with different events, but first we need to make a different approach to the problem We work with elements of the GUI, so the question now is which events affect to those objects (buttons, menus, scrollbars, windows, etc.)

11 OOP&M - theory lectures11 OOP&M – Event Handling Event Model VII Question: Which events are related to our GUI elements? ComponentEvents GeneratedMeaning ButtonActionEventUser clicked on the button CheckboxItemEventUser deselected or selected an item CheckboxMenuItemItemEventUser deselected or selected an item ChoiceItemEventUser deselected or selected an item ComponentComponentEventComponent moved, resized, hidden or shown FocusEventComponent gained or lost focus KeyEventUser pressed or released a key

12 OOP&M - theory lectures12 OOP&M – Event Handling Event Model VIII ComponentEvents GeneratedMeaning ComponentMouseEventUser pressed or released mouse button, mouse entered or exited a component or user moved a or dragged the mouse ContainerContainerEventComponent added to or removed from a container ListActionEventUser double-clicked item ItemEventUser deselected or selected an item MenuItemActionEventUser selected a menu item ScrollbarAdjustmentEventUser moved the scrollbar TextComponentTextEventUser changed text TextFieldActionEventUser finished editing txt WindowWindowEventWindow opened, closed, iconified, deiconified or close request

13 OOP&M - theory lectures13 OOP&M – Event Handling – Working method Event Model IX What shall be the question when we want to control an event? Are we looking for controlling something that happens (mouse movements, clicks, key pressed...) Are we looking for controlling a GUI’s component? Use 1st table and look for the special event that you want to control Use 2nd table and look for exactly the component that you want to control A B

14 OOP&M - theory lectures14 OOP&M – Event Handling – generalizing import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements XListener { public void init() { r = new Object(...); add(r); r.addXListener(this); } public void XaOccurred(XEvent event) { s = getInformationFromX(); DoSomethingWithTheInformation(); } public void XbOccurred(KeyEvent event) {;} private Object r; }

15 OOP&M - theory lectures15 OOP&M – Event Handling – the keyboard Event Model X Imagine a case of the first class, imagine that we want to control the keyboard, it is quite clear from table one that we must look for a KeyEvent Example: KeyEventKeyListenerkeyPressed() keyReleased() keyTyped() From the table we can extract the following information: –KeyEvents require the implementation of KeyListeners –Each KeyListener includes three different methods: if a key was pressed if it was released if it was typed (pressed and released)

16 OOP&M - theory lectures16 OOP&M – Event Handling – the keyboard Exercise Knowing that the keyboard is a source of KeyEvents, and that the KeyListener can attend to the methods: keyPressed(), keyReleased() and keyTyped(), try to write a program that shows a Label component where it will be shown which key was typed [Hint: *.getKeyText() *.getKeyCode() are needed for detecting which key was typed ]

17 OOP&M - theory lectures17 OOP&M – Event Handling – the keyboard import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements KeyListener { public void init() { result = new Label(" "); add(result); result.addKeyListener(this); } public void keyPressed(KeyEvent event) { String s = event.getKeyText(event.getKeyCode()); result.setText(s); doLayout(); } public void keyTyped(KeyEvent event) {;} public void keyReleased(KeyEvent event) {;} private Label result; }

18 OOP&M - theory lectures18 OOP&M – Event Handling – the keyboard import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicKey extends Applet implements KeyListener { public void init() { result = new Label(" "); add(result); result.addKeyListener(this); } public void keyPressed(KeyEvent event) { String s = event.getKeyText(event.getKeyCode()); result.setText(s); doLayout(); } public void keyTyped(KeyEvent event) {;} public void keyReleased(KeyEvent event) {;} private Label result; } Declaration of the listener of the events on the keyboard Here we override the declaration of the method that will be called by the interface Even if we don’t use them, we must declare the rest of the methods for this listener

19 OOP&M - theory lectures19 OOP&M – Event Handling – Working method Event Model XI What shall be the question when we want to control an event? Are we looking for controlling something that happens (mouse movements, clicks, key pressed...) Are we looking for controlling a GUI’s component? Use 1st table and look for the special event that you want to control Use 2nd table and look for exactly the component that you want to control A B

20 OOP&M - theory lectures20 OOP&M – Event Handling – a Checkbox Event Model XII Imagine a case of the second class, imagine that we want to control a Checkbox, from table two we get the information about which events are related to the object. In this case is ItemEvent Example: ItemEventItemListeneritemStateChanged() From the table we can extract the following information: –Checkboxes are controlled by ItemEvents –ItemEvents require the implementation of ItemListeners –Each ItemListener includes only one possible method: if the state of the item changed

21 OOP&M - theory lectures21 OOP&M – Event Handling – the keyboard Exercise Knowing that a CheckBox is a source of ItemEvents, and that the ItemListener can attend to the methods: ItemStatechanged(), try to write a program that shows a Checkbox component where it will be shown that is is selected on its Label [Hint: *.setLabel() is needed for showing the text, *.getState() for knowing the state ]

22 OOP&M - theory lectures22 import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicCheckbox extends Applet implements ItemListener { public void init() { mybox = new Checkbox("not checked"); add(mybox); mybox.addItemListener(this); } public void itemStateChanged(ItemEvent event) { if (mybox.getState()) { mybox.setLabel("checked"); } else { mybox.setLabel("not checked"); } doLayout(); } private Checkbox mybox; } OOP&M – Event Handling – a Checkbox

23 OOP&M - theory lectures23 import java.awt.*; import java.applet.*; import java.awt.event.*; public class BasicCheckbox extends Applet implements ItemListener { public void init() { mybox = new Checkbox("not checked"); add(mybox); mybox.addItemListener(this); } public void itemStateChanged(ItemEvent event) { if (mybox.getState()) { mybox.setLabel("checked"); } else { mybox.setLabel("not checked"); } doLayout(); } private Checkbox mybox; } Declaration of the listener of the item’s events Here we override the declaration of the method that will be called by the interface There are not other methods for this listener OOP&M – Event Handling – a Checkbox

24 OOP&M - theory lectures24 OOP&M – Event Handling – Inner Classes import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble3 extends Applet implements MouseListener, MouseMotionListener { public void init() { b = new Button("Toggle Color"); add(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (status) { result.setText("Red"); status = false; } else { result.setText("Black"); status = true; } doLayout(); //b.removeActionListener(this); } }); result = new Label("Black"); add(result); } private Button b; private Label result; private boolean status = true; }


Download ppt "OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” - Spok -"

Similar presentations


Ads by Google