OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” -...
-
date post
19-Dec-2015 -
Category
Documents
-
view
217 -
download
1
Transcript of OOP&M - theory lectures1 OOP&M – the sixth day “… there must be a secret out there …” -...
OOP&M - theory lectures 1
OOP&M – the sixth day
“… there must be a secret out there …”- Spok -
OOP&M - theory lectures 2
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
OOP&M - theory lectures 3
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
OOP&M - theory lectures 4
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()
OOP&M - theory lectures 5
OOP&M – Event Handling
Event Model IV
•The following slides show the relationship between events, listeners and methods
Event class Listener Interface Listener methodsActionEvent ActionListener actionPerformed()AdjustmentEvent AdjustmentListener adjustmentValueChanged()ComponentEvent ComponentListener componentHidden()
componentMoved()componentResized()componentShown()
ContainerEvent ContainerListener componentAdded()componentRemoved()
FocusEvent FocusListener focusGained()focusLost()
ItemEvent ItemListener itemStateChanged()KeyEvent KeyListener keyPressed()
keyReleased()keyTyped()
OOP&M - theory lectures 6
OOP&M – Event Handling
Event Model V
Event class Listener Interface Listener methodsMouseEvent MouseListener mouseClicked()
mouseEntered()mouseExited()mousePressed()mouseReleased()
MouseMotionListener mouseDragged()mouseMoved()
TextEvent TextListener textValueChanged()WindowEvent WindowListener windowActivated()
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.
OOP&M - theory lectures 7
OOP&M – After the party
OOP&M - theory lectures 8
OOP&M – After the party
Bjorn Lisa
Events of the classesput and order
Listener:He handles the events:-order the records-put the lyrics into the folders-put the folders in their placeBut 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
OOP&M - theory lectures 9
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){;}
OOP&M - theory lectures 10
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.)
OOP&M - theory lectures 11
OOP&M – Event Handling
Event Model VII
•Question: Which events are related to our GUI elements?
Component Events Generated Meaning
Button ActionEvent User clicked on the button
Checkbox ItemEvent User deselected or selected an item
CheckboxMenuItem ItemEvent User deselected or selected an item
Choice ItemEvent User deselected or selected an item
Component ComponentEvent Component moved, resized, hidden or shown
FocusEvent Component gained or lost focus
KeyEvent User pressed or releaseda key
OOP&M - theory lectures 12
OOP&M – Event Handling
Event Model VIII
Component Events Generated Meaning
Component MouseEvent User pressed or releasedmouse button, mouse entered or exited a component or user moved
a or dragged the mouseContainer ContainerEvent Component added to or
removed from a containerList ActionEvent User double-clicked item
ItemEvent User deselected or selected an item
MenuItem ActionEvent User selected a menu itemScrollbar AdjustmentEvent User moved the scrollbarTextComponent TextEvent User changed textTextField ActionEvent User finished editing txt
Window WindowEvent Window opened, closed, iconified, deiconified
or close request
OOP&M - theory lectures 13
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
OOP&M - theory lectures 14
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;}
OOP&M - theory lectures 15
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:
KeyEvent KeyListener keyPressed()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)
OOP&M - theory lectures 16
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 ]
OOP&M - theory lectures 17
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;}
OOP&M - theory lectures 18
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
OOP&M - theory lectures 19
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
OOP&M - theory lectures 20
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:
ItemEvent ItemListener itemStateChanged()
•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
OOP&M - theory lectures 21
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 ]
OOP&M - theory lectures 22
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
OOP&M - theory lectures 23
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
OOP&M - theory lectures 24
OOP&M – Event Handling – Inner Classesimport 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;
}