Hi,
I am actually not sure if this is the right place for my question but I probably will notice if not.
I come from an ActionScript background and am now dealing with Java to which I would like to port some graphical stuff I have made in “Flash” before.
The rendering is no problem for me at all but which makes me some headache is the way events are handled in Java because I have some swing gui that interacts with my model.
For instance given a custom GUI component (which extends JPanel) is composed of two other GUI components:JLabel and JButton.
If the user clicks on the JButton instance I would like to now the state of the JLabel (from a parent component which instanciated my custom component) without nowing much about the custom component (loose coupling).
So imho it would be the best solution to forward the new state(in this case it is the String of the label) as part of the custom event.
In ActionScript I couldy easily dispatch an custom event like this:
class MyCustomComponent extends Sprite {
private const myButton : MyButton = new Button();
public function MyCustomComponent() {
myButton.addEventListener(MouseEvent.CLICK, onButtonClick);
}
private function onButtonClick(event : MouseEvent) : void {
dispatchEvent(new CustomEvent("change", "new string"));
}
}
Another GUI component could handle events of MyComponent like this:
var myCustomComponent : MyCustomComponent = new MyCustomComponent();
myCustomComponent.addEventListener("change", onCustomComponentChange);
In onCustomComponentChange I then could handle the changed state of the component.
So how is this solved “correctly” in Java?
Unfortunately it seems that I can not dispatch custom events that forward objects to the listener.
I thought about some objective c delegate solution:
myCustomComponent.delegate = myDelegate;
where the delegate needs to implement some component specific callbacks.
But this smells boilerplate.
Maybe I have missed something fundamental?
Thank you for your time, any help is appreciated