[Tip] Calling Servoy Methods from Beans Using Listeners
by Jeff Bader
Freight Logistics
Before I write too much I want to point out that the method outlined below is one which is a combination of trial and error and forum input, and may or may not be the most effective way to utilize listeners in Servoy (which, to the best of my knowledge is undocumented?). I am hoping that by posting this article, Servoy and/or some of the Servoy forum's more active members help to further clarify or demonstrate how exactly to handle listeners in the most proper way. In fact, I really feel that a "Java for Servoy Developers" whitepaper would be most beneficial to the community if there is anyone up to the task?
Servoy + Beans
Servoy's inclusion of Java beans gives Servoy developers access to a whole wide range of great UI elements and other tools. For a non-java programmer though (such as myself), they are a bit foreign, and can feel pretty disconnected from Servoy. Beans are very powerful though, and can integrate nicely within your solutions if you know how to use them.
In my case, I wanted a specific bean, the JList bean to integrate tightly with my solution by firing a servoy method each time a selection was made from the JList's "defaultListModel" (AKA list of values). What I found out was if I wanted my JList bean and my Servoy solution to play nice I needed to use a "listener."
Listener Basics
A listener "listens" for some event to occur and allows you to programatically do something when it happens. Different types of listeners can be added to different types of Beans. For example, a JList bean can have a "listSelectionListener" added to it, while a JInternalFrame can have an "internalFrameListener" added to it etc. Different listeners have different events that they listen for, so an event for a listSelectionListener and an internalFrameListener will be different. I have only worked with JLists and their associated listSelectionListeners, so that is what is demonstrated below.
Step 1: Creating a listSelctionListener
Before we can attach a listener to a bean, we need to create it. The amount of code it takes to do this is minimal.
var listener = new Packages.javax.swing.event.ListSelectionListener({ valueChanged: myServoyCallbackMethod });
In the above code, we are creating a new listSelectionListener and specifying the event that will fire the callback (valueChanged), and what callback to fire when the event is triggered (myServoyCallbackMethod). Once we do this, the listener is ready to be added to a bean.
Step 2: Adding a listener to a bean
Assuming you have a JList bean on your form, the following code adds the listener we just created to the JList...
elements.myJList.addListSelectionListener(listener);
So...believe it or not, that's it. We have created the listener, added it to a JList and asked that listener to fire a Servoy callback method whenever the valueChanged* event occurs. Not much to it.
*Re: "valueChanged"
If you use "valueChanged" your callback method will fire for any change of state on the JList.
Comments (11)