**- Undocumented Matlab - https://undocumentedmatlab.com -**

Matlab callbacks for Java events in R2014a

Posted By Yair Altman On May 8, 2014 | __16 Comments__

Exactly five years ago, in one of this blog’s first articles, I wrote an article explaining how to trap the standard Java Swing events in Matlab ^{[1]}, in order to achieve much more extensive behavior customizability than Matlab’s standard uicontrols. Three years ago I wrote a related article showing how to trap any Java events as simple Matlab callbacks ^{[2]}, even those from custom Java classes. This worked very well over the years, until now.

Over the past few weeks, several people have commented ^{[3]} and emailed me about a problem that occurs in R2014a but not in earlier releases: Whereas Java objects in R2013b and earlier automatically exposed their public events as Matlab callbacks, they no longer do so in R2014a.

As a simple example to illustrate the point, let’s take a simple Java JButton and modify its label when the mouse moves over it, using the `mouseEntered, mouseExited`

events. In R2013b and earlier this was pretty easy:

```
% Create and display a simple JButton
jButton = javax.swing.JButton('click me');
javacomponent(jButton, [50,50,80,20], gcf);
% Set the Matlab callbacks to the JButton's events
set(jButton, 'MouseEnteredCallback', @(h,e)set(jButton,'Text','NOW !!!'))
set(jButton, 'MouseExitedCallback', @(h,e)jButton.setText('click me'))
```

(Note how I used two distinct variants to set the button’s text, using either the Matlabized

Unfortunately, in R2014a we get an error, since the

`jButton`

object no longer exposes its events as Matlab callbacks:
>> set(jButton, 'MouseEnteredCallback', @(h,e)set(jButton,'Text','NOW !!!')) The name 'MouseEnteredCallback' is not an accessible property for an instance of class 'javax.swing.JButton'.

Do not set Matlab callbacks on the so-called “naked” Java reference. Instead, wrap the reference with a Matlab * handle*:

```
>> jButton = handle(jButton, 'CallbackProperties');
hjButton =
javahandle_withcallbacks.javax.swing.JButton
% we can now set the callbacks on jButton as before
```

The simple act of using the * handle* wrapper rather than the naked Java reference also prevents some memory leaks, as I explained here

Note that the explanation above relates to all Java objects that expose events, not just for GUI ones. I used

`JButton`

merely to illustrate the point.Categories: High risk of breaking in future versions, Java, Undocumented feature

16 Comments (Open | Close)

Article printed from Undocumented Matlab: **https://undocumentedmatlab.com**

URL to article: **https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a**

URLs in this post:

[1] trap the standard Java Swing events in Matlab: **http://undocumentedmatlab.com/blog/uicontrol-callbacks**

[2] trap any Java events as simple Matlab callbacks: **http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events**

[3] commented: **http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events#comment-323345**

[4] here: **http://undocumentedmatlab.com/blog/uicontrol-callbacks#memory_leak**

[5] to avoid EDT timing issues: **http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt**

[6] Matlab-Java programming book: **http://undocumentedmatlab.com/matlab-java-book**

[7] Matlab callbacks for Java events : **https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events**

[8] Matlab callbacks for uifigure JavaScript events : **https://undocumentedmatlab.com/articles/matlab-callbacks-for-uifigure-javascript-events**

[9] Uicontrol callbacks : **https://undocumentedmatlab.com/articles/uicontrol-callbacks**

[10] Matlab-Java interface using a static control : **https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control**

[11] JMI – Java-to-Matlab Interface : **https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface**

[12] Waiting for asynchronous events : **https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events**

[13] : **https://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events**

[14] : **https://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/**

[15] : **http://www.java2s.com/Code/Java/Swing-JFC/ListeningforScrollbarValueChangesinaJScrollPaneContainer.htm**

[16] : **http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html**

[17] : **https://undocumentedmatlab.com/articles/javacomponent**

Click here to print.

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.

16 Comments To "Matlab callbacks for Java events in R2014a"

#1 CommentByDonn ShullOn May 14, 2014 @ 13:18This change “enforces” using callbacks in a way which prevents memory leaks. But since this was essentially undocumented all along this change opens up a more interesting question. Is this change part of under the hood reorganizations in preparation for HG2? Or possibly a movement towards aligning java support with MCOS and away from UDD?

#2 CommentByYair AltmanOn May 14, 2014 @ 13:25Maybe both, I don’t know…

For the record, I miss UDD’s ability to dynamically control/add class properties (

et al) in run time. It’s causing me a lot of headaches migrating some of my code to HG2.schema.prop#3 CommentBySean LynchOn September 12, 2014 @ 08:12I’ve just run the above example and it works without the update; Have MATLAB patched R2014a. my version reports 8.3.0.532 (R2014a)?

Also the EventTest example does not work/without the update:

Any pointers?

#4 CommentByYair AltmanOn September 12, 2014 @ 08:22@Sean – as I explained

^{[13]}, you need to place the relevant class folder/JAR in Matlab’s static Java classpath.#5 CommentBySean LynchOn September 12, 2014 @ 08:59Thank you, using the static path worked (but I had to use “javaclasspath.txt” in the prefdir() instead). The callback now sets, but the value looks wrong:

Your example showed:

We are nearly there.

PS: I do have a copy of your book which has been very helpful.

#6 CommentByYair AltmanOn September 13, 2014 @ 09:40This is due to the difference between running platforms and/or Matlab releases. If you’d like my assistance in helping you on your specific platform, contact me offline for a consulting proposal.

#7 CommentByDanielle DOn January 3, 2015 @ 14:59Running Matlab 2014b, I had the same problem the Sean Lynch posted:

But trying something like

demonstrates that it is working. One step at a time.

Thank you! This article helped a lot as well as the previous article:

^{[14]}.#8 CommentByDave BrownOn August 24, 2015 @ 10:49I ran into a problem from using java 7 that took a while to discover so I thought I’d share it. Hopefully it helps those with the same problem in the future.

The problem is java 7 has changed the interface to the component and it is no longer a state change event but an adjustment event.

(see

^{[15]}). It is corrected by using`hjScroll.AdjustmentValueChangedCallback`

in this example. Be on the lookout for these types of changes in the java interface.#9 CommentByMatthew HarrisonOn November 11, 2015 @ 10:30Does anyone know how to get the window events like window closing using this method?

#10 CommentByYair AltmanOn November 11, 2015 @ 12:37#11 CommentByMatthew HarrisonOn November 12, 2015 @ 14:50Is there a clean way to do it from when your loading a .jar file?

For example I have a .jar file called test.jar

I call it like jFrame=test(); after adding it to the dynamic Java classpath

but that jFrame doesnt have properties to getWindow or JavaFrame?

#12 CommentByYair AltmanOn November 12, 2015 @ 15:35JAR files contain Java classes, not Matlab figures. The entire blog here discusses Matlab.

#13 CommentByMuthukumarOn January 26, 2017 @ 08:57How do I get delimiter of the date format ?

example :

system foramt – 1/23/2017

Get Output from your script – mm dd,yyyy

I need output like – dd/mm/yyyy

Thanks in Advance

#14 CommentByYair AltmanOn January 26, 2017 @ 10:27You can either use the Java

^{[16]}class to convert the date format into your desired output format (read the Java docs online to see how to do this), or use Matlab’sanddatenumfunctions with the appropriate format specifiers (read the corresponding Matlab docs to see how to do this).datestr#15 CommentByNeeksOn January 2, 2020 @ 21:11This looks elegant. Thank you. One issue I am not able to sort out – on maximizing the figure window the location of “javax.swing.JSlider” object does not scale properly?Is there a way to specify the ‘Units’,’normalize’ property like we do in uicontrol() to maintain the scaling and location.

#16 CommentByYair AltmanOn January 3, 2020 @ 02:16The

^{[17]}returns 2 objects: a Java reference and a Matlab container. Simply set the value of theUnitsproperty of the Matlab container to ‘normalized’: