[4957] in java-interest
Observable problems
daemon@ATHENA.MIT.EDU (Christopher Helck)
Wed Jan 24 10:16:08 1996
Date: Wed, 24 Jan 1996 08:54:48 -0500
From: chris@telesph.com (Christopher Helck)
To: java-interest@java.sun.com
I'm running into problems using the Observable class and the Observer interface.
I have a class (call it Foo) which I would like to be Observable (when a button
is pressed I need to tell two or more observers about it). I can't make Foo
a sub-class of Observable because Foo already is a sub-class of Panel. I can't
have Foo use Obserable (have an Observable instance as a member of Foo) because
Observable.setChanged() and Observable.clearChanged() are protected functions
and can only be called by sub-classes of Observable.
One solution is to make a class like this:
class MyObservable extends Observable {
...
public setChanged() {
super.setChanged();
}
public clearChanged() {
super.clearChanged();
}
}
And then use it inside my class like this:
class Foo extends Panel {
MyObservable myObservable;
addObserver(Observer o) {
myObservable.addObserver(o);
}
....
/*
* When something changes
*/
myObservable.setChanged();
myObservable.notifyObservers("Something Changed");
....
}
But, this strikes me as a kludge. Can anyone tell me a better method? Can
anyone explain why setChanged() and clearChanged() are protected?
Having fun,
Christopher J. Helck
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com