define events for self-made component
Hi,
I created a component which formed by a panel and some basic components. Now I would like to make this component to have its self event handler, so that outside dialog can add/delegate this event.
what I want to do is to pass a structure from the self-made component to dialog on every changes of the value.
Could some of you give me some hints on How to define this event?
Thanks a lot !
# 1
Check out the "observer" pattern and model your code after that. You can find a brief synopsis of it in wikipedia [url=http://en.wikipedia.org/wiki/Observer_pattern]here[/url], or even better, get the "Head First Design Patterns" book, or better still, the GOF book.
Also, emulate what Swing does w/ its events. It is essentially using the observer pattern.
# 2
Have you considered using a [url=http://java.sun.com/javase/6/docs/api/java/beans/PropertyChangeListener.html]PropertyChangeListener[/url]? Swing components already contain the wiring necessary to emit PropertyChangeEvents, which makes it much more attractive than defining your own event types.
# 4
After reading uncle alice's post, I think that you are probably better off using his (her?) recommendation, but still, read my articles, cuz knowledge of design patterns will help you immensely in the long run./Pete