My findjobj utility, created in 2007 and updated over the years, has received wide recognition and is employed by numerous Matlab programs, including a few dozen utilities in the Matlab File Exchange. I am quite proud of this utility and find it extremely useful for customizing Matlab controls in many ways that are impossible using standard Matlab properties. I have shown many examples of this in this blog over the past years.
I am happy to announce that I have just uploaded a new version of findjobj to the Matlab File Exchange, which significantly improves the utility’s performance for the most common use-case of a single input and a single output, namely finding the handle of the underlying Java component (peer) of a certain Matlab control:
>> hButton = uicontrol('String','click me!'); >> tic, jButton = findjobj(hButton); toc % old findjobj Elapsed time is 1.513217 seconds. >> tic, jButton = findjobj(hButton); toc % new findjobj Elapsed time is 0.029348 seconds. |
The new findjobj is backward-compatible with the old findjobj and with all prior Matlab releases. It is a drop-in replacement that will significantly improve your program’s speed.
The new version relies on several techniques:
First, as I showed last year, in HG2 (R2014 onward), Matlab uipanels have finally become full-featured Java JPanel
s, that can be accessed and customized in many interesting manners. More to the point here, we can now directly access the underlying JPanel
component handle using the uipanel‘s hidden JavaFrame property (thanks to MathWorks for supplying this useful hook!). The new findjobj version detects this and immediately returns this handle if the user specified a uipanel input.
I still do not know of any direct way to retrieve the underlying Java component’s handle for Matlab uicontrols, this has been a major frustration of mine for quite a few years. So, we need to find the containing Java container in which we will recursively search for the control’s underlying Java handle. In the old version of finjobj, we retrieve the containing figure’s JFrame
reference and from it the ContentPane
handle, and use this handle as the Java container that is recursively searched. This is quite slow when the figure window is heavily-laden with multiple controls. In the new version, we try to use the specified Matlab uicontrol‘s direct parent, which is very often a uipanel. In this case, we can directly retrieve the panel’s JPanel
reference as explained above. This results in a must smaller and faster search since we need to recursively search far fewer controls within the container, compared to the figure’s ContentPane
.
In addition, I used a suggestion by blog reader Hannes for a faster recursive search that uses the control’s tooltip rather than its size, position and class. Finally, the search order is reversed to search backward from the last child component, since this is the component that will most often contain the requested control peer.
Feel free to download and use the new findjobj version. The code for the fast variant can be found in lines #190-205 and #3375-3415.
Enjoy!
p.s. – as I explained last week, today’s discussion, and in general anything that has to do with Java peers of GUI controls, only relates to the existing JFrame
-based figure windows, not to the new web-based uifigure.
Hi Yair
First off thank you for this post, and all the other articles the are very interesting, and i have learned a lot out of it.
Today I downloaded this updated findjobj file, and when running it in Matlab R2015A it gave the following error.
For the moment I simply solved it by commenting this line, but this is maby not the best solution. So I was wondering if you have any tips to solve this? Is not a very important thing but i thought it was good to let you know.
Sjoerd
Hi Yair,
Kudos on the good work and on your meticulous maintenance. It helps a lot.
One small thing though:
findjobj
still doesn’t accept thegroot
object. For example (Matlab R2015b),Nevertheless, the old syntax still works:
Cheers
Hi Yair,
I’ve been using your excellent utility in a deployed toolbox for about a year now.
Anyway, when I updated, I know get the following error
This seems to be related to this call:
This call has worked without issue in an older version of |findjobj| (% $Revision: 1.46 $ $Date: 2015/01/12 13:54:47 $)
I’m also directed to a MW page about deprecating JFrame support.
Any thoughts would be most appreciated!
Best,
Meade
@Meade – as you can see, the error is not in the findjobj call but in your code (
jscroll.getComponent(4)
). You need to check the jscroll object that findjobj returned and see why there is no 5th component (Java indexes start from 0, so “4” means “5th”). You can do something like this:@Yair – Thanks so much for the reply.
I’ve been using your .list tip to dig into this more deeply and it seems that the new findjobj function returns a different (shorter) list of components than the older revision. This seems to be why my subsequent call for
fails.
For example:
yields:
While
yields:
I’m sure that I’m missing something obvious, but any insight you have would be greatly appreciated.
(As a final note, curiously, if I call the older version of findjobj first, then the newer version will successfully find all components. Thus, there is some sort of order dependence?! Weird!
Thanks so much,
Meade
@Meade
Not sure about this. Are your components on the EDT? Are the components visible? You might try a drawnow() and see if that order dependence disappears. Also see javaObjectEDT.
ML
@Malcolm
I do not know if they are all on EDT. I’m an absolute java novice, but from reading, I cannot say whether all components are being accessed in the same thread. This seems like a question that Yair might be able to answer since all the calls are being made within the findjobj call.
I understand this all seems stranges, but I get the errors while literally only changing the version of findjobj that is being used, so the difference must be related to that. I’m happy/willing/excited to make changes since Yair has made such dramatic speed improvements in his utility, I’m just at a loss as where to start.
Thanks for all the good suggestions.
Meade
Meade
Create your JScrollPane through a call to
>> jscroll=javaObjectEDT(‘javax.swing.JScrollPane’);
That on it’s own might(??) fix all your problems as subsequent methods calls to the scroll pane should be despatched via the EDT. If not do post your construction code. Very few Swing methods are inherently thread-safe and this does look like a treading issue.
@Malcolm,
You’ve got my attention! My situation is that the JScrollPane is actually created by the uitable command. Is it possible to tweak this matlab command in order to accomplish what you’ve suggested? I was hoping to avoid a pure java creation, since I’m more comfortable creating the gui using Matlab methods.
%(begin code)
% end code
Thanks again for all the help.
Meade
@Meade
You can call jscroll methods in the EDT if you can get a reference to it and call javaObjectEDT on the reference:
If you have problems after a javaObjectEDT you might try
Revalidate and repaint are both thread-safe and will be run on the EDT by Swing.
I am on a Mac and all seems to work fine without tweaks (and the same with the latest or older findjobj) so I can’t be much help but here’s what I see anyway:
All seems OK to me:
Hi Yair,
thanks a lot for your work!
I built a figure with some tabs. in one specific tab i set another small uitabgroup (and tabs) to show some tables.
I can’t take the uitabgroup handle from command window (empty value is returned) but I can see the “object” and
modify all his properties from findjobj window.
what is the problem? how can I continue to use findjobj from command window also in this case?
best regards
claudio