<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FindJObj &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/findjobj/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 11 Apr 2016 09:18:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Faster findjobj</title>
		<link>https://undocumentedmatlab.com/articles/faster-findjobj?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=faster-findjobj</link>
					<comments>https://undocumentedmatlab.com/articles/faster-findjobj#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 11 Apr 2016 09:18:14 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Undocumented property]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6376</guid>

					<description><![CDATA[<p>the ubiquitous findjobj utility has been significantly improved for speed for the most common use case. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/faster-findjobj">Faster findjobj</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/findjobj-find-underlying-java-object" rel="bookmark" title="FindJObj &#8211; find a Matlab component&#039;s underlying Java object">FindJObj &#8211; find a Matlab component&#039;s underlying Java object </a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/findjobj-gui-display-container-hierarchy" rel="bookmark" title="FindJObj GUI &#8211; display container hierarchy">FindJObj GUI &#8211; display container hierarchy </a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/faster-csvwrite-dlmwrite" rel="bookmark" title="Faster csvwrite/dlmwrite">Faster csvwrite/dlmwrite </a> <small>The speed of the builtin csvwrite, dlmwrite functions can be improved dramatically. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>My <a href="/articles/findjobj-find-underlying-java-object" target="_blank"><i><b>findjobj</b></i> utility</a>, 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 <a href="/?s=findjobj" target="_blank">many examples of this</a> in this blog over the past years.<br />
I am happy to announce that I have just uploaded a new version of <i><b>findjobj</b></i> to the <a href="http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects" rel="nofollow" target="_blank">Matlab File Exchange</a>, which significantly improves the utility&#8217;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:</p>
<pre lang="matlab">
>> 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.
</pre>
<p>The new <i><b>findjobj</b></i> is backward-compatible with the old <i><b>findjobj</b></i> and with all prior Matlab releases. It is a drop-in replacement that will significantly improve your program&#8217;s speed.<br />
The new version relies on several techniques:<span id="more-6376"></span><br />
First, as I <a href="/articles/customizing-matlab-uipanels" rel="nofollow" target="_blank">showed last year</a>, in HG2 (R2014 onward), Matlab <i><b>uipanel</b></i>s have finally become full-featured Java <code>JPanel</code>s, that can be accessed and customized in many interesting manners. More to the point here, we can now directly access the underlying <code>JPanel</code> component handle using the <i><b>uipanel</b></i>&#8216;s hidden <b>JavaFrame</b> property (thanks to MathWorks for supplying this useful hook!). The new <i><b>findjobj</b></i> version detects this and immediately returns this handle if the user specified a <i><b>uipanel</b></i> input.<br />
I still do not know of any direct way to retrieve the underlying Java component&#8217;s handle for Matlab <i><b>uicontrol</b></i>s, 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&#8217;s underlying Java handle. In the old version of <i><b>finjobj</b></i>, we retrieve the containing figure&#8217;s <code>JFrame</code> reference and from it the <code>ContentPane</code> 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 <i><b>uicontrol</b></i>&#8216;s direct parent, which is very often a <i><b>uipanel</b></i>. In this case, we can directly retrieve the panel&#8217;s <code>JPanel</code> 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&#8217;s <code>ContentPane</code>.<br />
In addition, I used a <a href="/articles/findjobj-gui-display-container-hierarchy#comment-332487" target="_blank">suggestion</a> by blog reader Hannes for a faster recursive search that uses the control&#8217;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.<br />
Feel free to download and use the new <i><b>findjobj</b></i> version. The code for the fast variant can be found in lines #190-205 and #3375-3415.<br />
Enjoy!<br />
p.s. &#8211; as I explained last week, today&#8217;s discussion, and in general anything that has to do with Java peers of GUI controls, only relates to the existing <code>JFrame</code>-based figure windows, not to the new web-based <i><b>uifigure</b></i>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/faster-findjobj">Faster findjobj</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/findjobj-find-underlying-java-object" rel="bookmark" title="FindJObj &#8211; find a Matlab component&#039;s underlying Java object">FindJObj &#8211; find a Matlab component&#039;s underlying Java object </a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/findjobj-gui-display-container-hierarchy" rel="bookmark" title="FindJObj GUI &#8211; display container hierarchy">FindJObj GUI &#8211; display container hierarchy </a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/faster-csvwrite-dlmwrite" rel="bookmark" title="Faster csvwrite/dlmwrite">Faster csvwrite/dlmwrite </a> <small>The speed of the builtin csvwrite, dlmwrite functions can be improved dramatically. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/faster-findjobj/feed</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>Listbox layout customization</title>
		<link>https://undocumentedmatlab.com/articles/listbox-layout-customization?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=listbox-layout-customization</link>
					<comments>https://undocumentedmatlab.com/articles/listbox-layout-customization#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 13 Nov 2013 20:08:28 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4374</guid>

					<description><![CDATA[<p>Matlab's listbox layout can be modified to display multiple item columns and different inter-cell margins. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/listbox-layout-customization">Listbox layout customization</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-listbox-mouse-actions" rel="bookmark" title="Setting listbox mouse actions">Setting listbox mouse actions </a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items" rel="bookmark" title="Customizing listbox/combobox items">Customizing listbox/combobox items </a> <small>Matlab listboxes can be customized using custom Java cell-renderers. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/listbox-selection-hacks" rel="bookmark" title="Listbox selection hacks">Listbox selection hacks </a> <small>Matlab listbox selection can be customized in a variety of undocumented ways. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I haven&#8217;t written on listboxes in a long while, and since I&#8217;ve recently posted on related controls (<a target="_blank" href="/articles/editbox-data-input-validation/">editbox</a>, <a target="_blank" href="/articles/editable-combo-box/">combo-box</a>), I thought of following up with an article on customizing Matlab listbox layout. By the end of today&#8217;s article, you should be able to customize the layout of items within your listbox, such as the following:<br />
<center><figure style="width: 420px" class="wp-caption aligncenter"><img decoding="async" alt="Customized listbox layout" src="https://undocumentedmatlab.com/images/listbox4.png" title="Customized listbox layout" width="160" height="90" /> &nbsp; <img decoding="async" alt="Customized listbox layout" src="https://undocumentedmatlab.com/images/listbox1b" title="Customized listbox layout" width="225" height="90" /><figcaption class="wp-caption-text">Customized listbox layouts</figcaption></figure></center><br />
<span id="more-4374"></span><br />
For the following hacks, we need to gain access to the listbox control&#8217;s underlying Java component, which is a MathWorks class that extends the standard <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/list.html">JList</a>. We can get this component&#8217;s reference using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>:</p>
<pre lang='matlab'>
>> hListbox = uicontrol('Style','List', 'String',{'item #1','item #2'});
>> jScrollPane = java(findjobj(hListbox))
jScrollPane =
com.mathworks.hg.peer.utils.UIScrollPane[...]
>> jListbox = jScrollPane.getViewport.getView
jListbox =
com.mathworks.hg.peer.ListboxPeer$UicontrolList[...]
</pre>
<p>Like multi-line editboxes, listboxes are actually composed of a container (a <code>com.mathworks.hg.peer.utils.UIScrollPane</code> object) that includes three children, as expected from any <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html">JScrollPane</a>: a <code>javax.swing.JViewport</code> that contains the <code>ListboxPeer$UicontrolList</code> component, and horizontal/vertical scrollbars. I explained how to <a target="_blank" href="/articles/customizing-listbox-editbox-scrollbars/">customize the scrollbars</a> in an article back in early 2010.<br />
Today we are not interested in the scroll-pane or scollbars, but rather the <code>jListbox</code> component that takes up the view-port&#8217;s contents. This component includes many useful properties that we can access and modify, including several that control the layout of the list items:<br />
<b>LayoutOrientation</b> sets the layout of listbox items within the viewport. Possible values include:</p>
<ul>
<li>The default value (<code>jListbox.VERTICAL</code>=0) indicates the regular top-to-bottom arrangement</li>
<li><code>jListbox.VERTICAL_WRAP</code>=1 sets a horizontal item layout, wrapping to a new row as necessary for the maximum number of rows determined by the <b>VisibleRowCount</b> property (default=8)</li>
<li><code>jListbox.HORIZONTAL_WRAP</code>=2 sets a vertical item layout, wrapping to a new column at row number <b>VisibleRowCount</b></li>
</ul>
<p>For example:</p>
<pre lang='matlab'>
jListbox.setLayoutOrientation(jListbox.HORIZONTAL_WRAP);
jListbox.setVisibleRowCount(3);
set(jListbox, 'LayoutOrientation',2, 'VisibleRowCount',3);  % equivalent alternative
</pre>
<table style="border:none; text-align:center; background:white;">
<tbody>
<tr>
<td width="30%" style="border-top:none;"><img decoding="async" src="https://undocumentedmatlab.com/images/listbox1a.png" width="150" height="60" /><br />
<b>LayoutOrientation</b> = <code>VERTICAL</code> = 0<br />
<b>VisibleRowCount</b> is irrelevant</td>
<td width="30%" style="border-top:none;"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/listbox1b.png" width="150" height="60" /><br />
<b>LayoutOrientation</b> = <code>VERTICAL_WRAP</code> = 1<br />
<b>VisibleRowCount</b> = 3</td>
<td width="30%" style="border-top:none;"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/listbox1c.png" width="150" height="60" /><br />
<b>LayoutOrientation</b> = <code>HORIZONTAL_WRAP</code> = 2<br />
<b>VisibleRowCount</b> = 3</td>
</tr>
</tbody>
</table>
<p><b>FixedCellHeight</b> and <b>FixedCellWidth</b> hold the listbox&#8217;s cells height (default=13 pixels) and width (default=-1). A value of -1 means that the actual size is determined by the default platform-dependent <b>CellRenderer</b> size:</p>
<table style="border:none; text-align:center; background:white;">
<tbody>
<tr>
<td width="30%" style="border-top:none;"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/listbox1c.png" width="150" height="60" /><br />
<b>FixedCellHeight</b> = -1<br />
<b>FixedCellWidth</b> = -1</td>
<td width="30%" style="border-top:none;"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/listbox2b.png" width="150" height="60" /><br />
<b>FixedCellHeight</b> = 10<br />
<b>FixedCellWidth</b> = 30</td>
<td width="30%" style="border-top:none;"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/listbox2c.png" width="150" height="60" /><br />
<b>FixedCellHeight</b> = 16<br />
<b>FixedCellWidth</b> = 50</td>
</tr>
</tbody>
</table>
<p>We can use these properties to display a selection matrix of icons. For example, let&#8217;s display the icons in Matlab standard icons folder:</p>
<pre lang='matlab'>
% Prepare the list of ImageIcon objects
iconsFolder = fullfile(matlabroot,'toolbox/matlab/icons');
imgs = dir(fullfile(iconsFolder,'*.gif'));
for iconIdx = 1 : length(imgs)
   iconFilename = fullfile(iconsFolder,imgs(iconIdx).name);
   iconFilename = strrep(iconFilename, '\', '/');
   htmlStr{iconIdx} = ['<html><img decoding="async" src="file:/' iconFilename '"/>'];  % no need for </html>
end
% Display in a standard one-column listbox
hListbox = uicontrol('style','list', 'string',htmlStr, 'position',[10,10,160,90]);
% Modify the listbox layout to display 18x18-pixel cells
jScrollPane = findjobj(hListbox);
jListbox = jScrollPane.getViewport.getView;
jListbox.setLayoutOrientation(jListbox.HORIZONTAL_WRAP)
jListbox.setVisibleRowCount(4)
jListbox.setFixedCellWidth(18)   % icon width=16  + 2px margin
jListbox.setFixedCellHeight(18)  % icon height=16 + 2px margin
jListbox.repaint  % refresh the display
</pre>
<p><center><figure style="width: 160px" class="wp-caption aligncenter"><img decoding="async" alt="Customized listbox layout" src="https://undocumentedmatlab.com/images/listbox4.png" title="Customized listbox layout" width="160" height="90" /><figcaption class="wp-caption-text">Customized listbox layout</figcaption></figure></center><br />
Other interesting things that we can do with listboxes (among others):</p>
<ul>
<li><a target="_blank" href="/articles/customizing-listbox-editbox-scrollbars/">Customize the scrollbars</a>, as noted above<br />
&nbsp;
</li>
<li><a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">Display HTML-formatted list items</a>:
<pre lang='matlab'>
uicontrol('Style','list', 'Position',[10,10,70,70], 'String', ...
   {'<html><font color="red">Hello</font></html>', 'world', ...
    '<html><font style="font-family:impact;color:green"><i>What a', ...   % note: </i></font></html> are not needed
    '<html><font color="blue" face="Comic Sans MS">nice day!</font>'});   % note: </html> is not needed
</pre>
<p><center><figure style="width: 100px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" title="Listbox with HTML colored items" src="https://undocumentedmatlab.com/images/html11.png" alt="Listbox with HTML'ed items" width="70" height="70" /><figcaption class="wp-caption-text">Listbox with HTML colored items</figcaption></figure></center>
</li>
<li><a target="_blank" href="/articles/setting-listbox-mouse-actions/">Setting dynamic tooltips and right-click context-menus</a>:<br />
<center><figure style="width: 150px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Listbox dynamic tooltip" src="https://undocumentedmatlab.com/images/listbox_tooltip.png" title="Listbox dynamic tooltip" width="120" height="78" /><figcaption class="wp-caption-text">Listbox dynamic tooltip</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 150px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Listbox dynamic context (right-click) menu" src="https://undocumentedmatlab.com/images/listbox_contextmenu.png" title="Listbox dynamic context (right-click) menu" width="128" height="133" /><figcaption class="wp-caption-text">Listbox dynamic context (right-click) menu</figcaption></figure></center><center></center></li>
</ul>
<p>Note that while the code above used the underlying Java component, absolutely no knowledge of Java is required to understand it or use it. In fact, the entire code above is pure Matlab, simply setting the component&#8217;s properties and calling its methods, and using its inherent support of HTML strings.<br />
Much more advanced customizations are possible at the Java level, especially using a dedicated <b>CellRenderer</b>. Interested readers can find more information about these and other possible customizations in my report on &#8220;<i>Advanced Customizations of Matlab Uicontrols</i>&#8220;. This 90-page PDF report can be purchased <a target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;currency_code=USD&#038;business=altmany@gmail.com&#038;amount=29&#038;item_name=Matlab+uicontrols+customization+report">here</a> ($29, please allow 24 hours for delivery by email). The report explains how to customize Matlab&#8217;s uicontrols in ways that are simply not possible using documented Matlab properties. This includes treatment of push buttons, toggle buttons, radio buttons, checkboxes, editboxes, listboxes, popup menus (aka combo-boxes/drop-downs), sliders, labels, and tooltips. Much of the information in the report is also available in hard-copy format in chapter 6 of my <a target="_blank" href="/matlab-java-book/">Matlab-Java programming book</a>.<br />
If you&#8217;d like me to personally add similar magic to your GUI, <a target="_blank" href="/consulting/">email me</a> to see if I can help.<br />
<center><figure style="width: 286px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" title="Advanced listbox CellRenderer customization" src="https://undocumentedmatlab.com/images/listbox4d.png" alt="Advanced listbox CellRenderer customization" width="286" height="134" /><figcaption class="wp-caption-text">Advanced listbox CellRenderer customization</figcaption></figure> </center><br />
Now tell the truth &#8211; doesn&#8217;t Matlab&#8217;s standard listbox look kinda boring after all this? 🙂</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/listbox-layout-customization">Listbox layout customization</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-listbox-mouse-actions" rel="bookmark" title="Setting listbox mouse actions">Setting listbox mouse actions </a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items" rel="bookmark" title="Customizing listbox/combobox items">Customizing listbox/combobox items </a> <small>Matlab listboxes can be customized using custom Java cell-renderers. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/listbox-selection-hacks" rel="bookmark" title="Listbox selection hacks">Listbox selection hacks </a> <small>Matlab listbox selection can be customized in a variety of undocumented ways. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/listbox-layout-customization/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing editboxes</title>
		<link>https://undocumentedmatlab.com/articles/customizing-editboxes?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-editboxes</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-editboxes#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Sep 2013 14:00:26 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4212</guid>

					<description><![CDATA[<p>Matlab's editbox can be customized in many useful manners</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-editboxes">Customizing editboxes</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-2" rel="bookmark" title="Customizing menu items part 2">Customizing menu items part 2 </a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-labels" rel="bookmark" title="Customizing Matlab labels">Customizing Matlab labels </a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>As a natural follow-up to last week&#8217;s article about <a target="_blank" href="/articles/rich-contents-log-panel/">rich-content log panels</a> (multi-line editbox), today I discuss some additional customizations that can be done to Matlab&#8217;s editbox control.</p>
<h3 id="duality">Matlab&#8217;s dual editbox controls</h3>
<p>There are two distinct uicontrols called &#8216;editbox&#8217; in Matlab: a single-line editbox and a multi-line editbox. Matlab automatically uses the single-line control if the <b>Max</b> property is set to 1 (the default value, backward compatible with early Matlab versions). If <b>Max</b> > 1, the multi-line editbox is used. Today&#8217;s article will focus on features shared by both the single-line and multi-line editbox controls.<br />
Beware of a possible pitfall using Matlab&#8217;s editbox controls: when switching styles, including switching between the single-line and multi-line editbox versions, Matlab replaces the underlying Java component with a new component that has default properties. Therefore, if we need any customizations to the uicontrol, then we should ensure that they are done after setting the final uicontrol style, otherwise they will be discarded.</p>
<h3 id="java">Underlying Java object</h3>
<p>As discussed in many prior articles, the first step in customization is to get access to the Matlab control&#8217;s underlying Java control. This is done using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> utility:</p>
<pre lang='matlab'>
% Prepare the log editbox
hEditbox = uicontrol('Style','edit', 'String','Matlab', ...);
% Get the underlying Java editbox
jEditbox = findjobj(hLogPanel);
try
    % Multi-line editboxes are contained within a scroll-panel
    jEditbox = handle(jEditbox.getViewport.getView, 'CallbackProperties');
catch
    % probably a single-line editbox
end
</pre>
<h3 id="borders">Borders</h3>
<p>As I have <a target="_blank" href="/articles/customizing-uicontrol-border/">explained</a> long ago, all uicontrol borders can be customized using the underlying <code>jEditbox</code> handle&#8217;s <b>Border</b> property:</p>
<pre lang="matlab">
% Create a new border
lineColor = java.awt.Color(1,0,0);  % =red
thickness = 3;  % pixels
roundedCorners = true;
newBorder = javax.swing.border.LineBorder(lineColor,thickness,roundedCorners);
% Override the default control's border
jEditbox.Border = newBorder;  % or: set(jEditbox,'Border',newBorder) or: jEditbox.setBorder(newBorder)
% Remove the border altogether
jEditbox.Border = [];
% Redraw the modified control after we have changed its appearance
jEditbox.repaint;
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with regular border" src="https://undocumentedmatlab.com/images/editbox1.png" title="editbox with regular border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with regular border</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with custom border" src="https://undocumentedmatlab.com/images/editbox4a.png" title="editbox with custom border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with custom border</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with no border" src="https://undocumentedmatlab.com/images/editbox3.png" title="editbox with no border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with no border</figcaption></figure></center><br />
<span id="more-4212"></span><br />
Much more complex and interesting borders can be created in much the same way. Interested readers are referred to the <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/border.html">official documentation of Java Borders</a> or any decent Swing textbook.</p>
<h3 id="selection">Text selection</h3>
<p>Several <code>jEditbox</code> properties control the text-selection functionality:</p>
<ul>
<li><b>SelectionStart</b>, <b>SelectionEnd</b> control the characters within the displayed text that are selected (typically with some shaded background color). A value of 0 means the first character, 1 means the second character etc. Setting <b>SelectionStart</b>=0 and <b>SelectionEnd</b>=<b><i>intmax</i></b> selects the entire text. We can also use <code>jEditbox.<i>select(selectionStart,selectionEnd)</i></code>. For example:
<pre lang='matlab'>
set(jEditbox, 'SelectionStart',1, 'SelectionEnd',5);
jEditbox.select(1,5)  % equivalent alternative
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Setting the selected text" src="https://undocumentedmatlab.com/images/editbox_selection2.png" title="Setting the selected text" width="90" height="30" /><figcaption class="wp-caption-text">Setting the selected text</figcaption></figure></center>
</li>
<li><b>SelectionColor</b>, <b>SelectedTextColor</b> change the foreground and background colors of the selected text. These properties are overridden whenever the editbox gains focus, so we need to be override them in the editbox’s <b>FocusGainedCallback</b>:
<pre lang='matlab'>
cbStr = ['set(gcbo,''SelectionColor'',java.awt.Color.red,' ...
               '''SelectedTextColor'',java.awt.Color.blue)'];
set(jEditbox, 'FocusGainedCallback', cbStr);
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Non-standard selection colors and FocusGainedCallback" src="https://undocumentedmatlab.com/images/editbox_selection.png" title="Non-standard selection colors and FocusGainedCallback" width="90" height="30" /><figcaption class="wp-caption-text">Non-standard selection colors, FocusGainedCallback</figcaption></figure></center>
</li>
<li><b>SelectedText</b> is a read-only property holding the text currently selected, between the <b>SelectionStart</b> and <b>SelectionEnd</b> positions. Associated property <b>Text</b> holds the entire text within the editbox. Note that both these properties hold a <code>java.lang.String</code> object, which should be cast to a Matlab string via Matlab’s built-in <b><i>char</i></b> function, unless we use Matlab&#8217;s <i><b>get</b></i> function (which does this conversion for us automatically):
<pre lang='matlab'>
str = char(jEditbox.getSelectedText);
str = get(jEditbox,'SelectedText');  % equivalent alternative
</pre>
</li>
</ul>
<h3 id="caret">Customizing the input caret</h3>
<p>The <b>Caret</b> property, which is common to all Java Swing data-entry components, references a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/text/DefaultCaret.html">javax.swing.text.DefaultCaret</a> object that controls the text caret appearance (this is naturally relevant only for editable editboxes).<br />
The caret object has its own properties that can be customized. For example: <b>BlinkRateRate</b>, <b>Visible</b> and <b>UpdatePolicy</b>. The caret&#8217;s <b>StateChangedCallback</b> is invoked whenever the caret position is updated.<br />
Some additional caret-related properties can be set using <code>jEditbox</code> properties: <b>CaretColor</b> and <b>CaretPosition</b> (which uses 0-based, like <b>SelectionStart</b> and <b>SelectionEnd</b> above). Here is an example that modifies the default caret color to something a bit more lively:</p>
<pre lang='matlab'>
% Set the caret color to red
jEditbox.setCaretColor(java.awt.Color(1.0,0,0));
jEditbox.setCaretColor(java.awt.Color.red);	% an alternative
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Red CaretColor" src="https://undocumentedmatlab.com/images/editbox_caretcolor.png" title="Red CaretColor" width="90" height="30" /><figcaption class="wp-caption-text">Red CaretColor</figcaption></figure></center></p>
<h3 id="behavior">Behavior</h3>
<p>Several properties of the <code>jEditbox</code> handle control the editbox behavior beyond what is available by the Matlab uicontrol:</p>
<ul>
<li><b>Editable</b> &#8211; (default=true) a boolean flag indicating whether or not the editbox text can be modified. Note that the Matlab HG handle (<code>hEditbox</code>) only allows setting the <b>Enable</b> property (its <code>jEditbox</code> Java counterpart property is called <b>Enabled</b>), but not to set an <i>enabled yet uneditable</i> control – this can only be done using the Java <b>Editable</b> property.</li>
<li><b>DisabledTextColor</b> controls the text color (default=gray) when the editbox is disabled.</li>
<li><b>DragEnabled</b> – (default=false) a boolean flag indicating whether the editbox contents can be mouse-dragged externally as a DND source (for example, onto an editor, command line or any DND target). The <b>DropMode</b>, <b>DropLocation</b>, <b>DropTarget</b> and <b>TransferHandler</b> properties enable the editbox act as a DND target, accepting externally-dragged data as input sources.</li>
<li><b>FocusAccelerator</b> – (default=char(0)) sets the keyboard accelerator sequence that will cause the receiving text component to get the focus. The accelerator will be the key combination of the &lt;Alt> key and the specified character, converted to upper-case. Any previous key accelerator setting, including menu-bar accelerators, will be superseded. A char(0) key has the effect of turning off the focus accelerator. By default, there is no focus accelerator key (i.e., an accelerator of \0=char(0)). For example, let us set the accelerator to &lt;Alt>-E, overriding the menu-bar&#8217;s default accelerator for the Edit menu:
<pre lang='matlab'>
>> jEditbox.setFocusAccelerator('e');
>> jEditbox.getFocusAccelerator		% let us check...
ans =
E	% 'e' converted to 'E', meaning an Alt-E accelerator
</pre>
</li>
</ul>
<p>Additional editbox behavior can be customized using the dozens of callback functions that <code>jEditbox</code> exposes. These callbacks enable modifying the appearance and behavior of the editbox based on events such as mouse movements (into/over/out-of), focus gain/loss, key-clicks (that enable input data validation) etc. I plan to describe a data input-validation function based on these callbacks, using some of the customizations shown above, in next week&#8217;s article.</p>
<h3 id="report">Available report &#8211; &#8220;Advanced Customizations of Matlab Uicontrols&#8221;</h3>
<p>Interested readers can find more information about these and other possible customizations in my report on &#8220;<i>Advanced Customizations of Matlab Uicontrols</i>&#8220;. This 90-page PDF report can be purchased <a target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;currency_code=USD&#038;business=altmany@gmail.com&#038;amount=29&#038;item_name=Matlab+uicontrols+customization+report">here</a> ($29, please allow 24 hours for delivery by email). The report explains how to customize Matlab&#8217;s uicontrols in ways that are simply not possible using documented Matlab properties. This includes treatment of push buttons, toggle buttons, radio buttons, checkboxes, editboxes, listboxes, popup menus (aka combo-boxes/drop-downs), sliders, labels, and tooltips. Much of the information in the report is also available in hard-copy format in chapter 6 of my <a target="_blank" href="/matlab-java-book/">Matlab-Java programming book</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-editboxes">Customizing editboxes</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-2" rel="bookmark" title="Customizing menu items part 2">Customizing menu items part 2 </a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-labels" rel="bookmark" title="Customizing Matlab labels">Customizing Matlab labels </a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-editboxes/feed</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
		<item>
		<title>Variables Editor scrolling</title>
		<link>https://undocumentedmatlab.com/articles/variables-editor-scrolling?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=variables-editor-scrolling</link>
					<comments>https://undocumentedmatlab.com/articles/variables-editor-scrolling#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 11 Sep 2013 10:35:27 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[Oleg Komarov]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4161</guid>

					<description><![CDATA[<p>The Matlab Variables Editor can be accessed to provide immediate scrolling to a specified cell location. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/variables-editor-scrolling">Variables Editor scrolling</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/recovering-previous-editor-state" rel="bookmark" title="Recovering previous editor state">Recovering previous editor state </a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to introduce guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/61651">Oleg Komarov</a>. Oleg has gathered a reputation as a Matlab veteran with plenty of insight into the arcane corners of Matlab, originally on the <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/author/123159">CSSM newsgroup</a>, and more recently on <a target="_blank" rel="nofollow" href="http://stackoverflow.com/users/2180721/oleg-komarov">StackExchange</a> (which admittedly contains more Q&#038;As of the challenging type). Today Oleg discusses a non-trivial solution to a deceivingly-simple problem.</i></p>
<h3 id="Introduction">Introduction</h3>
<p>I often work with datasets that in one way or another have millions of rows and several columns. Although the dimensions preclude visual-based approaches, e.g. imagine keeping all data on a spreadsheet, especially during the development and testing phases, I need to inspect the dataset at given &#8216;coordinates&#8217;.<br />
As a concrete example, in my work on financial time-series I sometimes encounter a subtle irregularity that might arise from a bug in the code when you stack different series of prices. Then you calculate returns and wonder why you get -99% or +2000%.<br />
Thankfully, the <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/community/2008/04/21/variable-editor/">Variables Editor</a> (VE) is very much what I would need for such inspection tasks, if it wasn&#8217;t that its usefulness is inversely proportional to the size of the data. An example would better clarify what I mean. Suppose you need to scroll to position 5677545 of your 1e8-by-1 data variable, the following screenshot illustrates how the scrollbar, pgdown/pgup or the arrow keys are not fine-tuned for such task.<br />
<span id="more-4161"></span><br />
<center><figure style="width: 376px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Problematic Variables Editor scrolling (and the scrollto solution)" src="https://undocumentedmatlab.com/images/scrollto_animated.gif" title="Problematic Variables Editor scrolling (and the scrollto solution)" width="376" height="500" /><figcaption class="wp-caption-text">Problematic Variables Editor scrolling (and the scrollto solution)</figcaption></figure></center><br />
You can easily waste minutes to mindless scrolling!<br />
We could argue that the task is painlessly accomplished through basic logical indexing in the Command Window (CW), as the following image shows (note that I already set the display to <i><b>format compact</b></i>):<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Sub-indexing into a large data matrix" src="https://undocumentedmatlab.com/images/scrollto_2.jpg" title="Sub-indexing into a large data matrix" width="450" height="373" /><figcaption class="wp-caption-text">Sub-indexing into a large data matrix</figcaption></figure></center><br />
However, this approach is limited because:</p>
<ol>
<li>it displays a static snapshot of the selected region, and often precludes interaction with the data, e.g. copy-paste</li>
<li>it requires more visual space than the VE, which either means that you need to modify your layout or scroll the CW</li>
<li>typing in the CW can be even more tedious than scrolling the VE&#8230;</li>
</ol>
<h3 id="openvar">Why <i>openvar</i> is not a good-enough solution</h3>
<p>In search of a solution that would retain the VE interactivity and would speed up the task of scrolling to the point of interest, I first checked if there was an API for the VE similar to that for the documents Editor. As a side-note: to date, the Editor&#8217;s API remains undocumented/unsupported, although having been <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/community/2011/05/09/r2011a-matlab-editor-api/">published</a> in what was then called the Desktop Blog (renamed <i>MATLAB Spoken Here</i> since <a target="_blank" href="/articles/customizing-menu-items-part-3/#ken_mike">Ken and Mike have left MathWorks</a> and the blog focus has changed).<br />
In any case, it turns out that there is a stub of an API for the Variables Editor, <i><b>matlab.desktop.vareditor</b></i>, which however comes much short of my expectations. In fact, it only goes as far as visualizing the data of some variable within a completely separate basic version of the VE. This could prove to be a valid embeddable alternative to the <i><b>uitable</b></i>, but it does not help us with the need to scroll to a specific data cell.<br />
Unlucky with the VE API, I looked into <i><b>openvar</b></i> for an undocumented option that would let me open some variable in the VE and then scroll to the desired &#8216;coordinates&#8217; &#8211; this effort proved fruitless.</p>
<h3 id="solution">A description of the solution that <i>did</i> work</h3>
<p>Since the standard Matlab did not offer a programmatic solution, I started digging into the underlying Java components.<br />
I found that one simple direct way is to open our variable of interest in the VE with <i><b>openvar</b></i>, retrieve the Java <code>Desktop</code> instance (in the same manner as we do to get the Desktop instance to <a target="_blank" href="/articles/accessing-the-matlab-editor/">customize the Editor</a>). From this instance we can find the handle to the appropriate <code>com.mathworks.mlwidgets.array.ArrayTable</code> with <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> (remember that there could be multiple ArrayTables, one for each inspected variable). Finally, we scroll to the desired position using the <i>scrollCellToVisible()</i> method.<br />
Note that the Matlab object builds on the <a target="_blank" href="/articles/customizing-listbox-editbox-scrollbars/"><code>javax.swing.JViewport</code></a> and provides a convenient interface as in the case of the scrolling function, since its java counterpart <i>scrollRectToVisible()</i> requires as input a <code>java.awt.rectangle</code> object rather than scalar doubles.<br />
A succinct usage example would be:</p>
<pre lang='matlab'>
% Create example variable
a = randn(1e8,1);
name = 'a';
% Open 'a' in the VE or grab focus
openvar(name)
% Retrieve Desktop instance and handle to client
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
varclient = desktop.getClient(name);
% Retrieve handle to scrollable table with findjobj
jVarTable = findjobj(varclient,'property',{'name','VariableTable'});
% Select, scroll and update the UI (note the zero-indexing, i.e. row-1)
row = 5677545;
col = 1;
jVarTable.setRowSelectionInterval(row-1, row-1)
jVarTable.setColumnSelectionInterval(col-1, col-1)
jVarTable.scrollCellToVisible(row-1, col-1)
jVarTable.updateUI
</pre>
<h3 id="FEX">The <i>scrollto</i> utility</h3>
<p>As most of the lines of the usage example above should already be familiar and/or self-explanatory to the followers of this blog, the next logical step is to encapsulate the snippet into a function which you can already find on the File Exchange: <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/42795-programmatically-scroll-variables-editor"><i><b>scrollto</b></i></a>.<br />
The <i><b>scrollto</b></i> function provides the following features:</p>
<ul>
<li>Accepts subs or indices</li>
<li>You can scroll indexed variables, i.e. &#8216;a{1}&#8217; or &#8216;a.field{2,3}&#8217; etc.</li>
<li>Scrolling is also supported in debug mode (from ver2.00), i.e. on variables of the &#8216;caller&#8217; workspace</li>
<li>Handles asynchronous rendering of the VE (see below)</li>
</ul>
<p>and supports the following classes (should be 2D arrays):</p>
<ul>
<li>Numeric and logical data – &#8216;VariableTable&#8217;</li>
<li>Cell arrays – &#8216;CellTable&#8217;</li>
<li>Timeseries – &#8216;TimeSeriesArrayEditorTablePanel:fDataTable&#8217;</li>
<li>Datasets (Statistics Toolbox) &#8211; &#8216;DatasetVariableTable&#8217;</li>
</ul>
<p>Matlab handles different classes in the VE through different interfaces. For this reason, for each supported class I reported the &#8216;name&#8217; property to use with <i><b>findjobj</b></i>.</p>
<h3 id="synchronization">Synchronization issues</h3>
<p>Asynchronous rendering of the VE can happen in either of two cases:</p>
<ol>
<li>if the variable has never been opened in the VE, or the variable was opened but it does not exist in the workspace anymore</li>
<li>in code that opens and interacts with the VE</li>
</ol>
<p>Writing <i><b>scrollto</b></i> proved to be more than a simple wrapping effort and it is worth mentioning the workaround implemented to allow a smooth workflow. The biggest issue I faced is the asynchronous rendering of the VE. As Yair reports in his <a target="_blank" href="/matlab-java-book/">book</a> Undocumented Secrets of Matlab-Java Programming, p. 538:</p>
<blockquote><p>&#8220;The tight-coupling of the Variable Editor to the Desktop Workspace variables is unfortunate in some respects. &#8230; Matlab only has a single computational thread, so Matlab code has to finish before the JMI request can be handled. This means that the Variables Editor contents cannot be displayed synchronously by the Matlab code that invokes it.&#8221;
</p></blockquote>
<p>In other words, we cannot retrieve the handle to e.g. the <code>VariableTable</code> until the function has finished executing.<br />
A workaround is to call <i><b>openvar</b></i>, wait until the focus is back to the CW, and then call <i><b>scrollto</b></i>. I cannot tell you how this workflow made me feel so close and yet so far from a satisfactory implementation.<br />
The ideal flowchart of a basic wrapper around the example (see above) would have been:<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Ideal scrollto workflow" src="https://undocumentedmatlab.com/images/scrollto_3.jpg" title="Ideal scrollto workflow" width="450" height="52" /><figcaption class="wp-caption-text">Ideal scrollto workflow</figcaption></figure></center><br />
Now, since we cannot retrieve the handle to the <code>VariableTable</code> if the VE has not rendered yet, I implemented an asynchronous second call to <i><b>scrollto</b></i> through a timer object that fires after the first call to <i><b>scrollto</b></i> has finished executing:<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Asynchronous scrollto workflow" src="https://undocumentedmatlab.com/images/scrollto_4.jpg" title="Asynchronous scrollto workflow" width="450" height="236" /><figcaption class="wp-caption-text">Asynchronous scrollto workflow</figcaption></figure></center><br />
The result is a better experience without unexpected interruptions or breaks in the workflow.<br />
I would like to thank Yair for his comments and guidance. Without his support, <i><b>scrollto</b></i> would not exist and humankind would be doomed to scroll the VE manually!</p>
<h3 id="announcement">Public-service announcement</h3>
<p><i>(This is Yair again): </p>
<p />I wish to invite you to join my online presentation (webinar) on &#8220;A Real-Time Trading System in MATLAB&#8221; in the upcoming <a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900">MATLAB Computational Finance Virtual Conference</a> next Thursday, September 19, 2013. I will be speaking at 2pm EST (8pm CEST). <a target="_blank" rel="nofollow" href="http://engage.vevent.com/registr_form.jsp?eid=1972&#038;seid=900&#038;language-code=en&#038;country-code=US&#038;page=1&#038;no-login=false">Registration</a> is free and it&#8217;s a virtual conference, so there&#8217;s no need for a tie and jacket&#8230; Following a half-hour presentation, I will be answering audience questions online. </p>
<p /> I gave an <a target="_blank" href="/articles/real-time-trading-system-demo/">earlier version of this presentation</a> at the Computational Finance Conference in New York on May 23, and you are welcome to look there for a preview. The presentation slides can be <a target="_blank" href="/files/Matlab%20real-time%20trading%20presentation.pdf">downloaded here</a>. Even if you&#8217;re not interested in real-time financial trading with Matlab, you might find it interesting to see the neat things that Matlab can do using a Java API interface and a few undocumented GUI tricks.</i><i><br />
<center><a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900"><img loading="lazy" decoding="async" alt="MATLAB Computational Finance Conference 2013" src="https://undocumentedmatlab.com/images/MATLAB_Computational_Finance_Conference_2013.jpg" title="MATLAB Computational Finance Conference 2013" width="600" height="112" /></a></center><br />
</i></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/variables-editor-scrolling">Variables Editor scrolling</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/accessing-the-matlab-editor" rel="bookmark" title="Accessing the Matlab Editor">Accessing the Matlab Editor </a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editormacro-assign-a-keyboard-macro-in-the-matlab-editor" rel="bookmark" title="EditorMacro &#8211; assign a keyboard macro in the Matlab editor">EditorMacro &#8211; assign a keyboard macro in the Matlab editor </a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/recovering-previous-editor-state" rel="bookmark" title="Recovering previous editor state">Recovering previous editor state </a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/variables-editor-scrolling/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Disabling menu entries in deployed docked figures</title>
		<link>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=disabling-menu-entries-in-deployed-docked-figures</link>
					<comments>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 14 Nov 2012 18:00:46 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Listeners]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Alexander Mering]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Docking]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[Menubar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3344</guid>

					<description><![CDATA[<p>Matlab's standard menu items can and should be removed from deployed  docked figures. This article explains how. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Last week I presented an <a target="_blank" href="/articles/removing-user-preferences-from-deployed-apps/">article</a> explaining how to solve an issue with deployed (compiled) Matlab applications. Today I&#8217;d like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/208302">Alexander Mering</a>, who will explain how to disable standard Matlab menu items in deployed docked Matlab figures. Alexander has been an avid follower of this blog, and from his <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/search_results?dur=all&#038;search_string=authorid%3A146471">CSSM posts</a> we can tell that he&#8217;s been heavily using advanced GUI features presented in this blog. His article today nicely shows how we can use different building-blocks presented in different articles in this blog, to achieve something new and useful.</i><br />
As Yair pointed out in many occasions, the power of Matlab could be greatly enhanced using the underlying Java mechanism. To me, while developing a larger standalone tool for technical calculations, these hints are worth a mint (as I guess for many of you).<br />
One of these very useful hints is the ability to <a target="_blank" href="/articles/docking-figures-in-compiled-applications/">dock figure windows in standalone applications</a>. This perfectly fits to my understanding of a &#8220;clean desktop&#8221;, i.e., having as less as possible separate windows. Since in many calculations dozens of figures are generated, the desktop gets up crowded very fast &#8211; if these are not grouped. So docking is essential (at least for me). Unfortunately there seems to be a serious problem with the resulting menu entries (at least in R2011b on Win XP), leading to a crash of the standalone application. Based on the posts by Yair, I will sketch a possible route to avoid this issue.</p>
<h3 id="symptom">The symptom</h3>
<p>In the compiled application, docking could be accomplished by accessing the figure frame&#8217;s underlying Java level:</p>
<pre lang='matlab'>
% get java frame for sophisticated modifications
jframe = get(handle(figure_handle), 'JavaFrame');
% allow docking
jframe.fHG1Client.setClientDockable(true)
</pre>
<p>Using this modification, the user is now allowed to dock and undock the figures manually. For initial docking of the figure,</p>
<pre lang='matlab'>javaFrame.fHG1Client.setClientWindowStyle(true,false)</pre>
<p>could be used during figure creation. Unfortunately, there are menu entries in the Figures container which are either unwanted (since not usable) or even directly crash the standalone applications:<br />
<span id="more-3344"></span><br />
<center><figure style="width: 320px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Useless Debug menu items in deployed applications" src="https://undocumentedmatlab.com/images/Useless_debug_menu.png" title="Useless Debug menu items in deployed applications" width="320" height="267" /><figcaption class="wp-caption-text">Useless Debug menu items in deployed applications</figcaption></figure></center><br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Menu items crashing deployed applications" src="https://undocumentedmatlab.com/images/Buggy_desktop_menu.png" title="Menu items crashing deployed applications" width="504" height="241" /><figcaption class="wp-caption-text">Menu items crashing deployed applications</figcaption></figure></center><br />
Since crashing menu entries will be found and used by end-users (though these are somehow hidden), these prohibit the usage of the docking feature as long as these could be invoked. So how can we disable / remove these menu items?</p>
<h3 id="unsuccessful">The unsuccessful solution</h3>
<p>Unfortunately, the straight forward solution of getting the handle to the Figures containers&#8217; menu bar and remove the unwanted items does not work. The reason for this is the (to me unexpected behavior) that the menu bar seems to be rebuilt whenever a figure is docked/undocked.<br />
This is actually the same behavior that automatically rebuilds the Editor&#8217;s menu bar whenever an editor file is added/removed. The Editor container is basically the same docking container as the Figures container, as shown by Yair&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup"><i><b>setFigDockGroup</b></i> utility</a>:<br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Docking a figure in the Editor container (group)" src="https://undocumentedmatlab.com/images/setFigDockGroup.png" title="Docking a figure in the Editor container (group)" width="504" height="551" /><figcaption class="wp-caption-text">Docking a figure in the Editor container (group)</figcaption></figure></center><br />
Therefore, removing unwanted menu items only helps until the next figure docking/undocking. To make it even worse: also pressing any of the buttons within the document bar (if having more than one figure) somehow rebuilds the entire menu structure, reverting our changes. So the solution becomes a bit more complex.</p>
<h3 id="solution">The working solution</h3>
<p>For the working solution, many pieces presented by Yair should be put together. The first piece results from the question how to detect a dock/undock event. Since no such callback is defined, we need to use a property listener as Yair showed in his post about the <a target="_blank" href="/articles/continuous-slider-callback/">continuous slider callback</a>:</p>
<pre lang='matlab'>
% listen to the WindowStyle property to detect docking / undocking events
hProp = findprop(handle(figure_handle),'WindowStyle');  % a schema.prop object
% if the event occurs, invoke the callback
hlistener = handle.listener(handle(figure_handle), hProp, 'PropertyPostSet',{@(source, event) Callback_DockingFcn});
% attach listener to the GUI since it needs to be known (as long as the figure exists)
setappdata(figure_handle, 'Handle_Listener', hlistener);
</pre>
<p>Now, whenever the figure&#8217;s <b>WindowStyle</b> property (which controls the docking state) is changed, our docking callback is invoked.<br />
The next piece of the puzzle takes care of the menu rebuild whenever any document bar button is pressed. To overcome this behavior, the idea is to define the <b>MousePressed</b> callback of theses buttons to (again) invoke the docking callback. This is necessary for two reasons: First, pressing the button (i.e., changing the current figure) rebuilds the menu, overwriting our changed menu entries. Secondly, all other buttons are also somehow rebuilt and the callbacks are removed if a new figure is docked.<br />
The handles to the document bar buttons could be found using Yair&#8217;s <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We have already seen that the Editor container is analogous to the Figures docking container. So let&#8217;s use the method described by Yair for <a target="_blank" href="/articles/accessing-the-matlab-editor/">accessing the Editor container</a>, to access the Figures container:</p>
<pre lang='matlab'>
figures_container = javaObjectEDT(matlab_instance.getGroupContainer('Figures'));
figures_frame = javaObjectEDT(figures_container.getTopLevelAncestor);
</pre>
<p>Once we get the Java Frame for the Figures container, the buttons could be found by digging through its children. This finally allows to set the callback using</p>
<pre lang='matlab'>
DTDocumentBar = javaObjectEDT(figures_frame.getRootPane.getLayeredPane.getComponent(1).getComponent(1).getComponent(0).getComponent(0).getComponent(1).getComponent(0));
ContentPanel = javaObjectEDT(DTDocumentBar.getComponent(0).getComponent(0).getViewport.getView);
if ~isempty(ContentPanel.getComponents) % less than two documents are open and no DTDocumentbar exists
    drawnow; pause(0.05)
    GroupPanel = javaObjectEDT(ContentPanel.getComponent(0));
    GroupPanel_Elements = javaObjectEDT(GroupPanel.getComponents);
    % change the MousePressed Callback for each of the buttons to invoke the function which disables the menu
    for n = 1 : GroupPanel.getComponentCount
        thisElement = GroupPanel_Elements(n);
        if isequal(char(thisElement.getClass.toString), 'class com.mathworks.widgets.desk.DTDocumentBar$DocumentButton')
            set(handle(thisElement, 'CallbackProperties'), 'MousePressedCallback', {@(source, event) Cbrake_Callback_Diagrams_DockingFcn})
        end
    end
    drawnow; pause(0.05)
end
</pre>
<p>where the loop runs through the current buttons in the document bar.<br />
As the last step of our procedure, we finally remove (or disable) the menu entries which are unwanted. This is achieved by extracting the handle to the Figures menu by:</p>
<pre lang='matlab'>figures_menu = javaObjectEDT(figures_frame.getJMenuBar);</pre>
<p>Running through the menu items, searching for the unwanted entries (as long as they have pre-defined <a target="_blank" href="/articles/modifying-default-toolbar-menubar-actions/">menu-item names</a>) at the end sets us into the position to take care of the menu items:</p>
<pre lang='matlab'>
% run through top-level menu items
for n = 1 : figures_menu.getMenuCount
    % completely deactivate Debugging options
    if isequal(char(figures_menu.getMenu(n-1).getName), 'DesktopDebugMenu')
        DesktopDebugMenuPos = n - 1;
    end
    % Remove some items from the Desktop menu
    if isequal(char(figures_menu.getMenu(n-1).getName), 'DesktopMenu')
        desktop_menu = javaObjectEDT(figures_menu.getMenu(n-1));
        DeletePos = [];
        for m = 1: desktop_menu.getMenuComponentCount
            if ismember({char(desktop_menu.getMenuComponent(m-1).getName)}, ...
                        {'ToggleFigure PaletteCheckBoxMenuItem', 'TogglePlot BrowserCheckBoxMenuItem', 'ToggleProperty EditorCheckBoxMenuItem'})
                DeletePos(end+1) = m - 1;
            end
        end
        for m = length(DeletePos) : -1 : 1
            desktop_menu.remove(DeletePos(m))
        end
    end
end
% finally remove the "Debug" menu
if ~isempty(DesktopDebugMenuPos)
    figures_menu.remove(DesktopDebugMenuPos)
end
</pre>
<p>Since this callback is invoked whenever a figure is docked/undocked, or the currently shown figure is changed (by pressing the document bar button), all unwanted menu items within the Figures menu could be removed.<br />
As a result, the new Figures container menu looks like:<br />
<center><figure style="width: 319px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Deployed menu without unwanted items" src="https://undocumentedmatlab.com/images/Reduced_figures_menu.png" title="Deployed menu without unwanted items" width="319" height="205" /><figcaption class="wp-caption-text">Deployed menu without unwanted items</figcaption></figure></center></p>
<h3 id="remarks">Remarks</h3>
<p>I must admit that above solution is still imperfect. For instance, sometimes there is a larger delay between the docking (or button press event) and the removing of the menu item. Nevertheless, this solution allows me to distribute my standalone with docked figures without having menu items directly leading to a fatal error.<br />
Obviously, the solution has some positive side effects:</p>
<ul>
<li>As could be seen from the screen shot, the Matlab desktop becomes available also within your compiled applications. This might be wanted. If not, it could be removed the same way as the other menu items. One drawback of making the desktop available should be mentioned: In my tests, the standalone Matlab desktop shows the whole list of recent files I have in the Matlab editor at compile time. This is somehow ugly but not that problematic.</li>
<li>Additional menu items could be added, giving more possibilities for modifications.</li>
</ul>
<p>I have uploaded a first version of the docking and creation functions, together with a small test project, to the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/39026-figures-menu-adaption-for-standalone-applications">Matlab file Exchange</a>. Readers are welcome to download the code and send me improvement suggestions. Or you could simply <a href="/articles/disabling-menu-entries-in-deployed-docked-figures#respond">leave a comment</a> below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
		<item>
		<title>Uitable cell colors</title>
		<link>https://undocumentedmatlab.com/articles/uitable-cell-colors?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uitable-cell-colors</link>
					<comments>https://undocumentedmatlab.com/articles/uitable-cell-colors#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 27 Oct 2011 01:51:53 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JIDE]]></category>
		<category><![CDATA[uitable]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2476</guid>

					<description><![CDATA[<p>A few Java-based customizations can transform a plain-looking data table into a lively colored one. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uitable-cell-colors">Uitable cell colors</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/uitable-customization-report" rel="bookmark" title="Uitable customization report">Uitable customization report </a> <small>Matlab's uitable can be customized in many different ways. A detailed report explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitable-sorting" rel="bookmark" title="Uitable sorting">Uitable sorting </a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/multi-line-uitable-column-headers" rel="bookmark" title="Multi-line uitable column headers">Multi-line uitable column headers </a> <small>Matlab uitables can present long column headers in multiple lines, for improved readability. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitab-colors-icons-images" rel="bookmark" title="Uitab colors, icons and images">Uitab colors, icons and images </a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A client recently asked me to develop an application that will help discover technical indicators for trading on the stock market. The application was very data-intensive and the analysis naturally required visual presentations of number-crunching results in a readable manner. One of the requirements was to present numeric results in a data table, so we naturally use <i><b>uitable</b></i> for this.<br />
Today I will show how using some not-so-complex Java we can transform the standard Matlab <i><b>uitable</b></i> into something much more useful.</p>
<h3 id="basic">First pass &#8211; basic data table</h3>
<p>We start by displaying the data in a simple <i><b>uitable</b></i>. The essence of the relevant code snippet was something like this:</p>
<pre lang='matlab'>
headers = {'Periods', ...
           '<html><center>Any period<br />returns</center></html>', ...
           '<html><center>Avg return<br />signal</center></html>', ...
           '<html><center>Avg<br />gain</center></html>', ...
           '<html><center>Avg<br />draw</center></html>', ...
           '<html><center>Gain/draw<br />ratio</center></html>', ...
           '<html><center>Max<br />gain</center></html>', ...
           '<html><center>Max<br />draw</center></html>', ...
           '<html><center>Random<br />% pos</center></html>', ...
           '<html><center>Signal<br />% pos</center></html>', ...
           'Payout', '% p-value'};
hTable = uitable('Data',data, 'ColumnEditable',false, ...
           'ColumnName',headers, 'RowName',[], ...
           'ColumnFormat',['numeric',repmat({'bank'},1,11)], ...
           'ColumnWidth','auto', ...
           'Units','norm', 'Position',[0,.75,1,.25]);
</pre>
<p><center><figure style="width: 596px" class="wp-caption aligncenter"><img decoding="async" alt="Plain data table - so boooooring..." src="https://undocumentedmatlab.com/images/uitable_plain1.png" title="Plain data table - so boooooring..." width="596" /><figcaption class="wp-caption-text">Plain data table - so boooooring...</figcaption></figure></center><br />
We can see from this simple example how I have used HTML to format the header labels into two rows, to enable compact columns. I have already <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">described</a> using HTML formatting in Matlab controls in several articles on this website. I have not done this here, but you can easily use HTML formatting for other effect such as superscript (&lt;sup&gt;), subscript (&lt;sub&gt;), bold (&lt;b&gt;), italic (&lt;i&gt;), font sizes and colors (&lt;font&gt;) and other standard HTML effects.<br />
Even with the multi-line headers, the default column width appears to be too wide. This is apparently an internal Matlab bug, not taking HTML into consideration. This causes only part of the information to be displayed on screen, and requires the user to either scroll right and left, or to manually resize the columns.</p>
<h3 id="auto-resizing">Second pass &#8211; auto-resizing that actually works&#8230;</h3>
<p>To solve the auto-resizing issue, we resort to a bit of Java magic powder. We start by using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a> to get the table&#8217;s underlying Java reference handle. This is the containing scrollpane, and we are interested in the actual data table inside. We then use the <i>setAutoResizeMode</i>, as <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setAutoResizeMode%28int%29">described</a> in the official Java documentation.</p>
<pre lang='matlab'>
jScroll = findjobj(hTable);
jTable = jScroll.getViewport.getView;
jTable.setAutoResizeMode(jTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
</pre>
<p><center><figure style="width: 596px" class="wp-caption aligncenter"><img decoding="async" alt="Auto-resized columns that actually work" src="https://undocumentedmatlab.com/images/uitable_plain2.png" title="Auto-resized columns that actually work" width="596" /><figcaption class="wp-caption-text">Auto-resized columns that actually work</figcaption></figure></center></p>
<h3 id="colors">Third pass &#8211; adding colors</h3>
<p>There are still quite a few other customizations needed here: enable <a target="_blank" href="/articles/uitable-sorting/">sorting</a>; remove the border outline; set a white background; set row (rather than cell) selection and several other fixes that may seem trivial by themselves but together giver a much more stylish look to the table&#8217;s look and feel. I&#8217;ll skip these for now (interested readers can read all about them, and more, in my detailed <a target="_blank" href="/articles/uitable-customization-report/"><i><b>uitable</b></i> customization report</a>).<br />
One of the more important customizations is to add colors depending on the data. In my client&#8217;s case, there were three requirements:</p>
<ul>
<li>data that could be positive or negative should be colored in <b><font color="green">green</font></b> or <b><font color="red">red</font></b> foreground color respectively</li>
<li>large payouts (abs &gt; 2) should be colored in <b><font color="blue">blue</font></b></li>
<li>data rows that have statistical significance (%p-value &lt; 5) should have a <span style="background-color:#ffff00;">yellow background highlight</span></li>
</ul>
<p>While we could easily use HTML or CSS formatting to do this, this would be bad for performance in a large data table, may cause some issues when editing table cells or sorting columns. I chose to use the alternative method of <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer">cell renderers</a>.<br />
<code>ColoredFieldCellRenderer</code> is a simple table cell renderer that enables setting cell-specific foreground/background colors and tooltip messages (it also has a few other goodies like smart text alignment etc.). This requires some Java knowledge to program, but in this case you can simply <a href="/files/ColoredFieldCellRenderer.zip">download</a> the ColoredFieldCellRenderer.zip file and use it, even if you don&#8217;t know Java. The source code is included in the zip file, for anyone who is interested.<br />
After using <i><b>javaaddpath</b></i> to add the zip file to the dynamic Java classpath (you can add it to the static <i>classpath.txt</i> file instead), the contained Java class file is available for use in Matlab. We configure it according to our data and then assign it to all our table&#8217;s columns.<br />
Java savvy readers might complain that the data-processing should perhaps be done in the renderer class rather than in Matlab. I have kept it in Matlab because this would enable very easy modification of the highlighting algorithm, without any need to modify the generic Java renderer class.<br />
Unfortunately, in the new <i><b>uitable</b></i> design (the version available since R2008a), JIDE and Matlab have apparently broken the <a target="_blank" rel="nofollow" href="http://java.sun.com/products/jfc/tsc/articles/architecture">standard MVC approach</a> by using a table model that not only controls the data but also sets the table&#8217;s appearance (row-striping background colors, for example), and disregards column cell-renderers. In order for our custom cell renderer to have any effect, we must therefore replace Matlab&#8217;s standard <code>DefaultUIStyleTableModel</code> with a simple <code>DefaultTableModel</code>. This in itself is not enough – we also need to ensure that the <i><b>uitable</b></i> has an empty <strong>ColumnFormat</strong> property, because if it is non-empty then it overrides our cell-renderer.<br />
In the following code, note that Java row and column indices start at 0, not at 1 like in Matlab. We need to be careful about indices when programming java in Matlab.<br />
The rest of the code should be pretty much self explanatory (again &#8211; more details can be found in the above-mentioned report):</p>
<pre lang='matlab'>
% Initialize our custom cell renderer class object
javaaddpath('ColoredFieldCellRenderer.zip');
cr = ColoredFieldCellRenderer(java.awt.Color.white);
cr.setDisabled(true);  % to bg-color the entire column
% Set specific cell colors (background and/or foreground)
for rowIdx = 1 : size(data,1)
  % Red/greed foreground color for the numeric data
  for colIdx = 2 : 8
    if data(rowIdx,colIdx) < 0
      cr.setCellFgColor(rowIdx-1,colIdx-1,java.awt.Color(1,0,0));    % red
    elseif data(rowIdx,colIdx) > 0
      cr.setCellFgColor(rowIdx-1,colIdx-1,java.awt.Color(0,0.5,0));  % green
    end
  end
  % Yellow background for significant rows based on p-value
  if data(rowIdx,12) < = 5 &#038;&#038; data(rowIdx,11)~=0
    for colIdx = 1 : length(headers)
      cr.setCellBgColor(rowIdx-1,colIdx-1,java.awt.Color(1,1,0));    % yellow
    end
  end
  % Bold blue foreground for significant payouts
  if abs(data(rowIdx,11)) >= 2
    cr.setCellFgColor(rowIdx-1,10,java.awt.Color(0,0,1));    % blue
    % Note: the following could easily be done in the renderer, just like the colors
    boldPayoutStr = ['<html><b>' num2str(data(rowIdx,11),'%.2f') '</b></html>'];
    %jTable.setValueAt(boldPayoutStr,rowIdx-1,10);  % this is no good: overridden when table model is replaced below...
    dataCells{rowIdx,11} = boldPayoutStr;
  end
end
% Replace Matlab's table model with something more renderer-friendly...
jTable.setModel(javax.swing.table.DefaultTableModel(dataCells,headers));
set(hTable,'ColumnFormat',[]);
% Finally assign the renderer object to all the table columns
for colIdx = 1 : length(headers)
  jTable.getColumnModel.getColumn(colIdx-1).setCellRenderer(cr);
end
</pre>
<p><center><figure style="width: 590px" class="wp-caption aligncenter"><img decoding="async" alt="Finally something lively!" src="https://undocumentedmatlab.com/images/uitable_colored.png" title="Finally something lively!" width="590" /><figcaption class="wp-caption-text">Finally something lively!</figcaption></figure></center><br />
This may not take a beauty contest prize, but you must admit that it now looks more useful than the original table at the top of this article.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uitable-cell-colors">Uitable cell colors</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/uitable-customization-report" rel="bookmark" title="Uitable customization report">Uitable customization report </a> <small>Matlab's uitable can be customized in many different ways. A detailed report explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitable-sorting" rel="bookmark" title="Uitable sorting">Uitable sorting </a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/multi-line-uitable-column-headers" rel="bookmark" title="Multi-line uitable column headers">Multi-line uitable column headers </a> <small>Matlab uitables can present long column headers in multiple lines, for improved readability. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitab-colors-icons-images" rel="bookmark" title="Uitab colors, icons and images">Uitab colors, icons and images </a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/uitable-cell-colors/feed</wfw:commentRss>
			<slash:comments>124</slash:comments>
		
		
			</item>
		<item>
		<title>Tri-state checkbox</title>
		<link>https://undocumentedmatlab.com/articles/tri-state-checkbox?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tri-state-checkbox</link>
					<comments>https://undocumentedmatlab.com/articles/tri-state-checkbox#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 19 Oct 2011 14:04:20 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[JIDE]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2467</guid>

					<description><![CDATA[<p>Matlab checkboxes can easily be made to support tri-state functionality.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/tri-state-checkbox">Tri-state checkbox</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/unorthodox-checkbox-usage" rel="bookmark" title="Unorthodox checkbox usage">Unorthodox checkbox usage </a> <small>There are various ways to display interactive split-panes in Matlab GUI. Uiextras flex-panels override a checkbox control's CData to display a divider. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/determining-axes-zoom-state" rel="bookmark" title="Determining axes zoom state">Determining axes zoom state </a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/recovering-previous-editor-state" rel="bookmark" title="Recovering previous editor state">Recovering previous editor state </a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/checkboxlist" rel="bookmark" title="CheckboxList">CheckboxList </a> <small>Matlab listboxes can be enhanced with checkboxes next to each item. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>When presenting information visually in graphical user interfaces (GUIs), we often need to present and enable interaction with state data (such as On/Off). In most cases, we would naturally use a checkbox to present the information and enable interaction. But What can we do if the data has three possible states. For example, Yes/No/Maybe, or: Full/Empty/Partial, or: Up/Down/Undetermined ?<br />
Until today, Matlab GUIs had to resort to using drop-down (aka combo-box or popup-menu) or radio-button controls to present such information. However, would it not be nicer if we could still use a checkbox? Outside Matlab, such a control is known as a tri-state checkbox and many modern GUI frameworks support it. Well, surprise surprise, so does Matlab (although you would never guess it from the documentation).</p>
<h3 id="CheckBoxTree">CheckBoxTree</h3>
<p>Last year, I have already <a target="_blank" href="/articles/customizing-uitree-nodes-2/#Built-in-classes">described</a> a built-in Matlab tree control whose nodes have tri-state checkboxes:<br />
<center><figure style="width: 311px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="a regular MJTree (left) and a CheckBoxTree (right)" src="https://undocumentedmatlab.com/images/CheckBoxTree.png" title="a regular MJTree (left) and a CheckBoxTree (right)" width="311" height="218" /><figcaption class="wp-caption-text">a regular MJTree (left) and a CheckBoxTree (right)</figcaption></figure></center><br />
Today I will show how we can use these checkboxes as independent GUI controls.</p>
<h3 id="uicontrol">Modifying the standard Matlab checkbox uicontrol</h3>
<p>In order to modify the standard Matlab checkbox <i><b>uicontrol</b></i>, we need to first get its underlying Java component reference. This is done using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We then update its UI wrapper to be the same as the <code>CheckBoxTree</code>&#8216;s checkbox control.<br />
To programmatically set a mixed state we update the &#8216;selectionState&#8217; client property to <code>SelectionState.MIXED</code> (<code>SelectionState</code> also has the <code>SELECTED</code> and <code>NOT_SELECTED</code> values).<br />
Here is an end-to-end example:</p>
<pre lang='matlab'>
hCB = uicontrol('Style','checkbox', ...);
jCB = findjobj(hCB);
jCB.setUI(com.mathworks.mwswing.checkboxtree.TriStateButtonUI(jCB.getUI));
% Update the checkbox state to MIXED
newState = com.mathworks.mwswing.checkboxtree.SelectionState.MIXED;
jCB.putClientProperty('selectionState', newState);
jCB.repaint;
</pre>
<p><center><figure style="width: 289px" class="wp-caption aligncenter"><img decoding="async" alt="Matlab checkboxes displaying mixed states" src="https://undocumentedmatlab.com/images/CheckBox_TriState.png" title="Matlab checkboxes displaying mixed states" width="289" /><figcaption class="wp-caption-text">Matlab checkboxes displaying mixed states</figcaption></figure></center></p>
<h3 id="independent">Displaying as an independent Java control</h3>
<p>Instead of retrofitting a standard Matlab <i><b>uicontrol</b></i> as described above, we can directly use the standard <code>javax.swing.JCheckBox</code> which does not normally support tri-state (it only has two states), displaying it in our GUI with the built-in <a target="_blank" href="/articles/javacomponent/"><i><b>javacomponent</b></i> function</a>:</p>
<pre lang='matlab'>
% Display the checkbox (UNSELECTED state at first)
jCB = javax.swing.JCheckBox('JCheckBox - mixed',0);
javacomponent(jCB, [10,70,150,20], gcf);
% Update the checkbox state to MIXED
import com.mathworks.mwswing.checkboxtree.*
jCB.setUI(TriStateButtonUI(jCB.getUI));
jCB.putClientProperty('selectionState', SelectionState.MIXED);
jCB.repaint;
</pre>
<p>Note that instead of using <code>javax.swing.JCheckBox</code>, we could use the internal Matlab class <code>com.mathworks.mwswing.MJCheckBox</code>, which directly extends <code>JCheckBox</code> and adds mnemonic (shortcut-key) support, but is otherwise fully compatible with <code>JCheckBox</code>. Actually, it is <code>MJCheckBox</code> which is the underlying Java component of the standard Matlab checkbox <i><b>uicontrol</b></i>.</p>
<h3 id="alternatives">Alternative controls</h3>
<p>Now that we have seen that Matlab includes built-in support (well, at least support in the technical sense, not the official customer-support sense), would you be surprised to learn that it includes similar support in other internal components as well?<br />
The internal Matlab class <code>com.mathworks.mwt.MWCheckbox</code> directly supports a tri-state (yes/no/maybe) checkbox, without any need to update its UI, as follows:</p>
<pre lang='matlab'>
% Display the checkbox (UNSELECTED state at first)
jCB = com.mathworks.mwt.MWCheckbox('MWCheckbox - mixed',0);
javacomponent(jCB, [10,70,150,20], gcf);
% Update the checkbox state to MIXED
jCB.setMixedState(true);
% Retrieve the current state
isMixedFlag = jCB.isMixedState();  % true/false
</pre>
<p><center><figure style="width: 289px" class="wp-caption aligncenter"><img decoding="async" alt="MJCheckBox vs. MWCheckbox" src="https://undocumentedmatlab.com/images/CheckBox_MWT_MWSwing.png" title="MJCheckBox vs. MWCheckbox" width="289" /><figcaption class="wp-caption-text">MJCheckBox vs. MWCheckbox</figcaption></figure></center><br />
Note that the State property, which controls the standard selected/unselected state, is entirely independent from the MixedState property. Both State and MixedState are boolean properties, so to get the actual checkbox state we need to query both of these properties.<br />
Another internal Matlab class that we can use is JIDE&#8217;s <code>com.jidesoft.swing.TristateCheckBox</code> (which is pre-bundled in Matlab and is fully documented <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/TristateCheckBox.html">here</a>).<br />
There are many other tri-state checkbox alternatives available online (for example, <a target="_blank" rel="nofollow" href="https://forums.oracle.com/forums/search.jspa?threadID=&#038;q=%28tri-state+OR+tristate%29+AND+checkbox&#038;objID=c285&#038;dateRange=all&#038;userID=&#038;numResults=30&#038;rankBy=10001">here</a>, <a target="_blank" rel="nofollow" href="http://www.javaspecialists.eu/archive/Issue145.html">here</a> and <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1263323/tristate-checkboxes-in-java">here</a>). We can easily include them in our Matlab GUI with the <i><b>javacomponent</b></i> function. Using external components we can be more certain of the compatibility issues in past and future Matlab releases. On the other hand, internal Matlab classes do have the advantage of being inherently accessible on all platforms of the same Matlab release, whereas non-Matlab components must be included in our deployment package.<br />
Do you use any tri-state controls, either Matlab or external, in your work? If so, please share your experience in a <a href="/articles/tri-state-checkbox/#respond">comment below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/tri-state-checkbox">Tri-state checkbox</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/unorthodox-checkbox-usage" rel="bookmark" title="Unorthodox checkbox usage">Unorthodox checkbox usage </a> <small>There are various ways to display interactive split-panes in Matlab GUI. Uiextras flex-panels override a checkbox control's CData to display a divider. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/determining-axes-zoom-state" rel="bookmark" title="Determining axes zoom state">Determining axes zoom state </a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/recovering-previous-editor-state" rel="bookmark" title="Recovering previous editor state">Recovering previous editor state </a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/checkboxlist" rel="bookmark" title="CheckboxList">CheckboxList </a> <small>Matlab listboxes can be enhanced with checkboxes next to each item. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/tri-state-checkbox/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing uicontrol border</title>
		<link>https://undocumentedmatlab.com/articles/customizing-uicontrol-border?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-uicontrol-border</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-uicontrol-border#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 31 Oct 2010 19:05:17 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[FindJObj]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1947</guid>

					<description><![CDATA[<p>Matlab uicontrol borders can easily be modified - this article shows how</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uicontrol-border">Customizing uicontrol border</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Today, a CSSM user <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/295214">posted</a> the question how to modify the border of Matlab editboxes. There is of course no documented way to do this in plain Matlab. However, this is so easy to do using the edit-box&#8217;s undocumented underlying Java, that I decided to skip my regular weekly article schedule and post an out-of-band explanation.<br />
As the CSSM poster correctly figured out, we first need to get the uicontrol&#8217;s underlying Java reference. This is done using my <a target="_blank" href="/articles/findjobj-find-underlying-java-object/">FindJObj utility</a>. The next step is to get the Java object&#8217;s <b>Border</b> property:</p>
<pre lang="matlab">
>> hEdit = uicontrol('Style','Edit');  % create Matlab uicontrol
>> jEdit = findjobj(hEdit)  % get underlying Java reference
jEdit =
	javahandle_withcallbacks.com.mathworks.hg.peer.EditTextPeer$hgTextField
>> jEdit.Border  % check the control's border
ans =
com.sun.java.swing.plaf.windows.XPStyle$XPFillBorder@e5a137
>> jEdit.Border.get
	BorderOpaque = on
	Class = [ (1 by 1) java.lang.Class array]
	LineColor = [0.498039 0.615686 0.72549]
	RoundedCorners = off
	Thickness = [1]
	...
</pre>
<p>Unfortunately, as the CSSM poster has discovered, this <b>Border</b> property cannot be modified. Such an object is called <i>immutable</i> in Java; another common example is the <b>Font</b> property.<br />
However, we can easily replace the object&#8217;s <b>Border</b> with a custom-made border, as follows:</p>
<pre lang="matlab">
lineColor = java.awt.Color(1,0,0);  % =red
thickness = 3;  % pixels
roundedCorners = true;
newBorder = javax.swing.border.LineBorder(lineColor,thickness,roundedCorners);
jEdit.Border = newBorder;
jEdit.repaint;  % redraw the modified control
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with regular border" src="https://undocumentedmatlab.com/images/editbox1.png" title="editbox with regular border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with regular border</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with custom border" src="https://undocumentedmatlab.com/images/editbox4a.png" title="editbox with custom border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with custom border</figcaption></figure></center><br />
Much more complex and interesting borders can be created in much the same way. Interested readers are referred to the <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/border.html">official documentation of Java Borders</a> or any decent Swing textbook.<br />
We can use the same technique to remove the uicontrol&#8217;s borders altogether:</p>
<pre lang="matlab">
jEdit.Border = [];
</pre>
<p><center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="editbox with no border" src="https://undocumentedmatlab.com/images/editbox3.png" title="editbox with no border" width="90" height="30" /><figcaption class="wp-caption-text">editbox with no border</figcaption></figure></center><br />
Setting the border as shown above works in much the same manner for all Java uicontrols. Simply use <i><b>findjobj</b></i> to get the Java reference and then update its <b>Border</b> property.<br />
This article has described the <b>Border</b> property. Matlab uicontrols&#8217; underlying Java objects contain numerous other useful properties and methods that can greatly extend our Matlab GUI, and I intend to describe some of them in the upcoming months/years.<br />
Do you have any particular uicontrol customization that you would like me to describe? If so, please do <a href="/articles/customizing-uicontrol-border/#respond">post a comment</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uicontrol-border">Customizing uicontrol border</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-uicontrol-border/feed</wfw:commentRss>
			<slash:comments>15</slash:comments>
		
		
			</item>
		<item>
		<title>JIDE Property Grids</title>
		<link>https://undocumentedmatlab.com/articles/jide-property-grids?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jide-property-grids</link>
					<comments>https://undocumentedmatlab.com/articles/jide-property-grids#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 22 Apr 2010 00:37:52 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[JIDE]]></category>
		<category><![CDATA[Levente Hunyadi]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[UIInspect]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1287</guid>

					<description><![CDATA[<p>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/jide-property-grids">JIDE Property Grids</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/advanced-jide-property-grids" rel="bookmark" title="Advanced JIDE Property Grids">Advanced JIDE Property Grids </a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-jide-combo-boxes" rel="bookmark" title="Using JIDE combo-boxes">Using JIDE combo-boxes </a> <small>Matlab includes many custom JIDE combo-box controls that can be used in Matlab GUIs out of the box. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/60898">Levente Hunyadi</a>.</i></p>
<h3>Matlab&#8217;s property inspector</h3>
<p>We often wish to edit properties of heterogeneous objects using a common interface. Matlab&#8217;s property inspector, invoked with the built-in <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inspect.html"><i><b>inspect</b></i></a> function, answers this need. The inspector is based on a two-column table of property names and values. Properties and their values are populated automatically, and the user can edit values in-place. The inspector enables property categorization, sub-categorization and sorting, which help users find and modify properties easily. For each property, the inspector displays a matching edit control: editbox/combobox/checkbox etc. This simplifies property value editing and prevents illegal value entry. Matlab&#8217;s GUI builder, <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f7-998368.html">GUIDE</a>, uses the inspector to let users edit GUI properties such as position, color etc. It is also used by other tools such as the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f9-47085.html#f9-43456">Plot Editor</a>.<br />
<center><figure style="width: 355px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab's built-in property inspector" src="https://undocumentedmatlab.com/images/Inspector_mde_cropped.gif" title="Matlab's built-in property inspector" width="355" height="271" /><figcaption class="wp-caption-text">Matlab's built-in property inspector</figcaption></figure></center><br />
The Matlab inspector can be embedded, with not-too-much effort, within Matlab GUI applications. Examples of this can be found in the <a target="_blank" href="/articles/findjobj-gui-display-container-hierarchy/">FindJObj</a> and <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935">UIInspect</a> utilities.<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><a href="/images/findjobj.png" target="_blank"><img loading="lazy" decoding="async" alt="FindJObj - embedded property inspector" src="https://undocumentedmatlab.com/images/findjobj.png" title="FindJObj - embedded property inspector" width="450" height="405" /></a><figcaption class="wp-caption-text">FindJObj - embedded property inspector</figcaption></figure></center><br />
Unfortunately, Matlab&#8217;s property inspector is limited to Handle Graphics, Java and COM objects, and cannot be used for structures or user-defined Matlab classes. We shall see below how to set up our own property grid, populate it with data, and subscribe to property change events. This is a rather procedural approach. It is usually more convenient to use a declarative approach in which a structure or Matlab class is passed to a function that automatically discovers its properties and their meta-information. The <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28732">Property Grid</a> utility at Matlab File Exchange provides these services.</p>
<h3 id="simple">A simple property grid</h3>
<p>Matlab&#8217;s property inspector is based on a property grid control by <a target="_blank" rel="nofollow" href="http://www.jidesoft.com">JIDE Software</a>. JIDE Grids is a collection of components that extend the standard Java Swing <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html">JTable</a> component, and is included in each Matlab installation (/java/jarext/jide/jide-grids.jar under the Matlab root). In particular, JIDE Grids includes the <em>PropertyTable</em> class, which is a fully customizable property grid component. You can find further details on JIDE Grids in the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf">Developer Guide</a> and the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/">Javadoc documentation</a>.<br />
There are several related classes associated with the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyTable.html"><em>PropertyTable</em></a> class. First, a <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyTableModel.html"><em>PropertyTableModel</em></a> encapsulates all properties that are visualized in the property grid. Each property derives from the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/Property.html"><em>Property</em></a> abstract class, which features some common actions to properties, most notably to get and set property value. <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/DefaultProperty.html"><em>DefaultProperty</em></a> is a default concrete subclass of <em>Property</em>. Finally, <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyPane.html"><em>PropertyPane</em></a> decorates a property grid with icons for changing category view to alphabetically sorted view as well as expanding and collapsing categories, and a description text box at the bottom that can be shown or hidden.<br />
Here are the <i>DefaultProperty</i> fields and their respective roles:</p>
<table>
<tbody>
<tr>
<th>Field</th>
<th>Role</th>
</tr>
<tr>
<td>Name</td>
<td>Interal property name, not necessarily displayed, used as a key to identify the property.</td>
</tr>
<tr>
<td>DisplayName</td>
<td>A short property name shown in the left column of the property grid.</td>
</tr>
<tr>
<td>Description</td>
<td>A concise description of the property, shown at the bottom of the property pane, below the grid.</td>
</tr>
<tr>
<td>Type</td>
<td>The Java type associated with the property, used to invoke the appropriate renderer or editor.</td>
</tr>
<tr>
<td>EditorContext</td>
<td>An editor context object. If set, both the type and the context are used to look up the renderer or editor to use. This lets, for instance, one flag value to display as a true/false label, while another as a checkbox.</td>
</tr>
<tr>
<td>Category</td>
<td>A string specifying the property&#8217;s category, for grouping purposes.</td>
</tr>
<tr>
<td>Editable</td>
<td>Specifies whether the property value is modifiable or read-only.</td>
</tr>
<tr>
<td>Value</td>
<td>The current property value, as a Java object.</td>
</tr>
</tbody>
</table>
<p>Just as with any Java object, these fields may either be accessed with the Java get/set semantics (e.g. <i>getName()</i> or <i>setName(name)</i>), or the Matlab <i><b>get/set</b></i> semantics (e.g. <i><b>get</b>(prop,&#8217;Name&#8217;)</i> or <i><b>set</b>(prop,&#8217;Name&#8217;,name)</i>). When using the Matlab syntax, remember to wrap the Java object in a <i><b>handle()</b></i> call, to prevent a <a target="_blank" href="/articles/uicontrol-callbacks/#memory_leak">memory leak</a>.<br />
To use a property grid in Matlab, first construct a set of <i>DefaultProperty</i> objects. For each object, set at least the name, type and initial value. Next, add the properties to a table model. Finally, construct a property grid with the given table model and encapsulate in a property pane:</p>
<pre lang="matlab">
% Initialize JIDE's usage within Matlab
com.mathworks.mwswing.MJUtilities.initJIDE;
% Prepare the properties list
list = java.util.ArrayList();
prop1 = com.jidesoft.grid.DefaultProperty();
prop1.setName('stringProp');
prop1.setType(javaclass('char',1));
prop1.setValue('initial value');
prop1.setCategory('My Category');
prop1.setDisplayName('Editable string property');
prop1.setDescription('A concise description for my property.');
prop1.setEditable(true);
list.add(prop1);
prop2 = com.jidesoft.grid.DefaultProperty();
prop2.setName('flagProp');
prop2.setType(javaclass('logical'));
prop2.setValue(true);
prop2.setCategory('My Category');
prop2.setDisplayName('Read-only flag property');
prop2.setEditable(false);
list.add(prop2);
% Prepare a properties table containing the list
model = com.jidesoft.grid.PropertyTableModel(list);
model.expandAll();
grid = com.jidesoft.grid.PropertyTable(model);
pane = com.jidesoft.grid.PropertyPane(grid);
% Display the properties pane onscreen
hFig = figure;
panel = uipanel(hFig);
javacomponent(pane, [0 0 200 200], panel);
% Wait for figure window to close & display the prop value
uiwait(hFig);
disp(prop1.getValue());
</pre>
<p>Here, <em>com.mathworks.mwswing.MJUtilities.initJIDE</em> is called to initialize JIDE&#8217;s usage within Matlab. Without this call, we may see a JIDE warning message in some Matlab releases. We only need to <em>initJIDE</em> once per Matlab session, although there is no harm in repeated calls.<br />
<i><b>javaclass</b></i> is a function (included in the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28732">Property Grid</a> utility, or directly downloadable from <a target="_blank" rel="nofollow" href="/files/javaclass.m">here</a>) that returns a Java class for the corresponding Matlab type with the given dimension: <em><b>javaclass</b>(&#8216;logical&#8217;)</em> or <em><b>javaclass</b>(&#8216;logical&#8217;,0)</em> (a single logical flag value) returns a <em>java.lang.Boolean</em> class; <em><b>javaclass</b>(&#8216;char&#8217;,1)</em> (a character array) returns a <em>java.lang.String</em> class; <em><b>javaclass</b>(&#8216;double&#8217;,2)</em> (a matrix of double-precision floating point values) returns <em>double[][]</em>.<br />
<i><b>javacomponent</b></i> is the undocumented built-in Matlab function that adds Java Swing components to a Matlab figure, using the given dimensions and parent handle. When the user closes the figure, prop.<em>getValue()</em> fetches and displays the new property value.<br />
<center><figure style="width: 302px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="A simple user-defined property grid" src="https://undocumentedmatlab.com/images/PropertyGrid_simple2.png" title="A simple user-defined property grid" width="302" height="294" /><figcaption class="wp-caption-text">A simple user-defined property grid</figcaption></figure></center><br />
Next week&#8217;s article will show how to add more complex renderers and editors (display the flag value as a checkbox for example), define nested properties, and subscribe to property value change events. So stay tuned&#8230;</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/jide-property-grids">JIDE Property Grids</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/advanced-jide-property-grids" rel="bookmark" title="Advanced JIDE Property Grids">Advanced JIDE Property Grids </a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-jide-combo-boxes" rel="bookmark" title="Using JIDE combo-boxes">Using JIDE combo-boxes </a> <small>Matlab includes many custom JIDE combo-box controls that can be used in Matlab GUIs out of the box. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/jide-property-grids/feed</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		
			</item>
		<item>
		<title>Additional uicontrol tooltip hacks</title>
		<link>https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=additional-uicontrol-tooltip-hacks</link>
					<comments>https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 17 Feb 2010 17:25:52 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Matt Whitaker]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1114</guid>

					<description><![CDATA[<p>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks">Additional uicontrol tooltip hacks</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-matlab-uicontrol-tooltips" rel="bookmark" title="Spicing up Matlab uicontrol tooltips">Spicing up Matlab uicontrol tooltips </a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/listbox-selection-hacks" rel="bookmark" title="Listbox selection hacks">Listbox selection hacks </a> <small>Matlab listbox selection can be customized in a variety of undocumented ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/inactive-control-tooltips-event-chaining" rel="bookmark" title="Inactive Control Tooltips &amp; Event Chaining">Inactive Control Tooltips &amp; Event Chaining </a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Once again I&#8217;d like to welcome guest blogger Matthew Whitaker. Today Matt will discuss uicontrol tooltips hacks as the first of several posts that follow a logical thread culminating in the long promised article on the EDT.</i><br />
<br />
A while back Yair wrote a cool article, <a target="_blank" href="/articles/spicing-up-matlab-uicontrol-tooltips/">Spicing up Matlab uicontrol tooltips</a>, describing use of HTML formatting and images in <b><i>uicontrol</i></b> tooltips. I want to share some limitations I&#8217;ve seen with tooltips and their solution using the Matlab control&#8217;s underlying Java object.</p>
<h3 id="disabled-controls">Situation 1: Displaying a tooltip on disabled controls</h3>
<p>One issue with the stock Matlab <b><i>uicontrol</i></b> tooltips is that if you turn the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol_props.html#bqxoiky">uicontrol&#8217;s <b>Enable</b> property</a> to &#8216;inactive&#8217; or &#8216;off&#8217;, its tooltip no longer displays. This is the behavior that we normally want, but occasionally we wish to display a tooltip on a disabled control, for example, to explain why the control is disabled.<br />
You can use the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><b><i>findjobj</i></b> utility</a> to find the Java handle for the <b><i>uicontrol</i></b>. This handle can then be used to set the tooltip text. The tooltip will display if you disable the control using its Java handle&#8217;s <b>Enabled</b> property rather than the Matlab handle&#8217;s <b>Enable</b> property:</p>
<pre lang="matlab">
hButton = uicontrol('String','Button');
drawnow;
jButton= findjobj(hButton);
set(jButton,'Enabled',false);
set(jButton,'ToolTipText','This is disabled for a reason');
</pre>
<p>As customary for Java objects, its properties can also be set using their corresponding accessor methods:</p>
<pre lang="matlab">
javaMethodEDT('setEnabled',jButton,false);
javaMethodEDT('setToolTipText',jButton,'Button is disabled for a reason');
</pre>
<p><center><figure style="width: 252px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="tooltip on a disabled uicontrol" src="https://undocumentedmatlab.com/images/tooltip1.png" title="tooltip on a disabled uicontrol" width="252" height="46" /><figcaption class="wp-caption-text">tooltip on a disabled uicontrol</figcaption></figure></center><br />
Unfortunately, this hack does not work for &#8216;inactive&#8217; controls. There is no direct Java analogy for inactive controls &#8211; it&#8217;s a Matlab extension. It appears that Matlab somehow intercepts the mouse events associated with inactive controls. My next post will show how event callback can be used to display tooltips for inactive controls.<br />
As an alternative for inactive edit-box controls, we can simulate the inactive behavior by setting the Java object&#8217;s <b>Editable</b> property (or by using its <i>setEditable()</i> accessor method), then setting the tooltip. Note that the extremely-useful Java <b>Editable</b> property is unavailable in the Matlab handle, for some inexplicable reason:</p>
<pre lang="matlab">
hEditbox = uicontrol('String','Edit Text','Style','edit');
drawnow;
jEditbox = findjobj(hEditbox);
set(jEditbox,'Editable',false);
set(jEditbox,'ToolTipText','Text is inactive for a reason');
</pre>
<p><center><figure style="width: 232px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="tooltip on a non-editable editbox" src="https://undocumentedmatlab.com/images/tooltip2.png" title="tooltip on a non-editable editbox" width="232" height="50" /><figcaption class="wp-caption-text">tooltip on a non-editable editbox</figcaption></figure></center></p>
<h3 id="truncated_text">Situation 2: Displaying a tooltip on truncated text</h3>
<p>If we want to conditionally display a tooltip for an editbox <b><i>uicontrol</i></b> when the text exceeds the control&#8217;s width, we can use the <b>TipWhenTruncatedEnabled</b> property (or its corresponding <i>setTipWhenTruncatedEnabled()</i> method). This will display a tooltip with the editbox contents if the string is shown truncated. This saves the user having to scroll through the control to see its contents. I often use this for edit controls that may contain long path names:</p>
<pre lang="matlab">
hEditbox(1) = uicontrol('Style','edit','Units','norm','Pos',[0.1,0.8,0.4,0.05], 'String','Too Short');
hEditbox(2) = uicontrol('Style','edit','Units','norm','Pos',[0.1,0.7,0.2,0.05], 'String','Long Enough to Display a Tool Tip');
drawnow;
jEditbox1 = findjobj(hEditbox(1));
jEditbox2 = findjobj(hEditbox(2));
set(jEditbox1,'TipWhenTruncatedEnabled',true);  % property-based alternative
javaMethod('setTipWhenTruncatedEnabled',jEditbox2,true);  % method-based alternative
</pre>
<p><center><figure style="width: 250px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="TipWhenTruncatedEnabled tooltip" src="https://undocumentedmatlab.com/images/tooltip3.png" title="TipWhenTruncatedEnabled tooltip" width="250" height="100" /><figcaption class="wp-caption-text">TipWhenTruncatedEnabled tooltip</figcaption></figure></center><br />
The <b>TipWhenTruncatedEnabled</b> property property is also available for multi-line editboxes, but has (obviously) no effect when scrollbars are present. Also note that setting the <b>TipWhenTruncatedEnabled</b> property to true overrides any previous tooltip that might have been set for the editbox.<br />
Finally, note that the <b>TipWhenTruncatedEnabled</b> property can also be set for the editbox component of popup-menu (aka drop-down) controls, <u>after</u> they have been set to be editable using their Java <b>Editable</b> property (note that both properties are false by default for Matlab uicontrols). In the following screenshot, the drop-down&#8217;s editbox component contained an HTML snippet, that is shown unformatted within the edit-box and HTML-formatted in the de-truncated tooltip:<br />
<center><figure style="width: 115px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="de-truncated HTML-format tooltip" src="https://undocumentedmatlab.com/images/popup_TipWhenTruncatedEnabled2.png" title="de-truncated HTML-format tooltip" width="115" height="72" /><figcaption class="wp-caption-text">de-truncated HTML-format tooltip</figcaption></figure></center></p>
<h3 id="tooltip_timing">Situation 3: Controlling tooltip timing</h3>
<p>As you have probably noticed, there is a slight delay between the time your mouse enters the control and when the tooltip actually appears. If you display a tooltip over a control for sufficiently long the tooltip will then disappear. Sometimes the default delays are too slow or fast for your application. These times can be controlled through the <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/ToolTipManager.html">javax.swing.ToolTipManager</a>. The ToolTipManager sets these parameters globally (including for your Matlab desktop components), but they are not persistent between sessions.<br />
Some examples using the ToolTipManager:</p>
<pre lang="matlab">
btn = uicontrol('String','Button','Tooltip','This is a button.','Pos',[100,100,75,25]);
txt = uicontrol('Style','edit','String','Edit Text','Tooltip','This is editable text','Pos',[100,50,75,25]);
tm = javax.swing.ToolTipManager.sharedInstance; %static method to get ToolTipManager object
initialDelay = javaMethodEDT('getInitialDelay',tm); %get the delay before display in milliseconds (=750 on my system)
javaMethodEDT('setInitialDelay',tm,0); %set tooltips to appear immediately
dismissDelay = javaMethodEDT('getDismissDelay',tm); %get the delay before the tooltip disappears (=10000 (10 sec) on my system)
javaMethodEDT('setDismissDelay',tm,2000); %set the dismiss delay to 2 seconds
javaMethodEDT('setEnabled',tm,false); %turns off all tooltips in system (including the Matlab desktop)
javaMethodEDT('setEnabled',tm,true);
javaMethodEDT('setInitialDelay',tm,initialDelay);
javaMethodEDT('setDismissDelay',tm,dismissDelay);
</pre>
<p>Note that I have made extensive use of the undocumented built-in <b><i>javaMethodEDT</i></b> function to execute Java methods that affect Swing components on the Swing Event Dispatch Thread (EDT). I plan to write about EDT following my next post on event callback chaining.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks">Additional uicontrol tooltip hacks</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/spicing-up-matlab-uicontrol-tooltips" rel="bookmark" title="Spicing up Matlab uicontrol tooltips">Spicing up Matlab uicontrol tooltips </a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/listbox-selection-hacks" rel="bookmark" title="Listbox selection hacks">Listbox selection hacks </a> <small>Matlab listbox selection can be customized in a variety of undocumented ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol" rel="bookmark" title="Setting line position in an edit-box uicontrol">Setting line position in an edit-box uicontrol </a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/inactive-control-tooltips-event-chaining" rel="bookmark" title="Inactive Control Tooltips &amp; Event Chaining">Inactive Control Tooltips &amp; Event Chaining </a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
	</channel>
</rss>
