From: joe on
"Yair Altman" <altmanyDEL(a)gmailDEL.comDEL> wrote in message <hkpvha$sq0$1(a)fred.mathworks.com>...
> Wolfgang - the following works perfectly for me on R2008a, WinXP SP3:
>
> 1. EventTest.java:
> ============
>
> public class EventTest {
> private java.util.Vector data = new java.util.Vector();
>
> public synchronized void addMyTestListener(MyTestListener l) {
> data.addElement(l);
> }
>
> public synchronized void removeMyTestListener(MyTestListener l) {
> data.removeElement(l);
> }
>
> public interface MyTestListener extends java.util.EventListener {
> void MyTest(MyTestEvent e);
> }
>
> public class MyTestEvent extends java.util.EventObject {
> private static final long serialVersionUID = 1L;
> public float oldValue,newValue;
>
> MyTestEvent(Object obj, float oldValue, float newValue) {
> super(obj);
> this.oldValue = oldValue;
> this.newValue = newValue;
> }
> }
>
> public void notifyMyTest() {
> java.util.Vector dataCopy;
> synchronized(this) {
> dataCopy = (java.util.Vector)data.clone();
> }
> for (int i=0; i<dataCopy.size(); i++) {
> MyTestEvent event = new MyTestEvent(this, 0, 1);
> ((MyTestListener)dataCopy.elementAt(i)).MyTest(event);
> }
> }
> }
>
> 2. Now compile with a JDK that is compatible with your Matlab JVM (as reported by version -java) => you get EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class files.
>
> 3. Now place these 3 files in your Matlab's *static* Java classpath (as reported by javaclasspath)
>
> 4. Restart Matlab and run the the following, which show that all works ok:
>
> >> which EventTest
> EventTest is a Java method % EventTest constructor
>
> >> evt = EventTest
> evt =
> EventTest(a)16166fc
>
> >> evt.get
> Class = [ (1 by 1) java.lang.Class array]
> MyTestCallback =
> MyTestCallbackData = []
> ...
>
> >> set(evt,'MyTestCallback',@(h,e)disp(h))
> >> get(evt)
> Class = [ (1 by 1) java.lang.Class array]
> MyTestCallback = [ (1 by 1) function_handle array]
> MyTestCallbackData = []
> ...
>
> >> evt.notifyMyTest
> 0.0009765625
>
> 5. Note that there is no specific need to use handle(,'CallbackProperties'), although it also works (and is generally advisable):
>
> >> hevt = handle(evt,'CallbackProperties')
> hevt =
> javahandle_withcallbacks.EventTest
>
> >> set(hevt,'MyTestCallback',@(h,e)disp(h))
>
> >> hevt.get
> Class: [1x1 java.lang.Class]
> MyTestCallback: @(h,e)disp(h)
> MyTestCallbackData: []
>
>
> Yair Altman
> http://UndocumentedMatlab.com

Hallo and thank you for your info. I like your java code and I would like to use it. I have only small experience programming java and it is hard for me to understand your code. Would it be possible for you to comment it, or to make another simple coding. Something like: JavaEventProducerClass - class that fires event and sends for example string as eventData. And another MatlabEventListenerClass - that recieve this evet object and displays the string.

Thanks Joe
From: joe on
"joe " <b574988(a)tyldd.com> wrote in message <i0snql$sc$1(a)fred.mathworks.com>...
> "Yair Altman" <altmanyDEL(a)gmailDEL.comDEL> wrote in message <hkpvha$sq0$1(a)fred.mathworks.com>...
> > Wolfgang - the following works perfectly for me on R2008a, WinXP SP3:
> >
> > 1. EventTest.java:
> > ============
> >
> > public class EventTest {
> > private java.util.Vector data = new java.util.Vector();
> >
> > public synchronized void addMyTestListener(MyTestListener l) {
> > data.addElement(l);
> > }
> >
> > public synchronized void removeMyTestListener(MyTestListener l) {
> > data.removeElement(l);
> > }
> >
> > public interface MyTestListener extends java.util.EventListener {
> > void MyTest(MyTestEvent e);
> > }
> >
> > public class MyTestEvent extends java.util.EventObject {
> > private static final long serialVersionUID = 1L;
> > public float oldValue,newValue;
> >
> > MyTestEvent(Object obj, float oldValue, float newValue) {
> > super(obj);
> > this.oldValue = oldValue;
> > this.newValue = newValue;
> > }
> > }
> >
> > public void notifyMyTest() {
> > java.util.Vector dataCopy;
> > synchronized(this) {
> > dataCopy = (java.util.Vector)data.clone();
> > }
> > for (int i=0; i<dataCopy.size(); i++) {
> > MyTestEvent event = new MyTestEvent(this, 0, 1);
> > ((MyTestListener)dataCopy.elementAt(i)).MyTest(event);
> > }
> > }
> > }
> >
> > 2. Now compile with a JDK that is compatible with your Matlab JVM (as reported by version -java) => you get EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class files.
> >
> > 3. Now place these 3 files in your Matlab's *static* Java classpath (as reported by javaclasspath)
> >
> > 4. Restart Matlab and run the the following, which show that all works ok:
> >
> > >> which EventTest
> > EventTest is a Java method % EventTest constructor
> >
> > >> evt = EventTest
> > evt =
> > EventTest(a)16166fc
> >
> > >> evt.get
> > Class = [ (1 by 1) java.lang.Class array]
> > MyTestCallback =
> > MyTestCallbackData = []
> > ...
> >
> > >> set(evt,'MyTestCallback',@(h,e)disp(h))
> > >> get(evt)
> > Class = [ (1 by 1) java.lang.Class array]
> > MyTestCallback = [ (1 by 1) function_handle array]
> > MyTestCallbackData = []
> > ...
> >
> > >> evt.notifyMyTest
> > 0.0009765625
> >
> > 5. Note that there is no specific need to use handle(,'CallbackProperties'), although it also works (and is generally advisable):
> >
> > >> hevt = handle(evt,'CallbackProperties')
> > hevt =
> > javahandle_withcallbacks.EventTest
> >
> > >> set(hevt,'MyTestCallback',@(h,e)disp(h))
> >
> > >> hevt.get
> > Class: [1x1 java.lang.Class]
> > MyTestCallback: @(h,e)disp(h)
> > MyTestCallbackData: []
> >
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> Hallo and thank you for your info. I like your java code and I would like to use it. I have only small experience programming java and it is hard for me to understand your code. Would it be possible for you to comment it, or to make another simple coding. Something like: JavaEventProducerClass - class that fires event and sends for example string as eventData. And another MatlabEventListenerClass - that recieve this evet object and displays the string.
>
> Thanks Joe

One another question: I did all you wrote in your last post.
After running:
set(evt,'MyTestCallback',@(h,e)disp(h))

I got following error:
java.lang.NullPointerException
at com.mathworks.jmi.bean.MatlabBeanInterface.addCallback(MatlabBeanInterface.java:765)
at com.mathworks.jmi.bean.MatlabCallbackInterface.addCallback(MatlabCallbackInterface.java:128)