[5070] in java-interest

home help back first fref pref prev next nref lref last post

Re: Observable problems

daemon@ATHENA.MIT.EDU (Tony Healy)
Sun Jan 28 01:53:51 1996

Date: Sun, 28 Jan 1996 16:21:24 +1100
To: chris@telesph.com, java-interest@java.Eng.Sun.COM
From: Tony Healy <thealy@magna.com.au>
Cc: Walter@stumpy.adco.com, David.Geary@Central.Sun.COM


Chris wants ... a button that can send data to one or more algorithm objects
which will be created and destroyed independently of the button. To do this,
he wants to make the buttons subclasses of Observable, but this is not
possible because they are already subclasses of Panel.

Chris, one way to do what you want is to have the buttons operate on members
which are Observables, and then have these notify the algorithm objects. I
think you mentioned this possibility, but had problems accessing the
setChanged() function of Observable. The way to do it is to define your own
subclass of Observable, with some callable function which can in turn call
Observable's setChanged() function. This gives you a completely flexible
architecture to which targets can be added and removed transparently and
safely at run time.

Stripped down code follows:

// MyApplet.java
// Demonstrates use of Observable and Observer architecture
// Tony Healy 26 Jan 96

import java.applet.Applet;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;

// Executive
// Class that carries out some task on receiving an instruction

class Executive implements Observer 
{
    public void update(Observable o,  Object arg) 
    {
        System.out.print("Executive_update(Observable o, Object arg)\n");
		
        // Do something useful here
    }
}

// Instruction
// A class that receives notifications from user interface
// elements such as panels and buttons, and issues them to
// Executives to be actioned
 
class Instruction extends Observable 
{
    public void doSomething() 
    {
        System.out.print("Instruction_doSomething\n");
        setChanged();
        notifyObservers( this );
    }
}

// MyPanel
// Class derived from Panel and able to issue instructions to
// Executive objects using Instruction objects

class MyPanel extends Panel
{
    private Instruction instruction;
    private Executive executive;

    // mouseDown
    
    public boolean mouseDown(Event event, int x, int y) 
    {
        System.out.print("MyPanel_mouseDown\n");
        if( instruction != null ) 
            instruction.doSomething();
        return true;
    }

    // Constructor
	
    public MyPanel()
    {
        super();

        instruction = new Instruction();
        executive = new Executive();
        instruction.addObserver( executive );
        setBackground( Color.orange );
        System.out.print("MyApplet_created\n");
    }    
}

// MyApplet

public class MyApplet extends Applet implements Runnable
{
    private MyPanel panel;
    private Thread thread = null;
    final int delay = 1000;
    
    // init
	
    public void init() 
    {
        setLayout( new BorderLayout());
        panel = new MyPanel();
        add("East", panel);
    }
     
    // run
    
    public void run() 
    {
        while (thread != null)
        {
            repaint();
            try{ Thread.sleep(delay); } 
            catch(Exception e) { System.out.println("Exception occurred in
run"); }
        }
        thread = null;		
    }

    // start
    
    public void start()
    {
        if (thread == null)
        {
            thread = new Thread(this);
            thread.start();
        }
    }

    // stop
    
    public void stop() { thread = null; }
}



-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post