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.