| Description |
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 property pairs (case insensitive). PropName may also be named -PropName
- - '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)
Examples:
>> handles=findjobj; %get list of all java elements in current fig (inc. menus, toolbars, ...)
>> findjobj('print'); %list all java elements in current figure
>> findjobj('print',6); %list all java elements in current fig, contained within 6th element
>> handles=findjobj(hButton); %hButton is a matlab button
>> handles=findjobj(gcf,'position',getpixelposition(hButton,1)); %as above but also return hButton's panel
>> handles=findjobj(hButton,'persist'); %as above, persist info for future reuse
>> handles=findjobj('class','pushbutton'); %get all pushbuttons in current figure
>> handles=findjobj('class','pushbutton','position',123,456); %get all pushbuttons at the specified position
>> handles=findjobj(gcf,'class','pushbutton','size',23,15); %get all pushbuttons with the specified size
>> handles=findjobj('property','Text','not','class','button'); %get all non-button elements with 'text' property
>> handles=findjobj('-property',{'Text','click me'}); %get all elements with 'text' property = 'click me'
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;
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
- Matlab uipanels are not implemented as simple java panels, and so they can't be found using this utility
- Labels have a write-only text property in java, so they can't be found using the 'property',{'Text','string'} notation
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)
Change log: see bottom of this page
See also:
java, handle, findobj, findall |