Communication between two classes, both ways.
Hi there,
I have been programming in actionscript3 ( adobe flash ) for quite a while now, and i wanted to rewrite some simple programs i made there, but this time in Java. However, i came upon this... well, problem :) When trying to communicate between two classes, in flash i could just dispatch events in sub classes, and listen for them in the top classes. In Pseudo-code, here is what i am trying to do.
Code:
ClassA{
public ClassB b = new ClassB();
b.addListener("ListenForThis",functionToCall);
b.doThisFunction();
private void functionToCall(Event e){
System.out.println("ClassB Dispatched the event");
}
}
ClassB{
private void doThisFunction(){
dispatchEvent("ListenForThis");
}
}
I hope someone understands my problem and is able to explain me how to get classes to 'communicate' from bottom to top.
Greetings,
Jelmer
Re: Communication between two classes, both ways.
Are you writing a GUI? If so there are Listeners available in the Swing classes. If not then it sounds like you want to look up "call back methods".
Re: Communication between two classes, both ways.
yes! You are brilliant :) The Callback Methods was exactly what i needed, thanks so much for the quick reply :)