image thumbnail

findjobj - find java handles of Matlab graphic objects

version 1.53.0.1 (183 KB) by Yair Altman
Find java objects contained within a specified java container or Matlab GUI handle

46.4K 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 (2021). 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
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

Community Treasure Hunt

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

Start Hunting!