java.util

Class Observable


public class Observable
extends Object

This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.

An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.

The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.

Note that this notification mechanism is has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object.

When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equals method returns true for them.

Since:
JDK1.0
See Also:
notifyObservers(), notifyObservers(Object), Observer, Observer.update(Observable,Object)

Constructor Summary

Observable()
Construct an Observable with zero Observers.

Method Summary

void
addObserver(Observer o)
Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set.
protected void
clearChanged()
Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged method will now return false.
int
countObservers()
Returns the number of observers of this Observable object.
void
deleteObserver(Observer o)
Deletes an observer from the set of observers of this object.
void
deleteObservers()
Clears the observer list so that this object no longer has any observers.
boolean
hasChanged()
Tests if this object has changed.
void
notifyObservers()
If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.
void
notifyObservers(Object arg)
If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.
protected void
setChanged()
Marks this Observable object as having been changed; the hasChanged method will now return true.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Observable

public Observable()
Construct an Observable with zero Observers.
Usages and Demos :

View More Examples of Observable()
   1: 
   2:   Observable o = new Observable() {
   3:     public void notifyObservers(Object arg) { setChanged(); super.notifyObservers(arg); }
   4:   };

View Full Code Here
   1: 
   2:     Observable observable() {
   3:         public void notifyObservers() {
   4:             setChanged();
   5:             super.notifyObservers();

View Full Code Here
   1:   private static Observable observable =
   2:     new Observable() {
   3:       public void notifyObservers(Object arg) {
   4:         setChanged();
   5:         super.notifyObservers(arg);

View Full Code Here
   1:   private static Observable observable =
   2:     new Observable() {
   3:       public void notifyObservers(Object arg) {
   4:         setChanged();
   5:         super.notifyObservers(arg);

View Full Code Here

Method Details

addObserver

public void addObserver(Observer o)
Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. The order in which notifications will be delivered to multiple observers is not specified. See the class comment.
Parameters:
o - an observer to be added.
Throws:
NullPointerException - if the parameter o is null.
Usages and Demos :

View More Examples of addObserver(Observer o)
   1: import java.awt.Canvas;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: {
   6:   Observable notifier;
   7:   Color cColor = newColor();
   8:         ...
   9:   }
  10:   OCBox(int x, int y, Observable notifier) {
  11:     this.x = x;
  12:         ...
  13:     this.y = y;
  14:     notifier.addObserver(this);

View Full Code Here
   1: 
   2: class BoxObservable extends Observable {
   3:   public void notifyObservers(Object b) {
   4:         ...
   5: public class BoxObserver extends Frame {
   6:   Observable notifier = new BoxObservable();
   7:   public BoxObserver(int grid) {
   8:         ...
   9: class OCBox extends Canvas implements Observer {
  10:   Observable notifier;
  11:   Color cColor = newColor();
  12:         ...
  13:     this.y = y;
  14:     notifier.addObserver(this);

View Full Code Here
   1: import java.util.Hashtable;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: 
   6:     public LogTextPanel(Observable model) {
   7:         ...
   8:         model.addObserver(this);
   9:         
  10:         ...
  11:     
  12:     public void update(Observable o, Object arg) {

View Full Code Here
   1: import java.util.Hashtable;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: 
   6:   private static Observable observable =
   7:     new Observable() {
   8:         ...
   9:     if (observable != null) {
  10:       observable.addObserver(obs);
  11:     }

View Full Code Here
   1:             acquiredObjects.put(address, object);
   2:             object.addObserver(this);
   3:         }
   4:         ...
   5:     
   6:     public void update(Observable object, Object arg) {
   7:         StoredObject storedObject = (StoredObject)object;
   8:         modifiedObjects.put(storedObject.getAddress(), storedObject);
   9:     }

View Full Code Here

clearChanged

protected void clearChanged()
Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged method will now return false. This method is called automatically by the notifyObservers methods.

countObservers

public int countObservers()
Returns the number of observers of this Observable object.
Returns:
the number of observers of this object.
Usages and Demos :

View More Examples of countObservers()
   1: import java.util.Map;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5:                 final Map<AEPLinkedKey, Object> sl = storeLinked;
   6: logger.log("[DataSourceBean notifying observers of change in exhibit data set, count: " + obs.countObservers() + ", map entries: "+((sl==null)?-1:sl.size())+".]");
   7: 
   8:         ...
   9: 
  10:     private static final class EDVHObservable extends Observable
  11:         {
  12:         ...
  13: 
  14:             final Observable obs = this;

View Full Code Here

deleteObserver

public void deleteObserver(Observer o)
Deletes an observer from the set of observers of this object. Passing null to this method will have no effect.
Parameters:
o - the observer to be deleted.
Usages and Demos :

View More Examples of deleteObserver(Observer o)
   1: 
   2: import java.util.Observable;
   3: 
   4:         ...
   5: 
   6:     public void update(Observable obj, Object arg) {
   7: 
   8:         ...
   9:             || arg.equals(identity)) {
  10:             obj.deleteObserver(this);
  11:         }

View Full Code Here
   1: 
   2: import java.util.Observable;
   3: 
   4:         ...
   5: 
   6:     public void update(Observable obj, Object arg) {
   7:         if (SanityManager.DEBUG) {
   8:         ...
   9: 
  10:             obj.deleteObserver(this);
  11:         }

View Full Code Here
   1: 
   2: import java.util.Observable;
   3: 
   4:         ...
   5: 
   6:     public void update(Observable obj, Object arg) {
   7:         if (SanityManager.DEBUG) {
   8:         ...
   9:             || arg.equals(identity)) {
  10:             obj.deleteObserver(this);
  11:         }

View Full Code Here
   1: import java.util.Hashtable;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: 
   6:   private static Observable observable =
   7:     new Observable() {
   8:         ...
   9:     if (observable != null) {
  10:       observable.deleteObserver(obs);
  11:     }

View Full Code Here
   1:         if (object.hasReferences()) return;
   2:         object.deleteObserver(this);
   3:         acquiredObjects.remove(object.getAddress());
   4:         ...
   5:     
   6:     public void update(Observable object, Object arg) {
   7:         StoredObject storedObject = (StoredObject)object;
   8:         modifiedObjects.put(storedObject.getAddress(), storedObject);
   9:     }

View Full Code Here

deleteObservers

public void deleteObservers()
Clears the observer list so that this object no longer has any observers.

hasChanged

public boolean hasChanged()
Tests if this object has changed.
Returns:
true if and only if the setChanged method has been called more recently than the clearChanged method on this object; false otherwise.

notifyObservers

public void notifyObservers()
If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

Each observer has its update method called with two arguments: this observable object and null. In other words, this method is equivalent to:

notifyObservers(null)
Usages and Demos :

View More Examples of notifyObservers()
   1:             StoredObject object = (StoredObject) z.next();
   2:             object.notifyObservers();
   3:         }
   4:         ...
   5:     
   6:     public void update(Observable object, Object arg) {
   7:         StoredObject storedObject = (StoredObject)object;
   8:         modifiedObjects.put(storedObject.getAddress(), storedObject);
   9:     }

View Full Code Here

notifyObservers

public void notifyObservers(Object arg)
If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

Each observer has its update method called with two arguments: this observable object and the arg argument.

Parameters:
arg - any object.
Usages and Demos :

View More Examples of notifyObservers(Object arg)
   1: import java.util.TimerTask;
   2: import java.util.Observable;
   3: 
   4:         ...
   5: 
   6:   Observable o = new Observable() {
   7:         ...
   8:     public void notifyObservers(Object arg) { setChanged(); super.notifyObservers(arg); }
   9:   };
  10:         ...
  11:     state = (state==Boolean.TRUE? Boolean.FALSE: Boolean.TRUE);
  12:     o.notifyObservers(state);

View Full Code Here
   1: import java.awt.Canvas;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: {
   6:   Observable notifier;
   7:   Color cColor = newColor();
   8:         ...
   9:   }
  10:   OCBox(int x, int y, Observable notifier) {
  11:     this.x = x;
  12:         ...
  13:     public void mousePressed(MouseEvent e) {
  14:       notifier.notifyObservers(OCBox.this);

View Full Code Here
   1: 
   2: class BoxObservable extends Observable {
   3:   public void notifyObservers(Object b) {
   4:         ...
   5:     setChanged();
   6:     super.notifyObservers(b);
   7:   }
   8:         ...
   9: public class BoxObserver extends Frame {
  10:   Observable notifier = new BoxObservable();
  11:   public BoxObserver(int grid) {
  12:         ...
  13: class OCBox extends Canvas implements Observer {
  14:   Observable notifier;

View Full Code Here
   1: 
   2:     Observable observable() {
   3:         public void notifyObservers() {
   4:         ...
   5:             setChanged();
   6:             super.notifyObservers(arg);
   7:         }
   8:         ...
   9:     }
  10:     return observable;
  11:     }
  12:         ...
  13:     if (observable != null && oldPath != null)
  14:         observable.notifyObservers(oldPath);

View Full Code Here
   1: import java.util.Hashtable;
   2: import java.util.Observable;
   3: import java.util.Observer;
   4:         ...
   5: 
   6:   private static Observable observable =
   7:     new Observable() {
   8:         ...
   9:         setChanged();
  10:         super.notifyObservers(arg);
  11:       }
  12:         ...
  13:     }
  14:     observable.notifyObservers(event);

View Full Code Here

setChanged

protected void setChanged()
Marks this Observable object as having been changed; the hasChanged method will now return true.