0

I would like to implement an application in which I hace two classes (a graphic one, and a controller one). Mi aim is to reflect the changes that happen in the controller class in the graphic one exactly when they occur. I need to do it by using the observer pattern, but I don't know how to get it...

The pretended working is the following one: - In the graphic class, I have three JPanels that I want to colour with a two-second interval between each Panle is coloured.

By now I have the following code:

----------------------Observable class--------------------------------------------

public class ImagenControl extends Observable{
private String panel1,panel2,panel3;
private int counter;
private Timer colorTimer;
private ActionListener colorListener=new ActionListener() {

    public void actionPerformed(ActionEvent ae) {
        if (counter==0){
            panel1="yellow";
            setChanged();
        }
        else if (counter==1){
            panel2="blue";
            setChanged();
        }
        else if (counter==2){
            panel3="green";
            setChanged();
        }
        if (counter==2)
            colorTimer.stop();
        else counter++;
    }
};
public void giveColor(){
    counter=0;
    colorTimer=new Timer(2000,colorListener);
    colorTimer.start();
}

---------------------------Observer class----------------------------------------

public class Imagen extends javax.swing.JFrame implements Observer{


public Imagen() {
    initComponents();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    ImagenControl ctr=new ImagenControl();
    ctr.giveColor();
}                                     

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Imagen().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
// End of variables declaration                   

@Override
public void update(Observable o, Object o1) {

}

How would you do that??

Thanks in advance!!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1454456
  • 51
  • 1
  • 7
  • In MVC, Controller is never the observed one, but the observer. Events happen either in GUI or on the model and Controller routes the updates from one to the other. – Marko Topolnik Jul 12 '12 at 18:22

1 Answers1

3

You're going to want to link your observer to your observable object. To do this, in your main pacakge (where you're creating both the Observer and Observable object) you'll have code like this:

ImageController ic = new ImageController()
Imagen obs = new Imagen()

ic.addObserver(obs)

Now in the Update method of the Observer, you implement what you want to happen when the Observable notifies it's Observers.

Finally, you can use the setChanged() method of the Observable to indicate that a something has changed in the Observable. The next time notifyObservers() is called by the Observable, all registered Observers will be told there was a change.

Please note, if setChanged() wasn't called before notifyObservers(), no observers will be notified (because there is nothing to notify them about)

Nick Rippe
  • 6,465
  • 14
  • 30