How to throw an event in Java?

Status
Not open for further replies.

jmmv

Ars Scholae Palatinae
970
Hi all,

I'm designing a class to implement a form. I derive it from JPanel. The new class contains several JTextFields and other components that store data. I'll call this the 'inner panel'.

Then, I create a JFrame window that includes the inner panel and adds some buttons. I'll call this the 'outer panel'. The outer panel gets button events and asks the inner panel to do some things like "clear fields", "get data", etc. This works perfectly.

But I have a little problem. The outer panel (which can be several different things) must know if the inner panel state changes (for example, we add some text, change an option in the combobox, etc). And I don't know how to do this.

I've think to use events. That is, I implement in the outer panel an 'ChangeListener', that will receive inner panel produced events. But... how do I throw a ChangeEvent from inside the inner panel?

Any idea?

Thanks!

PD: I've seen some fire* functions in other components, but I don't find any way to implement in my class a fireChangeEvent for example.
 

gumbo

Ars Tribunus Angusticlavius
6,107
You must use the PropertyChangeSupport class from java.beans - JPanel may support it natively, otherwise just implement the interface and fire the event. There were some threads about this on comp.lang.java.programmer - look for ones by author:gombos - I wrote that one, and IIRC I got some good help.

I suppose you could create your own fire event - just have a method that registers listeners into a Vector/array, then the outer panel implements that interface. For your fire event, loop through the array calling the stateChanged() method or whatever of the interface - the outer panel's implementation will be called.

gubmo
 
Status
Not open for further replies.