The problem with my code is that the update method doesn't get invoked even after I notify the observers. Did I miss something in my code?

package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class A extends JPanel implements Observer{
    JLabel lbl;
    public A(){
        setLayout(new FlowLayout());
        lbl = new JLabel("");
        add(lbl);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}


package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class B extends JPanel implements Observer{
    JLabel lbl;
    public B(){
        setLayout(new FlowLayout());
        lbl = new JLabel("");
        add(lbl);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}



package com.observer3;

import java.util.Observable;

public class C extends Observable{
    private int count=0;

    public C(int count){
        this.count = count;
        setChanged();
        notifyObservers();
    }

    public int getCount(){
        return count;
    }
}


package com.observer3;

import javax.swing.JFrame;

public class driver1 {
    public static void main(String[] args){
        JFrame f1 = new JFrame();
        A a = new A();
        f1.add(a);
        f1.setSize(200,200);
        f1.show();

        JFrame f2 = new JFrame();
        B b = new B();
        f2.add(b);
        f2.setSize(200,200);
        f2.show();
    }
}



package com.observer3;

public class driver2 {
     public static void main(String[] args){
         C c= new C(1);
     }
}

Your help is kindly appreciated.

Thank You.

I even attempted to add observer as below but it doesn't work:

package com.observer3;

public class driver2 {
     public static void main(String[] args){
         C c= new C(1);
         c.addObserver(new A());
         c.addObserver(new B());
     }
}

Calling addObserver is not optional if you want want observers to be notified of updates. Also, c.addObserver(new A()) will not accomplish much because new A() is a JPanel and that means it needs a visible container or else it cannot be visible. Since you never do anything with new A() except make it an observer of c, it is impossible for it to have a visible container.

You need to both add your A to a frame or other visible container, and add it as an observer to c. If you do just one of those two things, you won't get the result that you are hoping for.

In order to make use of being an Observable, you will also need some way for c to change over time, and notify the observers of c when each change happens. Currently it is impossible to change any object of class C because count is private and there are no methods that change count.

On line 58 you call notifyObservers, but this will do nothing because you are in the constructor of C, so it is too early for there to be any observers. Calling notifyObservers is only helpful after addObserver has been called at least once.

This works:

package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class A extends JFrame implements Observer{
    JLabel lbl;
    public A(){
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        lbl = new JLabel("test");
        p1.add(lbl);
        add(p1);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}


package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class B extends JFrame implements Observer{
    JLabel lbl;
    public B(){
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        lbl = new JLabel("test");
        p2.add(lbl);
        add(p2);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}



package com.observer3;

import java.util.Observable;

public class C extends Observable{
    private int count=0;

    public C(int count){
        this.count = count;
        setChanged();
    }

    public int getCount(){
        return count;
    }
}


package com.observer3;

import javax.swing.JFrame;

public class Driver {

    private A a;
    private B b;

    public void winA(){
        a = new A();
        a.setSize(200,200);
        a.show();
    }

    public void winB(){
        b = new B();
        b.setSize(200,200);
        b.show();
    }

    public A getWinA(){
        return a;
    }

    public B getWinB(){
        return b;
    }

    public static void main(String[] args){
        Driver d = new Driver();
        d.winA();
        d.winB();   

        C c= new C(5);
        c.addObserver(d.getWinA());
        c.addObserver(d.getWinB());
        c.notifyObservers();
    }
}

The other solution:

package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class A extends JFrame implements Observer{
    JLabel lbl;
    public A(C c){
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        lbl = new JLabel("test");
        p1.add(lbl);
        add(p1);
        c.addObserver(this);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}


package com.observer3;

import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class A extends JFrame implements Observer{
    JLabel lbl;
    public A(C c){
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        lbl = new JLabel("test");
        p1.add(lbl);
        add(p1);
        c.addObserver(this);
    }
    public void update(Observable o, Object arg) {
        C c = (C)o;
        lbl.setText(Integer.toString(c.getCount()));
    }
}


package com.observer3;

import java.util.Observable;

public class C extends Observable{
    private int count=0;

    public void setCount(int count){
        this.count = count;
        setChanged();
    }

    public int getCount(){
        return count;
    }
}


package com.observer3;

public class Driver {

    private A a;
    private B b;

    public void winA(C c){
        a = new A(c);
        a.setSize(200,200);
        a.show();
    }

    public void winB(C c){
        b = new B(c);
        b.setSize(200,200);
        b.show();
    }

    public A getWinA(){
        return a;
    }

    public B getWinB(){
        return b;
    }

    public static void main(String[] args){

        C c = new C();

        Driver d = new Driver();
        d.winA(c);
        d.winB(c);  

        c.setCount(5);
        c.notifyObservers();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.