Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects At Their Best— Java's Abstract Window Toolkit

Similar presentations


Presentation on theme: "Objects At Their Best— Java's Abstract Window Toolkit"— Presentation transcript:

1 Objects At Their Best— Java's Abstract Window Toolkit
GUI Aint Just Buttons Other components: Text fields Windows Associated with different events Changing text Closing a window Different events, different Event classes April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

2 Objects At Their Best— Java's Abstract Window Toolkit
Event Summary April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

3 Objects At Their Best— Java's Abstract Window Toolkit
Event Classes Each Event class is associated with One or more component classes event source A listener interface Implemented by event handler (listener) class A registration method allows listener to register with the event source defined in component class (event source) April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

4 Java Event Naming Conventions
Event classes EventTypeEvent Example: TextEvent, ActionEvent Listener interfaces EventTypeListener Example: TextListener, ActionListener Listener registration method addEventTypeListener or setEventTypeListener Example: addTextListener, addActionListener April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

5 Objects At Their Best— Java's Abstract Window Toolkit
Listener Interfaces Contain methods invoked by the event source No strict naming conventions Single method (usually) Example: textValueChanged, actionPerformed April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

6 Listener Interfaces With Multiple Methods
Some listener interfaces have multiple methods Example: MouseListener mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased Often the event handler is interested in only one mouseClicked What a pain! We want one method Gotta implement five ??? April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

7 Pain Reliever: Adapter Classes
Java supplies adapter classes for such interfaces. class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

8 Using the Adapter Class
Event handler can now inherit from this class just implement desired method (mouseClicked): class MyMouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent e) { // handle mouse click } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

9 The Model-View-Controller Paradigm
April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

10 Objects At Their Best— Java's Abstract Window Toolkit
A Counter Applet Two buttons Value field April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

11 The CounterApplet Class
import java.applet.*; import java.awt.*; import java.awt.event.*; public class CounterApplet extends Applet implements ActionListener { public void init() {…} public void actionPerformed(ActionEvent e) {…} Button incButton, decButton; // Instance variables TextField valField; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

12 Objects At Their Best— Java's Abstract Window Toolkit
The init Method public void init() { incButton = new Button("+"); // Create Controls decButton = new Button("-"); valField = new TextField(); valField.setText("0"); Panel p = new Panel(); // Lay them out p.setLayout(new BorderLayout()); p.add(incButton, "North"); p.add(decButton, "South"); add(p); add(valField); // Register as listener incButton.addActionListener(this); decButton.addActionListener(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

13 The actionPerformed Method
public void actionPerformed(ActionEvent e) { // Retrieve text field int val = Integer.parseInt(valField.getText()); if (e.getSource() == incButton) val++; else val--; // Update text field valField.setText(Integer.toString(val)); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

14 An Unexpected Benefit (Bug?)
Can directly modify the counter value through the TextField We’re using the contents of the TextField as our counter value. We’ll call it a feature April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

15 Objects At Their Best— Java's Abstract Window Toolkit
Concern I CounterApplet is modeling too many things: General behavior of an applet (inherited from Applet) GUI of our particular application Display of the two buttons and the text field Role as listener to the two buttons Behavior of an up/down counter April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

16 Objects At Their Best— Java's Abstract Window Toolkit
Concern II The counter logic is all over the place Initialization is in applet’s init method Maintenance logic is in actionPerformed No real counter value "maintained" indirectly as a string in a text field Makes it difficult to understand and maintain April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

17 Objects At Their Best— Java's Abstract Window Toolkit
Concern III What if we want a Scrollbar rather than a pair of Buttons a Label rather than a TextField? Requires changing the "counter" logic April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

18 Objects At Their Best— Java's Abstract Window Toolkit
Good Interface Design Separate interface from application: Interface: should not affect application logic Application: should not know about interface. April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

19 Revising the Counter Applet
Have a Counter class Encapsulate application logic of up/down counter The CounterApplet class then uses the Counter class. April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

20 Objects At Their Best— Java's Abstract Window Toolkit
The Counter Class public class Counter { public Counter(int max) {this.max = max;} public void reset() {val = 0;} public int getVal() {return val;} public void setVal(int val) { if (val >= 0 && val <= max) this.val = val; } public void inc() {if (val < max) val++;} public void dec() {if (val > 0) val--;} private int max, val=0; // Instance variables April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

21 The CounterApplet Class
import java.applet.*; import java.awt.*; import java.awt.event.*; public class CounterApplet extends Applet implements ActionListener { public void init() {…} public void actionPerformed(ActionEvent e) {…} Button incButton, decButton; // Instance variables TextField valField; Counter ctr; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

22 Objects At Their Best— Java's Abstract Window Toolkit
The init Method public void init() { ctr = new Counter(10); incButton = new Button("+"); decButton = new Button("-"); valField = new TextField(); valField.setText(Integer.toString(ctr.getVal())); Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add(incButton, "North"); p.add(decButton, "South"); add(p); add(valField); incButton.addActionListener(this); decButton.addActionListener(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

23 The actionPerformed Method
public void actionPerformed(ActionEvent e) { if (e.getSource() == incButton) ctr.inc(); else ctr.dec(); valField.setText(Integer.toString(ctr.getVal)); } The event handler for the Button invokes the appropriate counter method and updates the TextField. We no longer use the TextField for the counter value. April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

24 Objects At Their Best— Java's Abstract Window Toolkit
Better Applet, but... actionPerformed still responsible for updating the textField. What if we decide to change the TextField to a Label? A Scrollbar A gauge? April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

25 Objects At Their Best— Java's Abstract Window Toolkit
One Possible Approach We could isolate the display update logic into a helper method that can be called from anywhere. However, the CounterApplet still has to know WHEN the Counter has changed. We will try something a bit different. April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

26 Model-View-Controller (MVC)
Separates application, user input, and display logics. Makes for an exceptionally clean design Minimal coupling between interface and application April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

27 Objects At Their Best— Java's Abstract Window Toolkit
Model Application Interface-independent Examples Spreadsheet data Counter class April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

28 Objects At Their Best— Java's Abstract Window Toolkit
View Display Responsible for maintaining an updated view of the model Must be notified of changes to the model. Examples graph, matrix TextField update April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

29 Objects At Their Best— Java's Abstract Window Toolkit
Controller Input Often initiates changes to the model. Examples keyboard, mouse actionPerformed April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

30 Objects At Their Best— Java's Abstract Window Toolkit
Document-View Often easier to combine view and controller. Sometimes known as document-view. April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

31 Objects At Their Best— Java's Abstract Window Toolkit
Model as Observed Must notify view when changes happen Model must know: Who is the view? How to notify the view of the change? April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

32 Symmetric with Event Handling
View notification Who is the view? How to notify view? Event Handling Who is the listener? How to notify listener? April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

33 Objects At Their Best— Java's Abstract Window Toolkit
View as Observer Who is the view? View registers with the model How to notify view that change happened? View implements an "observer" interface April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

34 The Symmetry Continues
View notification View registers with model View implements observer interface Event Handling Listener registers with event source Listener implements listener interface April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

35 The Counter Applet Using MVC
class Counter— Model class CounterApplet — View/Controller interface CounterObserver — observer interface developed for our applet— not predefined April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

36 CounterObserver Interface
Implemented by view to ‘listen’ for model changes Single method with the Counter as a parameter. When invoked allows view to be updated. interface CounterObserver { public void counterHasChanged(Counter ctr); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

37 Counter Class Overview
Same basic functionality, plus… Maintains a CounterObserver as part of its state. Provides observer registration method registerAsObserver Notifies observer of changes Relies upon observer implementing CounterObserver April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

38 Objects At Their Best— Java's Abstract Window Toolkit
Counter Class public class Counter { public Counter(int max) {this.max = max;} public void reset() {val = 0;} public int getVal() {return val;} // Manipulator methods- // must notify observer the model has changed public void setVal(int val) {…} public void inc() {...} public void dec() {...} // Observer-related methods public void registerAsObserver(CounterObserver observer){…} private void notifyObserver() {...} private int max, val=0; CounterObserver observer; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

39 Counter’s Manipulator Methods
public void setVal(int val) { if (val >= 0 && val <= max) this.val = val; notifyObserver(); } public void inc() { if (val < max) val++; public void dec() { if (val > 0) val--; April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

40 Counter’s Observer-Related Methods
public void registerAsObserver(CounterObserver observer) { this.observer = observer; notifyObserver(); // Allow initial display } private void notifyObserver() { observer.counterHasChanged(this); April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

41 CounterApplet Class Overview
Implements CounterObserver interface (view) Registers with Counter during initialization (view) Event-handlers invoke Counter methods but do not update view. (controller) Updates TextField when counterHasChanged method is invoked. (view) April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

42 Objects At Their Best— Java's Abstract Window Toolkit
CounterApplet Class import java.applet.*; import java.awt.*; import java.awt.event.*; public class CounterApplet extends Applet implements ActionListener, CounterObserver { public void init() {…} public void actionPerformed(ActionEvent e) {…} public void counterHasChanged(Counter ctr) {…} Button incButton, decButton; TextField valField; Counter ctr; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

43 Objects At Their Best— Java's Abstract Window Toolkit
The init Method public void init() { incButton = new Button("+"); decButton = new Button("-"); valField = new TextField(); Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add(incButton, "North"); p.add(decButton, "South"); add(p); add(valField); incButton.addActionListener(this); decButton.addActionListener(this); ctr = new Counter(10); ctr.registerAsObserver(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

44 The Event-Handling Methods
public void actionPerformed(ActionEvent e) { if (e.getSource() == incButton) ctr.inc(); else ctr.dec(); } The event handler simply initiates model changes NOT responsible for the display! April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

45 The counterHasChanged Method
public void counterHasChanged(Counter ctr) { valField.setText(Integer.toString(ctrVal)); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

46 Objects At Their Best— Java's Abstract Window Toolkit
Now Let’s Have Some Fun! We’ve shown you the basics of MVC We’ve also claimed it results in a clean design Model knows nothing of controller/view’s workings All this leads to some very unexpected capabilities! April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

47 Objects At Their Best— Java's Abstract Window Toolkit
Counters... Two pairs of synchronized counters: East/West North/South April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

48 Objects At Their Best— Java's Abstract Window Toolkit
… Logs, ... A logging facility in synch with the counters April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

49 Objects At Their Best— Java's Abstract Window Toolkit
… and Alarms An alarm panel keeping track of the counter values April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

50 Objects At Their Best— Java's Abstract Window Toolkit
Class Overview Counter class: a model same basic functionality, but ... now allows multiple observers CounterPanel class: a view and controller creates our familiar counter GUI observes the counter similar structure to our previous CounterApplet class April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

51 Objects At Their Best— Java's Abstract Window Toolkit
Class Overview (II) CounterLogger: a view prints out counter changes to System.out observes the counter maintains counter name CounterAlarmPanel: a view observes multiple counters indicates movement past error threshold April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

52 Objects At Their Best— Java's Abstract Window Toolkit
Class Overview (III) CounterApplet creates the various interface components sits back and let’s them do all the work CounterObserver interface same as before April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

53 Objects At Their Best— Java's Abstract Window Toolkit
Counter Class import java.util.*; public class Counter { public Counter(int max) { this.max = max; observers = new Vector(); } public void reset() { /*Same as before*/ } public int getVal() { /*Same as before*/ } public void setVal(int val) { /*Same as before*/ } public void inc() { /*Same as before*/ } public void dec() { /*Same as before*/ } public void registerAsObserver(CounterObserver observer){…} private void notifyObservers() {…} private int max, val=0; Vector observers; // Allows multiple observers April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

54 The registerAsObserver Method
observers are added to the Vector public void registerAsObserver(CounterObserver observer) { observers.addElement(observer); observer.counterHasChanged(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

55 The notifyObservers Method
Notification involves enumerating through the Vector and notifying each observer private void notifyObservers() { Enumeration enum = observers.elements(); while (enum.hasMoreElements()) { CounterObserver observer = (CounterObserver)enum.nextElement(); observer.counterHasChanged(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

56 Objects At Their Best— Java's Abstract Window Toolkit
CounterLogger Class class CounterLogger implements CounterObserver { public CounterLogger(Counter ctr, String counterName) { this.counterName = counterName; ctr.registerAsObserver(this); // Observes counter } public void counterHasChanged(Counter ctr) { System.out.println(counterName + " changed to " + ctr.getVal()); private String counterName; // For id purposes April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

57 Objects At Their Best— Java's Abstract Window Toolkit
CounterPanel Class import java.applet.*; import java.awt.*; import java.awt.event.*; public class CounterPanel extends Panel implements ActionListener, CounterObserver { public CounterPanel(Counter ctr) {…} public void actionPerformed(ActionEvent e) {…} public void counterHasChanged(Counter ctr) {…} Button incButton, decButton; TextField valField; Counter ctr; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

58 CounterPanel Constructor
public CounterPanel(Counter ctr) { incButton = new Button("+"); decButton = new Button("-"); valField = new TextField(); Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add(incButton, "North"); p.add(decButton, "South"); add(p); add(valField); incButton.addActionListener(this); decButton.addActionListener(this); this.ctr = ctr; ctr.registerAsObserver(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

59 The Event-Handler Method
identical to before public void actionPerformed(ActionEvent e) { if (e.getSource() == incButton) ctr.inc(); else ctr.dec(); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

60 The counterHasChanged Method
Same as before public void counterHasChanged(Counter ctr) { valField.setText(Integer.toString(ctrVal)); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

61 CounterAlarmPanel Class
import java.util.*; import java.awt.*; class CounterAlarmPanel extends Panel implements CounterObserver { public CounterAlarmPanel() {counters = new Vector();} // add counter with error-threshold void add(Counter ctr, int err) {…} public void paint(Graphics g) {...} public void counterHasChanged(Counter ctr) {…} Vector counters; // counter/error-threshold pairs } Counter/error-threshold pairs maintained in helper class, CounterAlarm April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

62 Objects At Their Best— Java's Abstract Window Toolkit
The add Method Creates a CounterAlarm object from the counter/error-threshold Adds the CounterAlarm to the Vector Registers as an observer of the Counter void add(Counter ctr, int err) { counters.addElement(new CounterAlarm(ctr, err)); ctr.registerAsObserver(this); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

63 Objects At Their Best— Java's Abstract Window Toolkit
The paint Method Enumerates through the CounterAlarm Vector Paints a green circle if below the threshold; a red one otherwise public void paint(Graphics g) { Enumeration e = counters.elements(); int i = 0; while (e.hasMoreElements()) { CounterAlarm ctrAlarm = (CounterAlarm)e.nextElement(); int ctrval = ctrAlarm.ctr.getVal(); if (ctrval >= ctrAlarm.err) g.setColor(Color.red); else g.setColor(Color.green); g.fillOval(50+i*30, 30, 20, 20); g.setColor(Color.black); g.drawOval(50+i*30, 30, 20, 20); i++; } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

64 The counterHasChanged Method
Merely causes a repaint public void counterHasChanged(Counter ctr) { repaint(); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

65 Objects At Their Best— Java's Abstract Window Toolkit
CounterApplet Class import java.applet.*; import java.awt.*; public class CounterApplet extends Applet { public void init() { Counter ctr1 = new Counter(10); CounterPanel cp1 = new CounterPanel(ctr1); CounterPanel cp2 = new CounterPanel(ctr1); Counter ctr2 = new Counter(5); CounterPanel cp3 = new CounterPanel(ctr2); CounterPanel cp4 = new CounterPanel(ctr2); CounterAlarmPanel cpa = new CounterAlarmPanel(); cpa.add(ctr1, 8); cpa.add(ctr2, 4); setLayout(new BorderLayout()); add(cp1, "North"); add(cp2, "South"); add(cp3, "East"); add(cp4, "West"); add(cpa, "Center"); CounterLogger cl = new CounterLogger(ctr1, "Counter1"); CounterLogger c2 = new CounterLogger(ctr2, "Counter2"); } April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

66 Objects At Their Best— Java's Abstract Window Toolkit
CounterAlarm Class class CounterAlarm { CounterAlarm(Counter ctr, int err) { this.ctr = ctr; this.err = err; } Counter ctr; int err; April 4, 2019 Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit


Download ppt "Objects At Their Best— Java's Abstract Window Toolkit"

Similar presentations


Ads by Google