findjobj - find java handles of Matlab graphic objects

Find java objects contained within a specified java container or Matlab GUI handle
47.7K Downloads
Updated 17 Aug 2021

View License

Find all java objects contained within a java container or Matlab GUI handle
If no output parameter is specified, then an interactive GUI window will be displayed with a tree-view of all container components, their properties and callbacks.
Syntax:
[handles,levels,parentIds,listing] = findjobj(container,'PropName',PropValue(s),...)
Inputs:
- container - optional GUI handle. If unsupplied then current figure will be used
- 'PropName',PropValue - optional list of case insensitive property pairs. PropName may also be named -PropName.
Supported properties:
- 'position' - filter results based on those elements that contain the specified X,Y position or a java element
Note: specify a Matlab position (X,Y = pixels from bottom left corner), not a java one
- 'size' - filter results based on those elements that have the specified W,H (in pixels)
- 'class' - filter results based on those elements that contain the substring (or java class) PropValue
Note: filtering is case insensitive and relies on regexp, so you can pass wildcards etc.
- 'property' - filter results based on elements that possess the specified case-insensitive property string or have property values in cell array format: {'propName', 'propValue'}. Example: findjobj(...,'property', {'Text','click me'})
- 'depth' - filter results based on specified depth. 0=top-level, Inf=all levels (default=Inf)
- 'flat' - same as: 'depth',0
- 'not' - negates the following filter: 'not','class','c' returns all elements EXCEPT those with class 'c'
- 'persist' - persist figure components information, allowing much faster results for subsequent invocations
- 'print' - display all java elements in a hierarchical list
Note1: optional PropValue of element index or handle to java container
Note2: normally this option would be placed last, after all filtering is complete.
- 'list' - same as 'print'
Outputs:
- handles - list of handles to java elements
- levels - list of corresponding hierarchy level of the java elements (top=0)
- parentIds - list of indexes (in unfiltered handles) of the parent container of the corresponding java element
- listing - results of 'print'/'list' options (empty if 'print'/'list' were unspecified)
Sample usage:
>> hButton = uicontrol('string','click me');
>> jButton = findjobj(hButton,'nomenu'); % or: jButton = findjobj('property',{'Text','click me'});
>> jButton.setFlyOverAppearance(1);
>> jButton.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.HAND_CURSOR));
>> set(jButton,'FocusGainedCallback',@myMatlabFunction); % some 30 callback points available...
>> jButton.get; % list all changeable properties...
>> hEditbox = uicontrol('style',edit');
>> jEditbox = findjobj(hEditbox,'nomenu');
>> jEditbox.setCaretColor(java.awt.Color.red);
>> jEditbox.KeyTypedCallback = @myCallbackFunc; % many more callbacks where this came from...
>> jEdit.requestFocus;
(Many more examples in the utility's help section)
Known issues/limitations:
- Cannot currently process multiple container objects - just one at a time
- Initial processing is a bit slow when the figure is laden with many UI components (so better use 'persist')
- Passing a container Matlab handle is currently found by position+size: should find a better way to do this
- Labels have a write-only text property in java, so can't be found using 'property',{'Text','string'} notation
Technical description:
* https://UndocumentedMatlab.com/articles/findjobj-find-underlying-java-object
* https://UndocumentedMatlab.com/articles/findjobj-gui-display-container-hierarchy
Warning:
This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)
See also:
java, handle, findobj, findall

Cite As

Yair Altman (2024). findjobj - find java handles of Matlab graphic objects (https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Graphics Objects in Help Center and MATLAB Answers
Acknowledgements

Inspired by: UIINSPECT - display methods, properties & callbacks of an object

Inspired: inspect time series events, BacStalk, BiofilmQ, tweak, kristinbranson/JAABA, MATLAB Snippets, Stock Trader Journal, directorscut82/matbuttons, Microscopy Image Browser (MIB), attachScrollPanelTo - add scroll-panel to a uipanel or axes, Microscopy Image Browser 2 (MIB2), statusbar, getjframe - Retrieves a figure's underlying Java frame, enable/disable entire figure window, setDesktopVisibility, UIINSPECT - display methods, properties & callbacks of an object, Manage and Dock Figures into Group, Virtual Tackball, maximize, uhelp, Command window text, Graphical Wrappers, Reorderable Listbox, Colorize Document Bar, tortoise svn toolbar integration, Sim.I.am, Realtime trading with Matlab and IB presentation files, fixuilabels, Programmatically scroll Variables Editor, embeddedmethodsviewer - show functions and methods within matlab editor, Figure from text file (supports HTML tags), Panic Simulator, tprintf -- Print to Second Terminal Window, GavriYashar/Matlab-Editor-Plugin, Copy Paste, Probability Distribution Plotter, Dynamic Search Box, checkVersion - Check for a newer file version on the File Exchange, figs2tabs - move figures into a single tabbed gui, Dicom Operator - EsmeProcess, Chess Master, smart_scrollbars fixes Matlab's listbox/editbox scrollbars to only show as needed, Message Logger, scatool, SessionManager, kview - Interface for signal/data easy visualization and processing, uicontrolHyperLink

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.53.0.1

Updated findjobj_fast: support for desktop & figure handles, hide JavaFrame deprecation warning, improved warning handling (thanks T. Carpenter)

1.53.0.0

Fix for changed warning IDs in R2019b; removed javacomponent warning; added promo message

1.52.0.0

Additional fix for R2018b; added separate findjobj_fast utility

1.51.0.0

Fix for R2018b suggested by Eddie (FEX); speedup suggested by Martin Lehmann (FEX); alert if trying to use with uifigure

1.50.0.0

Fixed two edge-cases (one suggested by H. Koch)

1.49.0.0

Fixed edge-cases in old Matlab release; slightly improved performance even further

1.48.0.0

Improved performance for the most common use-case (single input/output): improved code + allow inspecting groot

1.47.0.0

Significantly improved performance for the most common use-case (single input/output)

1.27.0.0

Fixed case of docked figure

1.26.0.0

Differentiate between overlapping controls (for example in different tabs)

1.25.0.0

Additional fixes for R2014a, R2014b

1.24.0.0

Fixes for R2014b

1.23.0.0

Minor fix for R2014a; handled case of invisible figure container; only check for newer FEX version in non-deployed mode and only once every 12 hours

1.22.0.0

Additional fixes for the upcoming HG2

1.21.0.0

Fix for the upcoming HG2; fixed HG-Java warnings

1.20.0.0

Fixed callbacks table grouping & editing bugs; added hidden properties to the properties tooltip; updated help section

1.19.0.0

Improved callbacks table; improved export to workspace; fixed tree refresh failure; fixed: tree node-selection didn't update the props pane nor flash the selected component

1.18.0.0

Fixes for R2012a as well as some older Matlab releases

1.17.0.0

Fixed 'File is empty' messages in compiled apps

1.16.0.0

Fix suggested by Ward; fixes for R2011a and R2010b; fixed download (m-file => zip-file)

1.14.0.0

Minor fix to support combo-boxes (aka drop-down, popup-menu) on Windows

1.13.0.0

Important release: Fixes for R2010a, debug listing, objects not found, component containers that should be ignored etc.

1.12.0.0

Forced an EDT redraw before processing; warned if requested handle is invisible

1.11.0.0

Fix to display text labels deep in HG hierarchy

1.10.0.0

Fixed uitreenode warning; found a way to display label text next to the relevant node name

1.9.0.0

Fixed auto-collapse of invisible container nodes; added dynamic tree tooltips & context-menu; minor fix to version-check display

1.8.0.0

Fix for Matlab 7.0 as suggested by Oliver W; minor GUI fix (classname font); fixed edge-case of missing JIDE tables

1.7.0.0

Added support for future Matlab versions that will not support JavaFrame; added sanity checks for axes items; added 'debug' input arg; increased size tolerance 1px => 2px

1.6.0.0

Fixed location of popupmenus (always 20px high despite what's reported by Matlab...); fixed uiinspect processing issues; added blog link; narrower action buttons; automatic 'nomenu' for uicontrol inputs; significant performance improvement

1.5.0.0

Fixed position of some Java components; fixed properties tooltip; fixed node visibility indication

1.4.0.0

Indicated components visibility (& auto-collapse non-visible containers); auto-highlight selected component; fixed node icons, figure title & tree refresh; improved error handling; displayed FindJObj version update description if available

1.3.0.0

Fixed update check; added dedicated labels icon; fixed description

1.2.0.0

Fixed compatibility problems with previous Matlab versions; updated screenshot

1.1.0.0

Callbacks table fixes; better object inspection; fix update check according to new FEX website; R2008b compatibility; fixed minor bug as per Johnny Smith's comment

1.0.0.0

Fixed edge case problem with class properties tooltip; used existing object icon if available; added checkbox option to hide standard callbacks