FindJObj – find a Matlab component’s underlying Java object

In a previous post, I explained that all Matlab GUI (except the axes plotting engine) is based on Java components, and showed how we can use this information to display HTML contents in Matlab uicontrols. In other posts, I have shown how a utility called findjobj can be used to access the underlying Java components to enable customizations that are unavailable in standard Matlab: setting the line location in an edit-box, customizing button appearance, setting uicontrol callbacks, or setting list-box mouse actions. I have also shown how findjobj can be used to display the component hierarchy of complex Matlab containers such as the figure window, GUIDE or the Editor.

The time is therefore well overdue for a formal introduction of findjobj, explaining its uses and internal mechanism. Of course, readers are welcome to continue using findjobj as a black-box utility, but I think important insight can be gained from understanding its inner details. Findjobj‘s code is available for free download on the MathWorks File Exchange. It is one of my favorite submissions and is apparently well-liked by users, being highly reviewed and highly downloaded.

Findjobj has two main purposes:

  1. Find the underlying Java object reference of a given Matlab handle – Historically this was the original purpose, hence the utility’s name. Findjobj was meant to extend Matlab’s standard findobj function, which does not expose Java components.
  2. Display a container’s internal components hierarchy in a graphical user interface, to facilitate visualization of complex containers. This was later extended to also display and allow modification of the sub-components’ properties and callbacks.

Today I will focus on the first (programmatic) aspect; next week I will describe the second (GUI) aspect.

Findjobj‘s heart is finding a control’s underlying Java handle. Unfortunately, this is not exposed by Matlab except in very rare cases. As hard as I tried, I could not find a way to directly access the underlying Java-peer handle. I therefore resorted to getting the control’s enclosing Java frame (window) reference, and then working down its sub-components hierarchy until finding the Java object(s) which satisfy the position and/or class criteria. To get the enclosing Java frame (aka TopLevelAncestor), I use the Matlab figure’s undocumented JavaFrame property. Using this property issues a standard warning (since Matlab release R2008a) of becoming obsolete in some future Matlab release. Since it worked so far, I have turned off this warning in findjobj‘s code, but note that this code may well fail in some future Matlab version. If and when JavaFrame does become obsolete, be sure to look in this blog for workarounds…

Traversing the frame’s hierarchy presents several challenges: Main-menu items are accessed using different functions than other Swing components or sub-containers, and are not automatically accessible until first displayed. I have overcome this latter challenge by simulating a menu-open action in case menus should be searched (this is off by default since it takes several seconds and also changes the GUI focus). For “regular” sub-containers, sometimes we need to loop over getComponent(…) and in some other cases over getChildAt(…).

Another challenge was presented by the fact that Java positions start at (0,0) in the top left corner increasing rightward and downward, rather than starting at (1,1) in the bottom left and increasing upward as in Matlab. Moreover, Java positions are always pixel-based and relative to their parent container, which is different from Matlab (if the Matlab units is ‘pixels’ then the value is absolute; if ‘normalized’ then it returns a non-pixel value). To further complicate matters, some Matlab controls have a different size than their Java counterparts: some controls have a 5-pixel margins while others not, some controls are shifted by a pixel or two from their container’s border (for a total offset of up to 7 pixels), while some controls (such as popup-menus) have an entirely different reported size. In theory, we could use the Matlab component’s undocumented PixelBounds property (much faster than getpixelposition), but unfortunately PixelBounds turns out to be unreliable and returns erroneous values in many cases. Finally, different Java containers/components have different ways of returning their position: for some it is a getLocation() method, for others it is getX()/getY() and for others it is the X and Y properties (that sometimes have no corresponding getX()/getY() accessor methods!).

Having finally overcome all these challenges (and quite a few smaller ones, documented within the source code), I have wrapped the algorithm in a function interface that tries to emulate findobj‘s. Using findjobj can now be as easy as:

% Modify the mouse cursor when over the button
hButton = uicontrol('string','click me!');
jButton = findjobj(hButton);

Modified uicontrol cursor - a Java property

Modified uicontrol cursor - a Java property

…or as complex as:

% Find all non-button controls with the specified label
jControls = findjobj('property',{'text','click me!'}, 'not','class','button');

Space here is limited and findjobj is over 2500 lines long, so I have obviously not covered everything. I encourage you to download the utility and explore the code, and I gladly welcome your feedback.

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

Tags: , , , , , ,

Bookmark and SharePrint Print

69 Responses to FindJObj – find a Matlab component’s underlying Java object

  1. Peter says:

    Dear Yair

    Thank you very much for your great, great website. One really gets a feel for what is going on behind the scens in matlab.

    I have a few question related to the findobj application:

    You say that everything except the Java plotting engine is based on Java. I would like though to use the panelling from findobj in an application with for insatnce a matlab plot in one of the panels. Does your comment mean that I will be unsuccesful with that? If not, can you please give me a quick hint on how to get this?

    Second, when you publish fíndobj in “High risk of breaking…” what is the risk then? Does it include the panels feature that I am after or do you rather think of the more programmatic part of it?

    Thank you very much in advance!


    • Thanks Peter.

      Unfortunately, you cannot embed a Matlab plot axis in a Java container (only Java components in a Matlab container). However, to solve this problem I have created the UISplitPane utility that behaves exactly like a Java JSplitPane, and enables interactively-resizable panels with Matlab plots:

      Two levels of UISplitPane, with customized dividers

      Two levels of UISplitPane, with customized dividers

      As for your second question, I indicated “High risk of breaking in a future version” because FindJObj is highly dependent on internal Matlab implementation, which may break without warning between Matlab releases. For example, the very next release (R2010a) might remove the JavaFrame property, on which findjobj relies. Of course, since I maintain this utility I will try to bypass the problem and upload a new version (and findjobj has an automated mechanism to detect and suggest this new version), but I cannot promise anything concrete. Some other undocumented features I believe to be less prone to breaking in future releases, and so they get a less severe warning. Note that I base the severity on gut instinct and calculated guesses – I have no official access to Matlab’s development roadmap.

    • Peter says:

      Thank you very much Yair

      I will have a look at the UIsplitpane utility. It seems more appropiate for my use.

      Thanks again. It is a real cool site you have!


  2. Daniel says:

    Your program is great, but I found some trouble using it. It seems like it only works with controls created by code. I tried to find underlying java object for controls created with GUIDE, and findjobj always returned empty handles arrays. it’s that posible? is there another way to find a JTextbox behind a uicontrol created with GUIDE?


    • @Daniel – It works for me…
      Perhaps you’re calling findjobj in your _OpeningFcn() instead of in your _OutputFcn() or in callbacks? This would explain the problem, since the GUI has not yet rendered onscreen in _OpeningFcn() (the figure is still invisible), so findjobj cannot find the Java handle.

    • Daniel says:

      Yair, you’re right. I was calling it in my OpenFcn() method. My purpose was set a InputVerifier to an edit control. Is there another callback in wich i can do that?

      Thank you very much!

    • @Daniel – as I said, simply attach your InputVerifier in the _OutputFcn() instead of in the _OpeningFcn(). Both of these functions are automatically created by GUIDE: _OpeningFcn() is invoked before the figure is made visible and the Java peers are created; _OutputFcn() is invoked immediately after it’s made visible, when the peers are in place.

      I would be happy if you added a comment with your InputVerifier code, for the benefit of other readers. If the code is long, email me (altmany at gmail) and I’ll add a separate post about it.

    • Daniel says:

      Yair, i moved my code to the _OutputFcn, but findjobj still returns empty arrays. I couldn’t find out what is my error.
      i’ve written a simple code which replaces uicontrols with javax.swing.JTextFields:

      function [ output_args ] = ReemplazarControles(hObject)
      %UNTITLED2 Summary of this function goes here
      %   Detailed explanation goes here
          children = get(hObject,'Children');
          nSize = size(children);
          for i=1:nSize
              hControl = children(i);
              if isa(handle(hControl),'uicontrol') 
                  cStyle = get(hControl,'Style');
                  if strcmp(cStyle,'edit')==1
                      sPosition = get(hControl,'Position');
                      sUnits = get(hControl,'Units');
                      sParent = get(hControl,'Parent');
                      [text textcontainer] = javacomponent('javax.swing.JTextField',[0 0 100 100], hObject);
              if isa(handle(hControl),'uipanel')

      it works fine, but JTextFields not response Tab key strokes.
      I could use both methods. I don’t know wich is better.
      I deeply appreciate your help

  3. Peter says:

    Dear Yair

    I am sorry for this kind of java newbie question, but I am back using the panes from the findobj. Now, instead of including a figure (as I asked about above) in one of the panes I would like to put a uitable or preferably your createtable from the FEX. I have tried many approaches, but none of them successful. I would be very happy if you have any directions on how to do it or have an example.

    Thank you very much in advance!

    Best regards, Peter

  4. Pingback: Continuous slider callback | Undocumented Matlab

  5. Pingback: Tabbed panes – uitab and relatives | Undocumented Matlab

  6. Pingback: GUI integrated HTML panel | Undocumented Matlab

  7. Pingback: Customizing uicontrol border | Undocumented Matlab

  8. Sherif Elnabarawy says:

    Dear Yair , i am using findjobj to get the java handle of uitable to apply some java methods on it , but unfortunately using findjobj makes the uitabs and uitabgroups stop responding ! i can’t understand what’s going on but i hope that i am missing something so that i can fix and continue using it .
    this is code sample to show what i am talking about , try and feedback me .
    thanks a lot.

    uitab('v0', findobj('Tag','view_tabgroup'), 'title', 'System Data','Tag','system_tab');
    uitab('v0',findobj('Tag','view_tabgroup'), 'title', 'Network','Tag','network_tab');
    • @Sherif – it works ok for me on WinXP R2010b. Perhaps you forgot a call to drawnow before using findjobj, or perhaps you’re using an old version of findjobj.

    • Sherif Elnabarawy says:

      i am using WinXP R2009a , tabs is working fine until i call findjobj :S , my findjobj version is updated , called drawnow before and after findjobj , but nothing changed .
      i don’t know if the matlab version is the problem or there is something else .
      i’ll try anyway .

  9. Pingback: Tri-state checkbox | Undocumented Matlab

  10. Pingback: HG2 update | Undocumented Matlab

  11. Pingback: Variables Editor scrolling | Undocumented Matlab

  12. Pingback: Rich-contents log panel | Undocumented Matlab

  13. Pingback: Editbox data input validation | Undocumented Matlab

  14. Pingback: Editable combo-box | Undocumented Matlab

  15. Pingback: Using JIDE combo-boxes | Undocumented Matlab

  16. Pingback: Listbox layout customization | Undocumented Matlab

  17. Ruthy says:


    The function doesn’t work for matlab release 2013b.
    Do you know how it will be possible to adjust it to work in this release?


    • @Ruthy – The findjobj function works perfectly well on R2013b. It is your specific situation that perhaps doesn’t work, so I would suggest that you carefully check your code.

  18. Pingback: Button customization | Undocumented Matlab

  19. Christy says:

    Hey Mr. Altman,
    i have a question. I do create objects on the living program. That means for example i have uitabs and with every new uitab i add i add an editbox. Now on the first two editboxes i have “” directly but on the third box i get a 1-by-4 handle. How can i ask which one is the one i need?

  20. Pingback: checkClass | Undocumented Matlab

  21. Pingback: Matlab GUI: Numeric input for serious work | Notes

  22. Kathi says:

    Dear Yair,
    I have a question concerning invisible figures. As you already explained (to Daniel, January 2010),
    findjobj will not find any underlying jaya components while the figure is still invisible.

    However, I have a GUIDE created GUI and I need to call it, telling him to stay invisible: MyGUI(‘Visible’,’off’)

    Naturally, I get a warning:
    Warning: No Java reference was found for the requested handle, because
    the figure is still invisible in MyGUI_OutputFcn()

    Is there a way to find the java while the figure is still invisible?
    Moreover, the code is working: I got a sortable table with single row selection.

    Thank you very much,
    And thank you for your wonderful web site.

    Best regards,

  23. Mohsen says:

    Hi Yair,

    I’ve used findjobj to align the row headers in my uitable. The problem is when I try to change the dimension of my table it does not allow the new row names to be visible.
    for example, if I want to increase the row numbers from 5 to 10, the last 5 rows do not get visible!


  24. Sergey Kucheryavskiy says:

    Dear Yair,

    I am trying to get GUI layout toolbox ( working with the new HG2 system, which will be default from 2014b. The toolbox uses property “PixelBounds” as an indicator that size of container has been changed. And there is a trigger:

    obj.Listeners{end+1,1} = handle.listener( containerObj, findprop( containerObj, 'PixelBounds' ), ...
        'PropertyPostSet', @obj.onResized );

    I changed this to:

    obj.Listeners{end+1,1} = addlistener( containerObj, findprop(containerObj, 'PixelBounds'), 'PostSet', @obj.onResized );

    But then I realised that this property is not available in HG2. Can you please recommend a substitution for that? Thanks a lot in advance!

    • @Sergey – the GUI layout toolbox is incompatible with HG2 in several places, not just the one that you found. MathWorks is aware of this and I expect that they will update the FEX utility when 14b is finally released.

      Rather than using the GUI toolbox, you can use uiflowcontainer. By specifying non-inf WidthLimits and/or HeightLimits property values, you can fix the size/position automatically without needing a custom resize callback function.

    • Sergey Kucheryavskiy says:

      Yes, now I see it as well: — problems with Children in TabPanel and some others. Actually it was the TabPanel I decided to use the GUI layout, otherwise I feel quite comfortable with GridBagLayout you recommended in some go your post. Will find a workaround for now and wait for the fixes.


  25. Niko says:

    Hi Yair,

    Thank you so much for this amazing code! It allowed me access so much more java-based functionality not available in matlab… One problem I’m seeing though, is that when I have multiple instances of the same figure window open, findjobj works for the first instance, but not the later opened ones. Am I not using the code correctly, or maybe there is a way to work around this?

    In the openingfcn of my figure:


    And only the edit box in the first instance becomes in-editable.

    Thank you very much!



    • @Niko – findjobj should be places in the outputfcn() as I’ve explained in numerous places – it needs the figure window to be visible in order to find the Java components.

  26. H.A. Camp says:

    Hi Yair,

    First, thank you for your new book! Your last one has been invaluable to me, and I eagerly await the new arrival…

    With release R2013b, I cannot seem to get this simple code (from your article above, as well as page 356 in your Undocumented Secrets book) to work:

    hButton = uicontrol('string','click me!');
    jButton = findjobj(hButton);

    No errors are generated, but the button component’s cursor does not change to the expected hand. I have verified that the code works as advertised using R2012b, but not R2013b… am I doing something wrong, or is this expected behavior in the later MATLAB releases? Is there a workaround for newer releases?

    Many thanks for your efforts.

    • It works ok for me, all the way up to R2015a…

      Try to add drawnow; pause(0.01); and/or download the latest version of findjobj from the Matlab File Exchange.

    • H.A. Camp says:

      I’ve verified that I’m using the latest version of findjobj, but I still get the same behavior with or without the drawnow/pause addition. Hmm, I’m not sure how to figure out what’s wrong… Any ideas?

  27. John says:

    A significant part of my work is developing MATLAB-based GUIs for engineering R&D applications; as such, I have referenced and used your information and the findjobj utility; they are very useful – thank you!

    I’ve got a case where I’m using an “ActionPerformedCallback” for an edit control; in the callback I would like to get back the MATLAB edit control handle from the Java edit control peer (that is, the reverse of findjobj). Is this possible? I can always hunt down the MATLAB edit control by first getting the current figure and looking in that figure, but I wanted to see if there was an easy way to get back the MATLAB handle directly from the Java object.

    Best regards,


    • @John – there is no direct way to get the Matlab handle, but you can set it in your Java handle’s ApplicationData when you first find it using findjobj:

      hEditbox = uicontrol('Style','edit', ...);
      jEditbox = findjobj(hEditbox);
      setappdata(jEditbox, 'MatlabHandle',hEditbox);

      Then within your callback you could getappdata(jEditbox,'MatlabHandle') to get back the Matlab handle.

      As an alternative, you could pass the Matlab handle as an extra (third) input argument to your editting callback function:

      jEditbox.ActionPerformedCallback = {@myEditboxCallback, hEditbox};
      function myEditboxCallback(jEditbox, eventData, hEditbox)
    • John says:

      Awesome – thank you, Yair!


  28. Adam says:

    These lines of code give me the following Java exception errors. Can you please help?

    jEditbox = findjobj(editBoxHandle,'nomenu');
    jEditbox.setText('some string');
    Exception in thread “AWT-EventQueue-0″ java.lang.NullPointerException
    at javax.swing.text.DefaultCaret.paint(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.paintSafely(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.paint(Unknown Source)
    at javax.swing.plaf.basic.BasicTextUI.update(Unknown Source)
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$ Source)
    at java.awt.EventQueue$ Source)
    at Method)
    at$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at Source)
    • @Adam – try to ensure that your editbox is fully visible onscreen when you issue the findjobj() function. Also, ensure that you are using the latest version of the findjobj() utility from the File Exchange. Also, try adding a short delay before running findjobj(), as explained here. For example:

      drawnow; pause(0.05);
      jEditbox = findjobj(editBoxHandle);
      jEditbox.setText('some string');
    • Adam says:

      Thank you for responding but none of these suggestions helped. Another user is suffering from the exact same problem. Workaround he is suggestiong on this SO page works for me. No matter what I have tried (including but not limited to Null checks)

      jEditbox.setText('some string');

      is causing the Java exception errors. Running R2014a on a Windows machine.

    • ML says:

      A guess, and only a guess, is that the UI is making a call that expects the caret to have been set when it has not been. A test would be to see if jEditbox.getCaret() called before setText(…) returns null.

  29. Ravi Goyal says:

    Dear Yair,

    I am using findjobj in a GUI created for the Institute I work and it functions perfectly. In order to speed up the GUI I tweaked a bit around and we also just ordered your latest book (so far only used google snippets). Anyways, using the profiler the longest time I spend is using findjobj which I call regularly (I have a jtable included with cellcolorpicker ect…)

    Calls with the longest time:

    findjobj>traverseContainer and in there lines 507 and 580

    Is there anyway to speed up this portion, or is it possible to save a javahandle into a matlab handle so I can just reuse it. All my functions where I use the findjobj function use

    jscroll = functions.java_table.findjobj(handles.eis_data_table); 
    jtable = jscroll.getViewport.getComponent(false);

    Thanks and best regards

    • @Ravi – You can store the Java handles in your Matlab code variables, for later reuse. You can also use findjobj with the ‘persist’ input to avoid the need to rescan the figure contents each time. This of course depends on that new components were not added since the last time that you ran the function.

    • Ravi Goyal says:

      Thank You Yair,

      Using persist and set/getappdata, makes the whole thing way more responsive : )

      Perfect, thanks a lot.

  30. Travis says:


    I am trying to set the position in a uitable, but I can’t for the life of me find how to change the ‘value’ field for the vertical scroll bar. Using your interface I can see where it is, but I am unsure how to navigate java and its components.

    I have a variable with the uitable in question:

    A = findjobj(TABLE1)

    but that is as far as I have been able to get. Any suggestions would be very helpful.

    Thank you.

  31. Payam Nia says:

    Dear Yair,
    I have the same issue also.
    I am trying to define the vertical scroll bar’s position.


  32. Pingback: Listbox selection hacks | Undocumented Matlab

  33. Akshay says:

    I need create a round pushbutton in matlab gui. As it is not directly available in matlab gui, I was thinking to take it using java and then use it in matlab.
    I have some questions regarding this.
    1) How to identify that java component(round pushbutton)?
    2) will findjobj help me doing this?
    3) Is there any other method? I dont want to wrap circular images on square pushbuttons.

    • findjobj can be used to find the underlying Java control of an existing Matlab GUI control (e.g., pushbutton). You can then modify the control’s Border as explained here:

    • Akshay says:


      Ok. Can I customize default square pushbutton to have circular border?

    • @Ashkay – did you bother to read what I just sent you? You need to read the material and then search online to see how to create a rounded Java Border.

    • Akshay says:


      Hope you are doing fine. I have went through the document for customizing the border.
      Let me rephrase again to have we both on the same page, I need your help in understanding how to override the default Push Button border properties in java and refer the same in Matlab (need to override the default border function).

      I am quite new to MATLAB and don’t know how these two languages are exchanging their objects. I would really appreciate if you provide me the more details.

      Thanks for your time and support as always! :-)

  34. Xiangrui Li says:

    Hi Yair,
    Just in case you don’t know, Matlab Central web update likely breaks pattern search in checkVersion(). I use the similar method to your code, and realized this problem.
    My current solution is to search ‘user_version’, which seems reliable.

  35. Omar says:

    Hello Yair,

    I am using MATLAB R2017a and I get an “Undefined function or variable ‘findjobj’.”-Error when using findjobj(text). findobj(text) works tho.
    Is this no longer supported?

    • Omar – findjobj is not a built-in Matlab function, it is a function that you need to download from the Matlab File Exchange, as the article clearly states (read it!)…

  36. Eddie says:

    I just downloaded 2018b and findjobj does not work on uicontrol anymore :(

    For example,

    Returns an empty handle in 2018b, but in 2018a that is not a problem.

  37. Nivetha says:

    Hi Yair,

    Sorting of numeric values in UITable is not working . Any workaround or fix?

Leave a Reply

Your email address will not be published. Required fields are marked *