Observer/Observable: update not called

T

Timo Nentwig

Hi!

Can somebody explain, why update() is not called on notifyObservers()?


public class Observe extends Observable
{
protected class IndexObserver implements Observer
{
public void update(Observable o, Object arg)
{
System.out.println("update()");
}
}

private IndexObserver indexObserver;

public Observe()
{
addObserver(indexObserver = new IndexObserver());
}

public void go()
{
notifyObservers();
}

public static void main(String[] args)
{
(new Observe()).go();
}
}
 
T

Thomas Fritsch

Timo said:
Hi!

Can somebody explain, why update() is not called on notifyObservers()?

Because you never call setChanged() on your Observable.
See the javadoc of method Observable#notifyObservers().
public class Observe extends Observable
{
protected class IndexObserver implements Observer
{
public void update(Observable o, Object arg)
{
System.out.println("update()");
}
}

private IndexObserver indexObserver;

public Observe()
{
addObserver(indexObserver = new IndexObserver());
}

public void go()
{
notifyObservers();
}

public static void main(String[] args)
{
(new Observe()).go();
}
}


_________________________________________
T h o m a s . F r i t s c h @ o p s . d e
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top