tags:

views:

45

answers:

2

Hello all,

I know how to use an ActionListener class to pick up an actionPerformed event - however, I've implemented my own JPanel object and something I can't work out is how I create an action and indicate it has been performed such that an external action listener can pick up on it. Specifically, I wish to intercept an action of an internal component, transform its getSelectedValue() and store this into a local variable available via a getter. I then want to indicate the change such that an external class can pick up on this and read the value and act upon it.

Any ideas? I imagine this is quite straightforward but I can't find what I'm looking for on Google.

+2  A: 

You could use the observer pattern by extending Observable and making calls to the setChanged and notifyObservers methods.

dbyrne
+1 from me, that looks like a very good solution - I'll hang around and see if anyone can explain how to do it such that it triggers an actionlistener.
Ninefingers
Many times it's hard to apply the Observer pattern with Swing components because usually your custom classes already extend some existing Swing component so they cannot also extend Observable.
Shakedown
@Shakedown: Alternatively, one can delegate to an instance of `Observable`.
trashgod
+2  A: 

See Component.dispatchEvent to dispatch event types that are already defined on your component. If you add new event types, this may require a little more work. See how AbstractButton ties into JComponent's listenerList when adding the ActionListener options.

Michael Brewer-Davis
There's a nice overview of the approach in the `EventListenerList ` API, http://download.oracle.com/javase/6/docs/api/javax/swing/event/EventListenerList.html
trashgod