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

Faster findjobj

Posted By Yair Altman On April 11, 2016 | 11 Comments

My findjobj utility [3], 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 [4] 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 [5], 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 [6], in HG2 (R2014 onward), Matlab uipanels have finally become full-featured Java JPanels, 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 [7] 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.

Categories: GUI, Handle graphics, High risk of breaking in future versions, Java, UI controls


11 Comments (Open | Close)

11 Comments To "Faster findjobj"

#1 Comment By Sjoerd On April 18, 2016 @ 12:17 pm

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.

>> findjobj
findjobj: Number of elements in right hand side does not match number required
by left hand side
 
Error in findjobj (line 278)
            handles  (dataLen+1:end) = [];

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

#2 Comment By Yaroslav On April 18, 2016 @ 7:25 pm

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 the groot object. For example (Matlab R2015b),

>> findjobj(groot)
findjobj: The following error occurred converting from matlab.ui.Root to handle:
Conversion to handle from matlab.ui.Root is not possible.
 
Error in findjobj/traverseContainer (line 523)
        handles(thisIdx) = handle(jcontainer,'callbackproperties');
 
Error in findjobj (line 277)
            traverseContainer(container,0,1);

Nevertheless, the old syntax still works:

>> findjobj(0)
 
ans =
 
	handle: 1-by-1170

Cheers

#3 Comment By Meade On August 29, 2016 @ 11:29 pm

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

Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: No such child: 4
	at java.awt.Container.getComponent(Unknown Source)

This seems to be related to this call:

jscroll = findjobj(MyUitable)  %get jScroll UI element under Matlab table
rhv = jscroll.getComponent(4);

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

#4 Comment By Yair Altman On September 6, 2016 @ 7:21 pm

@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:

>> jscroll = findjobj(MyUitable)
>> jscroll.list

#5 Comment By Meade On September 22, 2016 @ 4:21 pm

@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

jscroll.getComponent(4);

fails.
For example:

jscroll = findobj(info);
jscroll.list

yields:

com.mathworks.hg.peer.utils.UIScrollPane[, 0, 0, 0x0, invalid, ..., viewportBorder = ]
javax.swing.JViewport[, 0, 0, 0x0, invalid, ..., scrollUnderway = false]
com.mathworks.hg.peer.ui.UITablePeer$23[hInfoTable, 0, 0, 0x0, invalid, ..., showHorizontalLines = true, showVerticalLines = true]
javax.swing.CellRendererPane[, 0, 0, 0x0, invalid, hidden]
com.mathworks.hg.peer.utils.UIScrollPane$1[, 0, 0, 0x0, invalid, ..., blockIncrement = 10, orientation = VERTICAL, unitIncrement = 1]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]
com.mathworks.hg.peer.utils.UIScrollPane$2[, 0, 0, 0x0, invalid, ..., blockIncrement = 10, orientation = HORIZONTAL, unitIncrement = 1]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]

While

jscrollOLD = findobjOLD(info);
jscrollOLD.list

yields:

com.mathworks.hg.peer.utils.UIScrollPane[, 0, 0, 500 x140, invalid, ..., viewportBorder = ]
javax.swing.JViewport[, 375, 1, 124 x121, invalid, ..., scrollUnderway = false]
com.mathworks.hg.peer.ui.UITablePeer$23[hInfoTable, 0, 0, 397 x120, invalid, ..., showHorizontalLines = true, showVerticalLines = true]
javax.swing.CellRendererPane[, 0, 0, 0x0, invalid, hidden]
com.mathworks.hg.peer.utils.UIScrollPane$1[, 0, 0, 0x0, invalid, hidden, ..., blockIncrement = 10, orientation = VERTICAL, unitIncrement = 1]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 0x0, invalid, ..., defaultCapable = true]
com.mathworks.hg.peer.utils.UIScrollPane$2[, 375, 122, 124 x17, invalid, ..., blockIncrement = 10, orientation = HORIZONTAL, unitIncrement = 1]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 107, 0, 17 x17, invalid, ..., defaultCapable = true]
com.sun.java.swing.plaf.windows.WindowsScrollBarUI$WindowsArrowButton[, 0, 0, 17 x17, invalid, ..., defaultCapable = true]
javax.swing.JViewport[, 375, 1, 124 x0, invalid, ..., scrollUnderway = false]
javax.swing.JLabel[, 1, 1, 374 x0, invalid, ..., verticalAlignment = CENTER, verticalTextPosition = CENTER]

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

#6 Comment By Malcolm Lidierth On September 23, 2016 @ 2:46 am

@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

#7 Comment By Meade On September 23, 2016 @ 5:33 pm

@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

#8 Comment By Malcolm Lidierth On September 23, 2016 @ 10:02 pm

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.

#9 Comment By Meade On September 26, 2016 @ 11:26 pm

@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)

info = uitable('Parent',hLeft,...
    'Data',cell(5,1),...
    'ColumnName', [],...
    'RowName',infoCell,...
    'RowStriping','off',...
    'ColumnWidth', {397},...
    'ColumnFormat', columnformat,...
    'ColumnEditable', false,...
    'Position',[30, 10, 500, 140 ],...
    'FontSize',10,...
    'TooltipString',TTstr.TSVinfo,...
    'BackgroundColor',[0.94 0.94 0.94],...
    'Tag','hInfoTable');
 
%%% Java voodoo for Info table
jscroll=findjobj(info);

% end code

Thanks again for all the help.
Meade

#10 Comment By Malcolm Lidierth On September 27, 2016 @ 2:55 am

@Meade

You can call jscroll methods in the EDT if you can get a reference to it and call javaObjectEDT on the reference:

jscroll=javaObjectEDT(jscroll)

If you have problems after a javaObjectEDT you might try

jscroll.revalidate();% Does layout
jscroll.repaint();% Paints component
drawnow()

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:

>> info = uitable('Parent',figure,...
    'Data',cell(5,1),...
    'ColumnName', [],...
    'RowName',{'1', '2', '3','4','5'},...
    'RowStriping','off',...
    'ColumnWidth', {397},...
    'ColumnEditable', false,...
    'Position',[30, 10, 500, 140 ],...
    'FontSize',10,...
    'BackgroundColor',[0.94 0.94 0.94],...
    'Tag','hInfoTable');
>> jscroll=findjobj(info)
 
jscroll =javahandle_withcallbacks.com.mathworks.hg.peer.utils.UIScrollPane

All seems OK to me:

>> jscroll.getComponents()
 
ans =
 
  java.awt.Component[]:
 
    [1×1 javax.swing.JViewport                     ]
    [1×1 com.mathworks.hg.peer.utils.UIScrollPane$1]
    [1×1 com.mathworks.hg.peer.utils.UIScrollPane$2]
    [1×1 javax.swing.JViewport                     ]
    [1×1 javax.swing.JViewport                     ]
    [1×1 javax.swing.JList                         ]

#11 Comment By claudio On June 2, 2017 @ 11:25 pm

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


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

URL to article: https://undocumentedmatlab.com/blog/faster-findjobj

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] findjobj utility: https://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object

[4] many examples of this: https://undocumentedmatlab.com/?s=findjobj

[5] Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects

[6] showed last year: https://undocumentedmatlab.com/blog/customizing-matlab-uipanels

[7] suggestion: https://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy#comment-332487

[8] FindJObj GUI – display container hierarchy : https://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy

[9] Matlab and the Event Dispatch Thread (EDT) : https://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt

[10] Minimize/maximize figure window : https://undocumentedmatlab.com/blog/minimize-maximize-figure-window

[11] Uitable sorting : https://undocumentedmatlab.com/blog/uitable-sorting

[12] IP address input control : https://undocumentedmatlab.com/blog/ip-address-input-control

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