<?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>Icons &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/icons/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Sun, 09 Feb 2020 19:54:55 +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>Matlab toolstrip – part 5 (icons)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-5-icons</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 06 Jan 2019 17:00:37 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8188</guid>

					<description><![CDATA[<p>Icons can be specified in various ways for toolstrip controls and the app window itself. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons">Matlab toolstrip – part 5 (icons)</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/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</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/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In a <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" target="_blank">previous post</a> I showed how we can create custom Matlab app toolstrips. Toolstrips can be a bit complex to develop so I’m trying to proceed slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">the Toolstrip miniseries</a> before reading this post. Today&#8217;s post describes how we can set various icons, based on the toolstrip created in the previous posts:<br />
<center><figure style="width: 440px" class="wp-caption aligncenter"><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_basic_controls.png" alt="Toolstrip example (basic controls)" title="Toolstrip example (basic controls)" width="100%" style="max-width:553px; margin:0" /><figcaption class="wp-caption-text">Toolstrip example (basic controls)</figcaption></figure></center><br />
<span id="more-8188"></span></p>
<h3 id="components">Component icons</h3>
<p>Many toolstrip controls (such as buttons, but not checkboxes for example) have a settable <b>Icon</b> property. The standard practice is to use a 16&#215;16 icon for a component within a multi-component toolstrip column (i.e., when 2 or 3 components are displayed on top of each other), and a 24&#215;24 icon for a component that spans the entire column height (i.e., when the column contains only a single component).<br />
We can use one of the following methods to specify the icon. Note that you need to <code>import matlab.ui.internal.toolstrip.*</code> if you wish to use the <code>Icon</code> class without the preceding package name.</p>
<ul>
<li>The <b>Icon</b> property value is typically empty (<code>[]</code>) by default, meaning that no icon is displayed.<br />
&nbsp;
</li>
<li>We can use one of ~150 standard icons using the format <code>Icon.&lt;icon-name&gt;</code>. For example: <code>icon = Icon.REFRESH_24</code>. These icons typically come in 2 sizes: 16&#215;16 pixels (e.g. Icon.REFRESH_16) that we can use with the small-size components (which are displayed when the column has 2-3 controls), and 24&#215;24 pixels (e.g. REFRESH_24) that we can use with the large-size components (which are displayed when the column contains only a single control). You can see the list of the standard icons by running
<pre lang="matlab">matlab.ui.internal.toolstrip.Icon.showStandardIcons</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_standard_icons.png" alt="Standard toolstrip control Icons" title="Standard toolstrip control Icons" width="50%" style="max-width:658px;" /></center></li>
<li>We can use the <code>Icon</code> constructor by specifying the full filepath for any PNG or JPG image file. Note that other file type (such as GIF) are not supported by this method. For example:
<pre lang="matlab">icon = Icon(fullfile(matlabroot,'toolbox','matlab','icons','tool_colorbar.png')); % PNG/JPG image file (not GIF!)</pre>
<p>In fact, the ~150 standard icons above use this mechanism under the hood: <code>Icon.REFRESH_24</code> is basically a public static method of the <code>Icon</code> class, which simply calls <code>Icon('REFRESH_24','Refresh_24')</code>  (note the undocumented use of a 2-input <code>Icon</code> constructor). This method in turn uses the <i>Refresh_24.png</i> file in Matlab&#8217;s standard toolstrip resources folder: <i>%matlabroot%/toolbox/shared/controllib/general/resources/toolstrip_icons/Refresh_24.png</i>.</p>
<li>We can also use the <code>Icon</code> constructor by specifying a PNG or JPG file contained within a JAR file, using the standard <code>jar:file:...jar!/</code> notation. There are numerous icons included in Matlab&#8217;s JAR files &#8211; simply open these files in WinZip or WinRar and browse. In addition, you can include images included in any external JAR file. For example:
<pre lang="matlab">icon = Icon(['jar:file:/' matlabroot '/java/jar/mlwidgets.jar!/com/mathworks/mlwidgets/actionbrowser/resources/uparrow.png']);</pre>
</li>
<li>We can also use the <code>Icon</code> constructor by specifying a Java <code>javax.swing.ImageIcon</code> object. Fortunately we can create such objects from a variety of image formats (including GIFs). For example:
<pre lang="matlab">
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','boardicon.gif');
jIcon = javax.swing.ImageIcon(iconFilename);  % Java ImageIcon from file (inc. GIF)
icon = Icon(jIcon);
</pre>
<p>If we need to resize the Java image (for example, from 16&#215;16 to 24&#215;24 or vise versa), we can use the following method:</p>
<pre lang="matlab">
% Resize icon to 24x24 pixels
jIcon = javax.swing.ImageIcon(iconFilename);  % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(24,24,jIcon.getImage.SCALE_SMOOTH))  % resize to 24x24
icon = Icon(jIcon);
</pre>
</li>
<li>We can apparently also use a CSS class-name to load images. This is only relevant for the JavaScript-based uifigures, not legacy Java-based figures that I discussed so far. Perhaps I will explore this in some later post that will discuss toolstrip integration in uifigures.</li>
</ul>
<h3 id="app">App window icon</h3>
<p>The app window&#8217;s icon can also be set. By default, the window uses the standard Matlab membrane icon (<i>%matlabroot%/toolbox/matlab/icons/matlabicon.gif</i>). This can be modified using the <code>hToolGroup.setIcon</code> method, which currently [R2018b] expects a Java <code>ImageIcon</code> object as input. For example:</p>
<pre lang="matlab">
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','reficon.gif');
jIcon = javax.swing.ImageIcon(iconFilename);
hToolGroup.setIcon(jIcon)
</pre>
<p>This icon should be set before the toolgroup window is shown (<code>hToolGroup.open</code>).<br />
<center><figure style="width: 400px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_app_icon.gif" alt="Custom app window icon" title="Custom app window icon" width="400" height="230" /><figcaption class="wp-caption-text">Custom app window icon</figcaption></figure></center><br />
An odd caveat here is that the icon size needs to be 16&#215;16 &#8211; setting a larger icon results in the icon being ignored and the default Matlab membrane icon used. For example, if we try to set &#8216;boardicon.gif&#8217; (16&#215;17) instead of &#8216;reficon.gif&#8217; (16&#215;16) we&#8217;d get the default icon instead. If our icon is too large, we can resize it to 16&#215;16, as shown above:</p>
<pre lang="matlab">
% Resize icon to 16x16 pixels
jIcon = javax.swing.ImageIcon(iconFilename);  % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(16,16,jIcon.getImage.SCALE_SMOOTH))  % resize to 16x16
hToolGroup.setIcon(jIcon)
</pre>
<p>It&#8217;s natural to expect that <code>hToolGroup</code>, which is a pure-Matlab MCOS wrapper class, would have an <b>Icon</b> property that accepts <code>Icon</code> objects, just like for controls as described above. For some reason, this is not the case. It&#8217;s very easy to fix it though &#8211; after all, the <code>Icon</code> class is little more than an MCOS wrapper class for the underlying Java <code>ImageIcon</code> (not exactly, but close enough). Adapting <code>ToolGroup</code>&#8216;s code to accept an <code>Icon</code> is quite easy, and I hope that MathWorks will indeed implement this in a near-term future release. I also hope that MathWorks will remove the 16&#215;16 limitation, or automatically resize icons to 16&#215;16, or at the very least issue a console warning when a larger icon is specified by the user. Until then, we can use the <code>setIcon(jImageIcon)</code> method and take care to send it the 16&#215;16 <code>ImageIcon</code> object that it expects.</p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss complex components, including button-group, drop-down, listbox, split-button, slider, popup form, gallery etc.<br />
Following that, my plan is to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order. Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons">Matlab toolstrip – part 5 (icons)</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/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</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/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Images in Matlab uicontrols &#038; labels</title>
		<link>https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=images-in-matlab-uicontrols-and-labels</link>
					<comments>https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 17 Oct 2012 18:00:57 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3177</guid>

					<description><![CDATA[<p>Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels">Images in Matlab uicontrols &amp; labels</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/icon-images-in-matlab-uicontrols" rel="bookmark" title="Icon images &amp; text in Matlab uicontrols">Icon images &amp; text in Matlab uicontrols </a> <small>HTML can be used to add image icons to Matlab listbox and popup (drop-down) controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparency-in-uicontrols" rel="bookmark" title="Transparency in uicontrols">Transparency in uicontrols </a> <small>Matlab uicontrols' CData property can be customized to provide background transparency....</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/panel-level-uicontrols" rel="bookmark" title="Panel-level uicontrols">Panel-level uicontrols </a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A couple of weeks ago, a reader <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/#comment-110444">here</a> asked how to integrate images in Matlab labels. I see quite a few queries about this, so I wanted to take today&#8217;s opportunity to explain how this can be done, and how to avoid common pitfalls.<br />
In fact, there are two main methods of displaying images in Matlab GUI &#8211; the documented method, and the undocumented one:</p>
<h3 id="documented">The documented method</h3>
<p>Some Matlab uicontrols (buttons, radio and checkboxes) have the <a target="_blank" rel="nofollow" href="http://www.mathworks.co.uk/help/matlab/ref/uicontrol_props.html#bqxoijz"><b>CData</b></a> property that can be used to load and display an image. For example:</p>
<pre lang='matlab'>
imgData = imread('myImage.jpg');   % or: imread(URL)
hButton = uicontrol('CData',imgData, ...);
</pre>
<p>While label uicontrols (<i><b>uicontrol</b>(&#8216;style&#8217;,&#8217;text&#8217;, &#8230;)</i>) also have the <b>CData</b> property, it has no effect on these controls. Instead, we can create an invisible axes that displays the image using the <i><b>image</b></i> function.</p>
<h3 id="undocumented">The undocumented method</h3>
<h4 id="web">web-based images</h4>
<p>I&#8217;ve already written extensively about Matlab&#8217;s <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">built-in support for HTML</a> in many of its controls. The supported HTML subset includes the &lt;img&gt; tag, and can therefore display images. For example:</p>
<pre lang='matlab' escaped="true">
htmlStr = '<html><b>Logo</b>: <img decoding="async" src="https://undocumentedmatlab.com/images/logo_68x60.png"/></html>';
hButton = uicontrol('Position',[10,10,120,70], 'String',htmlStr, 'Background','white');
</pre>
<p><center><figure style="width: 150px" class="wp-caption aligncenter"><img decoding="async" alt="uicontrol with HTML image" src="https://undocumentedmatlab.com/images/button_image.png" title="uicontrol with HTML image" width="120" height="70" /><figcaption class="wp-caption-text">uicontrol with HTML image</figcaption></figure></center></p>
<h4 id="local">local images</h4>
<p>Note that the image src (filename) needs to be formatted in a URL-compliant format such as <code>'http://www.website.com/folder/image.gif'</code> or <code>'file:/c:/folder/subfolder/img.png'</code>. If we try to use a non-URL-format filename, the image will not be rendered, only a placeholder box:</p>
<pre lang='matlab' escaped="true">
uicontrol('Position',..., 'String','<html><img decoding="async" src="img.gif"/></html>');  %bad
uicontrol('Style','list', ... 'String',{...,'<html><img decoding="async" src="img.gif"/></html>'});  %bad
</pre>
<p><center><figure style="width: 170px" class="wp-caption aligncenter"><img decoding="async" alt="Ill-specified HTML images in Matlab uicontrols" src="https://undocumentedmatlab.com/images/html_noimg1.png" title="Ill-specified HTML images in Matlab uicontrols" width="70" height="70"/> <img loading="lazy" decoding="async" alt="Ill-specified HTML images in Matlab uicontrols" src="https://undocumentedmatlab.com/images/html_noimg2.png" title="Ill-specified HTML images in Matlab uicontrols" width="70" height="70"/><figcaption class="wp-caption-text">Ill-specified HTML images in Matlab uicontrols</figcaption></figure></center><br />
Instead, we need to use correct URI syntax when specifying local images, which means using the <code>'file:/'</code> protocol prefix and the <code>'/'</code> folder separator: <span id="more-3177"></span></p>
<pre lang='matlab' escaped="true">
>> iconsFolder = fullfile(matlabroot,'/toolbox/matlab/icons/');
>> iconUrl = strrep(['file:/' iconsFolder 'matlabicon.gif'],'\','/');
>> str = ['<html><img decoding="async" src="' iconUrl '"/></html>']
str =
<html><img decoding="async" src="file:/C:/Program Files/MATLAB/ ..... /icons/matlabicon.gif"/></html>
>> uicontrol('Position',..., 'String',str);
>> uicontrol('Style','list', ... str});
</pre>
<p><center><figure style="width: 170px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Correctly-specified HTML images in Matlab uicontrols" src="https://undocumentedmatlab.com/images/html_img.png" title="Correctly-specified HTML images in Matlab uicontrols" width="70" height="70"/> <img loading="lazy" decoding="async" alt="Correctly-specified HTML images in Matlab uicontrols" src="https://undocumentedmatlab.com/images/html_img2.png" title="Correctly-specified HTML images in Matlab uicontrols" width="70" height="70"/><figcaption class="wp-caption-text">Correctly-specified HTML images in Matlab uicontrols</figcaption></figure></center><br />
A similar pitfall exists when trying to integrate images in GUI control tooltips. I already discussed this issue <a target="_blank" href="/articles/spicing-up-matlab-uicontrol-tooltips/">here</a>.<br />
You can use HTML to resize the images, using the &lt;img&gt; tag&#8217;s <i>width, height</i> attributes. However, beware that enlarging an image might introduce pixelization effects. I discussed image resizing <a target="_blank" href="/articles/customizing-menu-items-part-3/#custom">here</a> &#8211; that article was in the context of menu-item icons, but the discussion of image resizing also applies in this case.</p>
<h4 id="labels">images in text labels</h4>
<p>As for text labels, since text-style uicontrols do not unfortunately support HTML, we can use the equivalent Java <code>JLabel</code>s, as I have explained <a target="_blank" href="/articles/customizing-matlab-labels/">here</a>. Here too, we need to use the <code>'file:/'</code> protocol prefix and the <code>'/'</code> folder separator if we want to use local files rather than internet files (http://&#8230;).</p>
<h4 id="java">Java customizations</h4>
<p>Using a uicontrol&#8217;s <a target="_blank" href="/articles/findjobj-find-underlying-java-object/">underlying Java component</a>, we can customize the displayed image icon even further. For example, we can specify a different icon for selected/unselected/disabled/hovered/pressed/normal button states, as I have explained <a target="_blank" href="/articles/button-customization/">here</a>. In fact, we can even specify a unique icon that will be used for the mouse cursor when it hovers on the control:<br />
<center><figure style="width: 170px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/cursor3.png" alt="Custom cursor" title="Custom cursor" width="84" height="44" /> <img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/button_cursor_hand.png" alt="Custom cursor" title="Custom cursor" width="70" height="44" /><figcaption class="wp-caption-text">Custom uicontrol cursors</figcaption></figure></center></p>
<h4 id="R2019b">R2019b and newer</h4>
<p>Iin R2019b and later, unless you specify the image size nothing will be shown. A solution is presented in <a href="https://www.mathworks.com/matlabcentral/answers/497260-figure-uitable-does-not-display-html-image-in-2019b#answer_411812" rel="nofollow" target="_blank">this answer</a></p>
<pre lang="Matlab">str = ['<html><img loading="lazy" decoding="async" src="file:/' iconUrl '" height="16" width="16"></html>'];</pre>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels">Images in Matlab uicontrols &amp; labels</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/icon-images-in-matlab-uicontrols" rel="bookmark" title="Icon images &amp; text in Matlab uicontrols">Icon images &amp; text in Matlab uicontrols </a> <small>HTML can be used to add image icons to Matlab listbox and popup (drop-down) controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparency-in-uicontrols" rel="bookmark" title="Transparency in uicontrols">Transparency in uicontrols </a> <small>Matlab uicontrols' CData property can be customized to provide background transparency....</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/panel-level-uicontrols" rel="bookmark" title="Panel-level uicontrols">Panel-level uicontrols </a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels/feed</wfw:commentRss>
			<slash:comments>29</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing menu items part 3</title>
		<link>https://undocumentedmatlab.com/articles/customizing-menu-items-part-3?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-menu-items-part-3</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-menu-items-part-3#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 09 May 2012 18:00:05 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Menubar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2909</guid>

					<description><![CDATA[<p>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3">Customizing menu items part 3</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-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/customizing-menu-items-part-1" rel="bookmark" title="Customizing menu items part 1">Customizing menu items part 1 </a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In the past weeks I&#8217;ve shown how Matlab menus can be customized in a variety of undocumented manners, using HTML, <a target="_blank" href="/articles/customizing-menu-items-part-1/">pure Matlab</a>, and <a target="_blank" href="/articles/customizing-menu-items-part-2/">Java</a>. Today I conclude this mini-series with an article that explains how to use the underlying Java object to customize menu item icons. Menu customizations are explored in depth in section 4.6 of my <a target="_blank" href="/matlab-java-book/">book</a>.</p>
<h3 id="underlying">A reminder: accessing the underlying Java object</h3>
<p>Matlab menus (<i><b>uimenu</b></i>) are basically simple wrappers for the much more powerful and flexible Java Swing <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html"><code>JMenu</code> and <code>JMenuItem</code></a> on which they are based.  Many important functionalities that are available in Java menus are missing from the Matlab <i><b>uimenu</b></i>s.<br />
Getting the Java reference for the figure window&#8217;s main menu is very easy:</p>
<pre lang='matlab'>
jFrame = get(handle(hFig),'JavaFrame');
try
    % R2008a and later
    jMenuBar = jFrame.fHG1Client.getMenuBar;
catch
    % R2007b and earlier
    jMenuBar = jFrame.fFigureClient.getMenuBar;
end
</pre>
<p>There are many customizations that can only be done using the Java handle: setting icons, several dozen callback types, tooltips, background color, font, text alignment, and so on. etc. Interested readers may wish to <i><b>get</b></i>/<i><b>set</b></i>/<i><b>inspect</b></i>/<i><b>methodsview</b></i>/<i><b><a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object">uiinspect</a></b></i> the <code>jSave</code> reference handle and/or to read the documentation for <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JMenuItem.html"><code>JMenuItem</code></a>. Today&#8217;s article will focus on icon customizations.</p>
<h3 id="simple">Setting simple menu item icons</h3>
<p>Many of Matlab&#8217;s icons reside in either the <i>[matlabroot &#8216;/toolbox/matlab/icons/&#8217;]</i> folder or the <i>[matlabroot &#8216;/java/jar/mwt.jar&#8217;]</i> file (a JAR file is simply a zip file that includes Java classes and resources such as icon images). Let us create icons from the latter, to keep a consistent look-and-feel with the rest of Matlab (we could just as easily use our own external icon files):</p>
<pre lang='matlab'>
% External icon file example
jSave.setIcon(javax.swing.ImageIcon('C:\Yair\save.gif'));
% JAR resource example
jarFile = fullfile(matlabroot,'/java/jar/mwt.jar');
iconsFolder = '/com/mathworks/mwt/resources/';
iconURI = ['jar:file:/' jarFile '!' iconsFolder 'save.gif'];
iconURI = java.net.URL(iconURI);  % not necessary for external files
jSave.setIcon(javax.swing.ImageIcon(iconURI));
</pre>
<p>Note that setting a menu item&#8217;s icon automatically re-aligns all other items in the menu, including those that do not have an icon (an internal bug that was introduced in R2010a causes a misalignment, as shown below):<br />
<center><figure style="width: 230px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Menu item with a custom Icon (R2009b)" src="https://undocumentedmatlab.com/images/uimenu3.png" title="Menu item with a custom Icon (R2009b)" width="230" height="120"/><figcaption class="wp-caption-text">Menu item with a custom <b>Icon</b> (R2009b)</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 227px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="...and the same in R2010a onward" src="https://undocumentedmatlab.com/images/uimenu3b.png" title="...and the same in R2010a onward" width="227" height="120"/><figcaption class="wp-caption-text">...and the same in R2010a onward</figcaption></figure></center></p>
<h3 id="checkmark">Checkmark icon</h3>
<p>The empty space on the left of the menu is reserved for the check mark. Each Matlab menu item is check-able, since it is an object that extends the <code>com.mathworks.mwswing.MJCheckBoxMenuItem</code> class. I have not found a way to eliminate this empty space, which is really unnecessary in the File-menu case (it is only actually necessary in the View and Tools menus). Note that if an icon is set for the item, both the icon and the checkmark will be displayed, side by side.<br />
The check mark is controlled by the <b>State</b> property of the Java object (which accepts logical true/false values), or the <b>Checked</b> property of the Matlab handle (which accepts the regular &#8216;on&#8217;/&#8217;off&#8217; string values):</p>
<pre lang='matlab'>
% Set the check mark at the Matlab level
set(findall(hFig,'tag','figMenuFileSave'), 'Checked','on');
% Equivalent - set the checkmark at the Java level
jSave.setState(true);
</pre>
<p><center><figure style="width: 185px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="State = true, Icon = [ ]" src="https://undocumentedmatlab.com/images/uimenu_save_checked1.png" title="State = true, Icon = [ ]" width="185" height="138"/><figcaption class="wp-caption-text"><b>State</b> = true, <b>Icon</b> = &#091; &#093;</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 205px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="State = true, Icon = custom" src="https://undocumentedmatlab.com/images/uimenu_save_checked2.png" title="State = true, Icon = custom" width="205" height="137"/><figcaption class="wp-caption-text"><b>State</b> = true, <b>Icon</b> = custom</figcaption></figure></center></p>
<h3 id="custom">Customizing menu icons</h3>
<p>Icons can be customized: modify the gap between the icon and the label with the <b>IconTextGap</b> property (default = 4 [pixels]); place icons to the right of the label by setting <b>HorizontalTextPosition</b> to <code>jSave.LEFT</code> (=2), or centered using <code>jSave.CENTER</code> (=0). Note that the above-mentioned misalignment bug does not appear in these cases:<br />
<center><figure style="width: 222px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="jSave.setHorizontalTextPosition(jSave.LEFT)" src="https://undocumentedmatlab.com/images/uimenu4.png" title="jSave.setHorizontalTextPosition(jSave.LEFT)" width="222" height="121"/><figcaption class="wp-caption-text"><code>jSave.setHorizontalTextPosition<br />(jSave.LEFT)</code></figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 223px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="jSave.setHorizontalTextPosition(jSave.CENTER)" src="https://undocumentedmatlab.com/images/uimenu5.png" title="jSave.setHorizontalTextPosition(jSave.CENTER)" width="223" height="120"/><figcaption class="wp-caption-text"><code>jSave.setHorizontalTextPosition<br />(jSave.CENTER)</code></figcaption></figure></center><br />
Note how the label text can be seen through (or on top of) the icon when it is centered. This feature can be used to create stunning menu effects as shown below. Note how the width and height of the menu item automatically increased to accommodate my new 77&#215;31 icon size (icons are normally sized 16&#215;16 pixels):<br />
<center><figure style="width: 285px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Overlaid icon (HorizontalTextPosition = CENTER)" src="https://undocumentedmatlab.com/images/uimenu6.png" title="Overlaid icon (HorizontalTextPosition = CENTER)" width="285" height="136"/><figcaption class="wp-caption-text">Overlaid icon (<b>HorizontalTextPosition</b> = CENTER)</figcaption></figure></center><br />
To resize an icon programmatically before setting it in a Java component, we can use the following example:</p>
<pre lang='matlab'>
myIcon = fullfile(matlabroot,'/toolbox/matlab/icons/matlabicon.gif');
imageToolkit = java.awt.Toolkit.getDefaultToolkit;
iconImage = imageToolkit.createImage(myIcon);
iconImage = iconImage.getScaledInstance(32,32,iconImage.SCALE_SMOOTH);
jSave.setIcon(javax.swing.ImageIcon(iconImage));
</pre>
<p>Remember when rescaling images, particularly small ones with few pixels, that it is always better to shrink than to enlarge images: enlarging a small icon image might introduce a significant pixelization effect:<br />
<center><figure style="width: 90px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="16x16 icon image resized to 32x32" src="https://undocumentedmatlab.com/images/button_icon2.png" title="16x16 icon image resized to 32x32" width="90" height="90"/><figcaption class="wp-caption-text">16x16 icon image resized to 32x32</figcaption></figure></center><br />
Separate icons can be specified for a different appearance during mouse hover (<b>RolloverIcon</b>; requires <b>RolloverEnabled</b>=1), item click/press (<b>PressedIcon</b>), item selection (<b>SelectedIcon</b>, <b>RolloverSelectedIcon</b>, <b>DisabledSelectedIcon</b>), and disabled menu item (<b>DisabledIcon</b>). All these properties are empty ([]) by default, which applies a predefined default variation (image color filter) to the main item&#8217;s Icon. For example, let us modify <b>DisabledIcon</b>:</p>
<pre lang='matlab'>
myIcon = 'C:\Yair\Undocumented Matlab\Images\save_disabled.gif';
jSaveAs.setDisabledIcon(javax.swing.ImageIcon(myIcon));
jSaveAs.setEnabled(false);
</pre>
<p><center><figure style="width: 120px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Enabled, main Icon" src="https://undocumentedmatlab.com/images/uimenu7a.png" title="Enabled, main Icon" width="120" height="135"/><figcaption class="wp-caption-text">Enabled, main <b>Icon</b></figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 160px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Disabled, default Icon variation" src="https://undocumentedmatlab.com/images/uimenu7b.png" title="Disabled, default Icon variation" width="120" height="135"/><figcaption class="wp-caption-text">Disabled, default <b>Icon</b> variation</figcaption></figure> &nbsp;&nbsp;&nbsp; <figure style="width: 170px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Disabled, custom DisabledIcon" src="https://undocumentedmatlab.com/images/uimenu7c.png" title="Disabled, custom DisabledIcon" width="120" height="135"/><figcaption class="wp-caption-text">Disabled, custom <b>DisabledIcon</b></figcaption></figure></center><br />
Note the automatic graying of disabled menu items, including their icon. This effect can also be achieved programmatically using the static methods in <code>com.mathworks.mwswing.IconUtils</code>: <i>changeIconColor(), createBadgedIcon(), createGhostedIcon()</i>, and <i>createSelectedIcon()</i>. When we use a non-default custom <b>DisabledIcon</b>, it is used instead of the gray icon variant.<br />
This concludes my mini-series of customizing menus in Matlab. If you have used any nifty customization that I have not mentioned, please post a comment about it <a href="/articles/customizing-menu-items-part-3/#respond">below</a>.<br />
<span class="alignleft" id="ken_mike"><img loading="lazy" decoding="async" alt="Ken &#038; Mike" src="https://undocumentedmatlab.com/images/ken_and_mike.png" title="Ken &#038; Mike" width="98" height="83"/></span>In an unrelated note, I would like to extend good wishes to Mike Katz, who has left the MathWorks mobile development team to join Kinvey a few days ago. Mike has been with MathWorks since 2005 and has been responsible for maintaining the official <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/">MATLAB Desktop blog</a>, together with <a target="_blank" href="/articles/gui-integrated-browser-control/">Ken Orr</a>. I&#8217;m not sure yet which direction the Desktop blog will take, and by whom, but in any case it won&#8217;t be the same. You&#8217;re both missed, Mike &#038; Ken!</p>
<p />&nbsp;</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3">Customizing menu items part 3</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-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/customizing-menu-items-part-1" rel="bookmark" title="Customizing menu items part 1">Customizing menu items part 1 </a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-menu-items-part-3/feed</wfw:commentRss>
			<slash:comments>39</slash:comments>
		
		
			</item>
		<item>
		<title>Uitab colors, icons and images</title>
		<link>https://undocumentedmatlab.com/articles/uitab-colors-icons-images?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uitab-colors-icons-images</link>
					<comments>https://undocumentedmatlab.com/articles/uitab-colors-icons-images#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 10 Nov 2010 18:00:01 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[uitools]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1955</guid>

					<description><![CDATA[<p>Matlab's semi-documented tab panels can be customized using some undocumented hacks</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uitab-colors-icons-images">Uitab colors, icons and images</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/uitab-customizations" rel="bookmark" title="Uitab customizations">Uitab customizations </a> <small>This article shows several customizations that can be done to Matlab's undocumented tab-panels functionality...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives" rel="bookmark" title="Tab panels &#8211; uitab and relatives">Tab panels &#8211; uitab and relatives </a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-system-tray-icons" rel="bookmark" title="Setting system tray icons">Setting system tray icons </a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels" rel="bookmark" title="Images in Matlab uicontrols &amp; labels">Images in Matlab uicontrols &amp; labels </a> <small>Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few months ago I published a post about <a target="_blank" href="/articles/tab-panels-uitab-and-relatives/">Matlab&#8217;s semi-documented tab-panel functionality</a>, where I promised a follow-up article on tab customizations. A reader of this blog <a target="_blank" href="/articles/customizing-uicontrol-border/#comment-21714">asked a related question</a> earlier today, so I decided it&#8217;s about time I fulfilled this promise.<br />
As with most Matlab controls, the underlying Java component enables far greater customization than possible using plain Matlab. Today I will show three specific customizations. We start with a basic tab group, and get the underlying Java component:</p>
<pre lang="matlab">
% Prevent an annoying warning msg
warning off MATLAB:uitabgroup:OldVersion
% Prepare a tab-group consisting of two tabs
hTabGroup = uitabgroup; drawnow;
tab1 = uitab(hTabGroup, 'title','Panel 1');
a = axes('parent', tab1); surf(peaks);
tab2 = uitab(hTabGroup, 'title','Panel 2');
uicontrol(tab2, 'String','Close', 'Callback','close(gcbf)');
% Get the underlying Java reference (use hidden property)
jTabGroup = getappdata(handle(hTabGroup),'JTabbedPane');
</pre>
<h3 id="Colors">Foreground &#038; background tab colors</h3>
<p>We can set the tab font color using <i>setForeground()</i> and <i>setForegroundAt()</i>, or via HTML. Note that <i>setForegroundAt()</i> overrides anything set by <i>setForeground()</i>. Also remember that Java uses 0-based indexing so tab #1 is actually the second tab:</p>
<pre lang="matlab">
% Equivalent manners to set a red tab foreground:
jTabGroup.setForegroundAt(1,java.awt.Color(1.0,0,0)); % tab #1
jTabGroup.setTitleAt(1,'<html><font color="red"><i>Panel 2');
jTabGroup.setForeground(java.awt.Color.red);
</pre>
<p>Unfortunately, the corresponding <i>setBackgroundAt(tabIndex,color)</i> method has no visible effect, and the Matlab-extended tabs keep their white/gray backgrounds. A similar attempt to modify the tab&#8217;s <b>BackgroundColor</b> property fails, since Matlab made this property unmodifiable (=&#8217;none&#8217;).  A simple solution is to use a <a target="_blank" rel="nofollow" href="http://www.w3schools.com/css/css_background.asp">CSS background</a>:</p>
<pre lang="matlab">
% Equivalent manners to set a yellow tab background:
jTabGroup.setTitleAt(0,'<html><div style="background:#ffff00;">Panel 1');
jTabGroup.setTitleAt(0,'<html><div style="background:yellow;">Panel 1');
</pre>
<p><center><figure style="width: 340px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="uitabgroup with non-default forground and background tab colors and fonts" src="https://undocumentedmatlab.com/images/uitabgroup5_bgcolor.png" title="uitabgroup with non-default forground and background tab colors and fonts" width="340" height="265" /><figcaption class="wp-caption-text"><i><b>uitabgroup</b></i> with non-default forground and background tab colors and fonts</figcaption></figure></center><br />
We can set the foreground text color using the <a target="_blank" rel="nofollow" href="http://www.w3schools.com/css/css_colors.asp">CSS <i>color</i> directive</a>. Similarly, we can also set a background gradient image for the tabs, using the CSS <i>background-image</i> directive. Which leads us to our next customization:</p>
<h3 id="Icons">Icon images</h3>
<p>Icons and sub-components can be added to the tabs. Unfortunately, for some reason that I do not fully understand, jTabGroup.<i>setIconAt()</i> has no apparent effect. The solution is to set our own custom control as the requested tab, and add our icon (or other customizations) to it. Here is a simple example:</p>
<pre lang="matlab">
% Add an icon to tab #1 (=second tab)
icon = javax.swing.ImageIcon('C:\Yair\save.gif');
jLabel = javax.swing.JLabel('Tab #2');
jLabel.setIcon(icon);
jTabGroup.setTabComponentAt(1,jLabel);	% Tab #1 = second tab
% Note: icon is automatically grayed when label is disabled
jLabel.setEnabled(false);
jTabGroup.setEnabledAt(1,false);  % disable only tab #1
</pre>
<p><center><figure style="width: 292px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="tab with a custom icon (enabled)" src="https://undocumentedmatlab.com/images/uitabgroup6_icon.png" title="tab with a custom icon (enabled)" width="292" height="159" /><br /><img loading="lazy" decoding="async" alt="tab with a custom icon (enabled)" src="https://undocumentedmatlab.com/images/uitabgroup6_icon2.png" title="tab with a custom icon (disabled)" width="292" height="159" /><figcaption class="wp-caption-text">tab with a custom icon (enabled & disabled)</figcaption></figure></center></p>
<h3 id="Buttons">Close buttons</h3>
<p>Now let&#8217;s try a more complex example, of adding a close (&#8216;x&#8217;) button to one of the tabs. Generalizing this code snippet is left as an exercise to the reader:</p>
<pre lang="matlab">
% First let's load the close icon
jarFile = fullfile(matlabroot,'/java/jar/mwt.jar');
iconsFolder = '/com/mathworks/mwt/resources/';
iconURI = ['jar:file:/' jarFile '!' iconsFolder 'closebox.gif'];
icon = javax.swing.ImageIcon(java.net.URL(iconURI));
% Now let's prepare the close button: icon, size and callback
jCloseButton = handle(javax.swing.JButton,'CallbackProperties');
jCloseButton.setIcon(icon);
jCloseButton.setPreferredSize(java.awt.Dimension(15,15));
jCloseButton.setMaximumSize(java.awt.Dimension(15,15));
jCloseButton.setSize(java.awt.Dimension(15,15));
set(jCloseButton, 'ActionPerformedCallback',@(h,e)delete(tab2));
% Now let's prepare a tab panel with our label and close button
jPanel = javax.swing.JPanel;	% default layout = FlowLayout
set(jPanel.getLayout, 'Hgap',0, 'Vgap',0);  % default gap = 5px
jLabel = javax.swing.JLabel('Tab #2');
jPanel.add(jLabel);
jPanel.add(jCloseButton);
% Now attach this tab panel as the tab-group's 2nd component
jTabGroup.setTabComponentAt(1,jPanel);	% Tab #1 = second tab
</pre>
<p><center><figure style="width: 359px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="tab with an attached close button" src="https://undocumentedmatlab.com/images/uitabgroup8_close_button.png" title="tab with an attached close button" width="359" height="185" /><figcaption class="wp-caption-text">tab with an attached close button</figcaption></figure></center><br />
Next week&#8217;s article will conclude the series on Matlab&#8217;s <i><b>uitab</b></i>. Any particular customization you are interested in? Please do post a <a target="_blank" href="/articles/uitab-colors-icons-images/#respond">comment</a>.<br />
<b><u>Addendum Oct 3 2014</u></b>: the <i><b>uitab</b></i> and <i><b>uitabgroup</b></i> functions have finally become fully supported and documented in Matlab version 8.4 (R2014b). However, the Java-based customizations shown in this article are still unsupported and undocumented, although they remain practically unchanged from what I&#8217;ve described in this article, four years earlier.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uitab-colors-icons-images">Uitab colors, icons and images</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/uitab-customizations" rel="bookmark" title="Uitab customizations">Uitab customizations </a> <small>This article shows several customizations that can be done to Matlab's undocumented tab-panels functionality...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives" rel="bookmark" title="Tab panels &#8211; uitab and relatives">Tab panels &#8211; uitab and relatives </a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-system-tray-icons" rel="bookmark" title="Setting system tray icons">Setting system tray icons </a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels" rel="bookmark" title="Images in Matlab uicontrols &amp; labels">Images in Matlab uicontrols &amp; labels </a> <small>Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/uitab-colors-icons-images/feed</wfw:commentRss>
			<slash:comments>43</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing uitree nodes &#8211; part 2</title>
		<link>https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-uitree-nodes-2</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 01 Sep 2010 08:00:57 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[uitools]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1850</guid>

					<description><![CDATA[<p>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2">Customizing uitree nodes &#8211; part 2</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-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree" rel="bookmark" title="Customizing uitree">Customizing uitree </a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitree" rel="bookmark" title="uitree">uitree </a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In my previous posts I have shown how Matlab&#8217;s semi-documented <i><b>uitree</b></i> and <i><b>uitreenode</b></i> functions can be used to display hierarchical (tree) control in Matlab GUI. Today I conclude this mini-series by answering a reader&#8217;s <a target="_blank" href="/articles/customizing-uitree/#comment-15781">request</a> to show how checkboxes, radio buttons and other similar controls can be attached to tree nodes.<br />
There are actually several ways this can be done:</p>
<h3 id="Matlab-icon">Matlab icon control</h3>
<p>The simplest is to create two icons (checked/unchecked) and switch the node&#8217;s icon whenever it is selected (use mtree&#8217;s <strong>NodeSelectedCallback</strong> or jtree&#8217;s <strong>MouseClickedCallback</strong> callbacks) &#8211; a sample implementation was posted by <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/164189#416971">Gwendolyn Fischer</a> a couple of years ago, based on even earlier posts by <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/104957#269485">John Anderson</a>, <a target="_blank" rel="nofollow" href="http://web.archive.org/web/20071015115312/xtargets.com/cms/Tutorials/Matlab-Programming/Handle-Graphics-Tree-Control-With-Nested-Functions.html">Brad Phelan</a> and me. Here it is, with minor fixes:</p>
<pre lang="matlab">
function uitree_demo
% function based on treeExperiment6 by John Anderson
% see https://www.mathworks.com/matlabcentral/newsreader/view_thread/104957#269485
%
% The mousePressedCallback part is inspired by Yair Altman
%
% derived from Brad Phelan's tree demo
% create a tree model based on UITreeNodes and insert into uitree.
% add and remove nodes from the treeModel and update the display
import javax.swing.*
import javax.swing.tree.*;
% figure window
f = figure('Units', 'normalized');
b1 = uicontrol( 'string','add Node', ...
   'units' , 'normalized', ...
   'position', [0 0.5 0.5 0.5], ...
   'callback', @b1_cb);
b2 = uicontrol( 'string','remove Node', ...
   'units' , 'normalized', ...
   'position', [0.5 0.5 0.5 0.5], ...
   'callback', @b2_cb);
%[I,map] = imread([matlab_work_path, '/checkedIcon.gif']);
[I,map] = checkedIcon;
javaImage_checked = im2java(I,map);
%[I,map] = imread([matlab_work_path, '/uncheckedIcon.gif']);
[I,map] = uncheckedIcon;
javaImage_unchecked = im2java(I,map);
% javaImage_checked/unchecked are assumed to have the same width
iconWidth = javaImage_unchecked.getWidth;
% create top node
rootNode = uitreenode('v0','root', 'File List', [], 0);
% [matlab_work_path, '/fileListIcon.gif'],0);
% create two children with checkboxes
cNode = uitreenode('v0','unselected', 'File A', [], 0);
% as icon is embedded here we set the icon via java, otherwise one could
% use the uitreenode syntax uitreenode(value, string, icon, isLeaf) with
% icon being a qualified pathname to an image to be used.
cNode.setIcon(javaImage_unchecked);
rootNode.add(cNode);
cNode = uitreenode('v0','unselected', 'File B', [], 0);
cNode.setIcon(javaImage_unchecked);
rootNode.add(cNode);
% set treeModel
treeModel = DefaultTreeModel( rootNode );
% create the tree
tree = uitree('v0');
tree.setModel( treeModel );
% we often rely on the underlying java tree
jtree = handle(tree.getTree,'CallbackProperties');
% some layout
drawnow;
set(tree, 'Units', 'normalized', 'position', [0 0 1 0.5]);
set(tree, 'NodeSelectedCallback', @selected_cb );
% make root the initially selected node
tree.setSelectedNode( rootNode );
% MousePressedCallback is not supported by the uitree, but by jtree
set(jtree, 'MousePressedCallback', @mousePressedCallback);
  % Set the mouse-press callback
  function mousePressedCallback(hTree, eventData) %,additionalVar)
  % if eventData.isMetaDown % right-click is like a Meta-button
  % if eventData.getClickCount==2 % how to detect double clicks
  % Get the clicked node
    clickX = eventData.getX;
    clickY = eventData.getY;
    treePath = jtree.getPathForLocation(clickX, clickY);
    % check if a node was clicked
    if ~isempty(treePath)
      % check if the checkbox was clicked
      if clickX <= (jtree.getPathBounds(treePath).x+iconWidth)
        node = treePath.getLastPathComponent;
        nodeValue = node.getValue;
        % as the value field is the selected/unselected flag,
        % we can also use it to only act on nodes with these values
        switch nodeValue
          case 'selected'
            node.setValue('unselected');
            node.setIcon(javaImage_unchecked);
            jtree.treeDidChange();
          case 'unselected'
            node.setValue('selected');
            node.setIcon(javaImage_checked);
            jtree.treeDidChange();
        end
      end
    end
  end % function mousePressedCallback
  function selected_cb( tree, ev )
    nodes = tree.getSelectedNodes;
    node = nodes(1);
    path = node2path(node);
  end
  function path = node2path(node)
    path = node.getPath;
    for i=1:length(path);
      p{i} = char(path(i).getName);
    end
    if length(p) > 1
      path = fullfile(p{:});
    else
      path = p{1};
    end
  end
  % add node
  function b1_cb( h, env )
    nodes = tree.getSelectedNodes;
    node = nodes(1);
    parent = node;
    childNode = uitreenode('v0','dummy', 'Child Node', [], 0);
    treeModel.insertNodeInto(childNode,parent,parent.getChildCount());
    % expand to show added child
    tree.setSelectedNode( childNode );
    % insure additional nodes are added to parent
    tree.setSelectedNode( parent );
  end
  % remove node
  function b2_cb( h, env )
    nodes = tree.getSelectedNodes;
    node = nodes(1);
    if ~node.isRoot
      nP = node.getPreviousSibling;
      nN = node.getNextSibling;
      if ~isempty( nN )
        tree.setSelectedNode( nN );
      elseif ~isempty( nP )
        tree.setSelectedNode( nP );
      else
        tree.setSelectedNode( node.getParent );
      end
      treeModel.removeNodeFromParent( node );
    end
  end
end % of main function treeExperiment6
  function [I,map] = checkedIcon()
    I = uint8(...
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0;
         2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,1;
         2,2,2,2,2,2,2,2,2,2,2,2,0,2,3,1;
         2,2,1,1,1,1,1,1,1,1,1,0,2,2,3,1;
         2,2,1,1,1,1,1,1,1,1,0,1,2,2,3,1;
         2,2,1,1,1,1,1,1,1,0,1,1,2,2,3,1;
         2,2,1,1,1,1,1,1,0,0,1,1,2,2,3,1;
         2,2,1,0,0,1,1,0,0,1,1,1,2,2,3,1;
         2,2,1,1,0,0,0,0,1,1,1,1,2,2,3,1;
         2,2,1,1,0,0,0,0,1,1,1,1,2,2,3,1;
         2,2,1,1,1,0,0,1,1,1,1,1,2,2,3,1;
         2,2,1,1,1,0,1,1,1,1,1,1,2,2,3,1;
         2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
         2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
         2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
         1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1]);
     map = [0.023529,0.4902,0;
            1,1,1;
            0,0,0;
            0.50196,0.50196,0.50196;
            0.50196,0.50196,0.50196;
            0,0,0;
            0,0,0;
            0,0,0];
  end
  function [I,map] = uncheckedIcon()
     I = uint8(...
       [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1]);
     map = ...
      [0.023529,0.4902,0;
       1,1,1;
       0,0,0;
       0.50196,0.50196,0.50196;
       0.50196,0.50196,0.50196;
       0,0,0;
       0,0,0;
       0,0,0];
  end
</pre>
<p><center><figure style="width: 359px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="uitree with custom checkbox icons" src="https://undocumentedmatlab.com/images/uitree8.png" title="uitree with custom checkbox icons" width="359" height="283" /><figcaption class="wp-caption-text">uitree with custom checkbox icons</figcaption></figure></center></p>
<h3 id="Custom-Java">Custom Java classes</h3>
<p>An alternative is to create a custom tree Java class and/or a custom <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/tree/DefaultTreeCellRenderer.html">TreeCellRenderer</a>/<a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/tree/DefaultTreeCellEditor.html">TreeCellEditor</a>. Specifically, to change the icons of each node you have to implement your own java component which derives from <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/tree/DefaultMutableTreeNode.html">DefaultMutableTreeNode</a>.<br />
Some online resources to get you started:</p>
<ul>
<li>Sun&#8217;s official <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">JTree customization tutorial</a></li>
<li>Santhosh Kumar&#8217;s <a target="_blank" rel="nofollow" href="http://www.jroller.com/santhosh/entry/coloring_icons">custom tree icons</a>, <a target="_blank" rel="nofollow" href="http://www.jroller.com/santhosh/entry/jtree_with_checkboxes">tree-node checkboxes</a>, and <a target="_blank" rel="nofollow" href="http://www.jroller.com/santhosh/entry/show_checkboxes_only_on_specific">selective tree-node checkboxes</a></li>
<li>Java2s&#8217;s multiple <a target="_blank" rel="nofollow" href="http://www.java2s.com/Code/Java/Swing-JFC/Tree.htm">JTree customization snippets</a></li>
</ul>
<h3 id="Built-in-classes">Built-in classes</h3>
<p>Another option is to use Matlab&#8217;s built-in classes, either com.mathworks.mwswing.checkboxtree.CheckBoxTree or com.jidesoft.swing.CheckBoxTree:</p>
<pre lang="matlab">
import com.mathworks.mwswing.checkboxtree.*
jRoot = DefaultCheckBoxNode('Root');
l1a = DefaultCheckBoxNode('Letters'); jRoot.add(l1a);
l1b = DefaultCheckBoxNode('Numbers'); jRoot.add(l1b);
l2a = DefaultCheckBoxNode('A'); l1a.add(l2a);
l2b = DefaultCheckBoxNode('b'); l1a.add(l2b);
l2c = DefaultCheckBoxNode('<html><b>&alpha;'); l1a.add(l2c);
l2d = DefaultCheckBoxNode('<html><i>&beta;'); l1a.add(l2d);
l2e = DefaultCheckBoxNode('3.1415'); l1b.add(l2e);
% Present the standard MJTree:
jTree = com.mathworks.mwswing.MJTree(jRoot);
jScrollPane = com.mathworks.mwswing.MJScrollPane(jTree);
[jComp,hc] = javacomponent(jScrollPane,[10,10,120,110],gcf);
% Now present the CheckBoxTree:
jCheckBoxTree = CheckBoxTree(jTree.getModel);
jScrollPane = com.mathworks.mwswing.MJScrollPane(jCheckBoxTree);
[jComp,hc] = javacomponent(jScrollPane,[150,10,120,110],gcf);
</pre>
<p><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 />
Note: Matlab&#8217;s CheckBoxTree does not have a separate data model. Instead, it relies on the base MJTree&#8217;s model, which is a <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/tree/DefaultTreeModel.html">DefaultTreeModel</a> by default. JIDE&#8217;s CheckBoxTree does have its own model.<br />
This concludes my <i><b>uitree</b></i> mini-series. If you have any special customizations, please post a comment <a href="/articles/customizing-uitree-nodes-2/#respond">below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2">Customizing uitree nodes &#8211; part 2</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-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree" rel="bookmark" title="Customizing uitree">Customizing uitree </a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitree" rel="bookmark" title="uitree">uitree </a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2/feed</wfw:commentRss>
			<slash:comments>61</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing uitree nodes &#8211; part 1</title>
		<link>https://undocumentedmatlab.com/articles/customizing-uitree-nodes?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-uitree-nodes</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-uitree-nodes#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Aug 2010 18:00:20 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented feature]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[uitools]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1835</guid>

					<description><![CDATA[<p>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes">Customizing uitree nodes &#8211; part 1</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-uitree-nodes-2" rel="bookmark" title="Customizing uitree nodes &#8211; part 2">Customizing uitree nodes &#8211; part 2 </a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree" rel="bookmark" title="Customizing uitree">Customizing uitree </a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitree" rel="bookmark" title="uitree">uitree </a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In my previous posts, I introduced the semi-documented <a target="_blank" href="/articles/uitree/"><i><b>uitree</b></i> function</a> that enables displaying data in a hierarchical (tree) control in Matlab GUI, and showed how it can be <a target="_blank" href="/articles/customizing-uitree/">customized</a>. Today, I will continue by describing how specific <i><b>uitree</b></i> nodes can be customized.<br />
To start the discussion, let&#8217;s re-create last week&#8217;s simple <i><b>uitree</b></i>:</p>
<pre lang="matlab">
% Fruits
fruits = uitreenode('v0', 'Fruits', 'Fruits', [], false);
fruits.add(uitreenode('v0', 'Apple',  'Apple',  [], true));
fruits.add(uitreenode('v0', 'Pear',   'Pear',   [], true));
fruits.add(uitreenode('v0', 'Banana', 'Banana', [], true));
fruits.add(uitreenode('v0', 'Orange', 'Orange', [], true));
% Vegetables
veggies = uitreenode('v0', 'Veggies', 'Vegetables', [], false);
veggies.add(uitreenode('v0', 'Potato', 'Potato', [], true));
veggies.add(uitreenode('v0', 'Tomato', 'Tomato', [], true));
veggies.add(uitreenode('v0', 'Carrot', 'Carrot', [], true));
% Root node
root = uitreenode('v0', 'Food', 'Food', [], false);
root.add(veggies);
root.add(fruits);
% Tree
figure('pos',[300,300,150,150]);
mtree = uitree('v0', 'Root', root);
</pre>
<p><center><figure style="width: 350px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="User-created tree" src="https://undocumentedmatlab.com/images/uitree7a.png" title="User-created tree" width="158" height="232" /> &nbsp;&nbsp; <img loading="lazy" decoding="async" alt="User-created tree" src="https://undocumentedmatlab.com/images/uitree7b.png" title="User-created tree" width="158" height="232" /><figcaption class="wp-caption-text">User-created tree</figcaption></figure></center></p>
<h3 id="labels">Labels</h3>
<p>Node labels (descriptions) can be set using their <strong>Name</strong> property (the second <i><b>uitreenode</b></i> data argument). Note that the horizontal space allotted for displaying the node name will not change until the node is collapsed or expanded. So, if the new name requires more than the existing space, it will be displayed as something like &#8220;abc…&#8221;, until the node is expanded or collapsed.<br />
Node names share the same <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">HTML support feature</a> as all Java Swing labels. Therefore, we can specify font size/face/color, bold, italic, underline, super-/sub-script etc.:</p>
<pre lang="matlab">
txt1 = '<html><b><u><i>abra</i></u>';
txt2 = '<font color="red"><sup>kadabra</html>';
node.setName([txt1,txt2]);
</pre>
<p><center><figure style="width: 150px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="HTML-enriched tree nodes" src="https://undocumentedmatlab.com/images/uitree2.jpg" title="HTML-enriched tree nodes" width="116" height="50" /><figcaption class="wp-caption-text">HTML-enriched tree nodes</figcaption></figure></center></p>
<h3 id="icons">Icons</h3>
<p>Tree-node icons can be specified during node creation, as the third data argument to <i><b>uitreenode</b></i>, which accepts an icon-path (a string):</p>
<pre lang="matlab">
iconPath = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif');
node = uitreenode('v0',value,name,iconPath,isLeaf);
</pre>
<p>Tree node icons can also be created or modified programmatically in run-time, using Matlab&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/im2java.html">im2java</a> function. Icons can also be loaded from existing files as follows (real-life programs should check and possibly update jImage&#8217;s size to 16 pixels, before setting the node icon, otherwise the icon might get badly cropped; also note the tree-refreshing action):</p>
<pre lang="matlab">
jImage = java.awt.Toolkit.getDefaultToolkit.createImage(iconPath);
veggies.setIcon(jImage);
veggies.setIcon(im2java(imread(iconPath)));  % an alternative
% refresh the veggies node (and all its children)
mtree.reloadNode(veggies);
</pre>
<p><center><figure style="width: 158px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Setting node icon" src="https://undocumentedmatlab.com/images/uitree7c.png" title="Setting node icon" width="158" height="232" /><figcaption class="wp-caption-text">Setting node icon</figcaption></figure></center></p>
<h3 id="behavior">Behavior</h3>
<p>Nodes can be modified from leaf (non-expandable) to parent behavior (=expandable) by setting their <strong>LeafNode</strong> property (a related property is <strong>AllowsChildren</strong>):</p>
<pre lang="matlab">
set(node,'LeafNode',false);  % =expandable
node.setLeafNode(0);  % an alternative
</pre>
<p>One of the questions I was <a target="_blank" href="/articles/uicontrol-callbacks/#comment-388">asked</a> was how to &#8220;disable&#8221; a specific tree node. One way would be to modify the tree&#8217;s <strong>ExpandFcn</strong> callback. Another way is to use a combination of HTML rendering and the node&#8217;s <strong>AllowsChildren</strong> property:</p>
<pre lang="matlab">
label = char(veggies.getName);
veggies.setName(['<html><font color="gray">' label]);
veggies.setAllowsChildren(false);
t.reloadNode(veggies);
</pre>
<p><center><figure style="width: 158px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Disabled node" src="https://undocumentedmatlab.com/images/uitree7d.png" title="Disabled node" width="158" height="232" /><figcaption class="wp-caption-text">Disabled node</figcaption></figure></center><br />
Another possible behavioral customization is <a target="_blank" href="/articles/adding-context-menu-to-uitree/">adding a context-menu to a uitree</a>. We can set node-specific tooltips using similar means.<br />
Answering a reader&#8217;s <a target="_blank" href="/articles/customizing-uitree/#comment-15781">request</a> from last week, tree nodes icons can be used to present checkboxes, radio buttons and other similar node-specific controls. This can actually be done in several ways, that will be explored in next week&#8217;s article.<br />
There are numerous other possible customizations &#8211; if readers are interested, perhaps I will describe some of them in future articles. If you have any special request, please post a comment <a href="/articles/customizing-uitree-nodes/#respond">below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes">Customizing uitree nodes &#8211; part 1</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-uitree-nodes-2" rel="bookmark" title="Customizing uitree nodes &#8211; part 2">Customizing uitree nodes &#8211; part 2 </a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree" rel="bookmark" title="Customizing uitree">Customizing uitree </a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitree" rel="bookmark" title="uitree">uitree </a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-uitree-nodes/feed</wfw:commentRss>
			<slash:comments>33</slash:comments>
		
		
			</item>
		<item>
		<title>Figure toolbar components</title>
		<link>https://undocumentedmatlab.com/articles/figure-toolbar-components?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=figure-toolbar-components</link>
					<comments>https://undocumentedmatlab.com/articles/figure-toolbar-components#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 27 Aug 2009 16:31:36 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=541</guid>

					<description><![CDATA[<p>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/figure-toolbar-components">Figure toolbar components</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/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar" rel="bookmark" title="Adding a search box to figure toolbar">Adding a search box to figure toolbar </a> <small>An interactive search-box can easily be added to a Matlab figure toolbar for enhanced user experience. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Toolbars are by now a staple of modern GUI design. An unobtrusive list of small icons enables easy access to multiple application actions without requiring large space for textual descriptions. Unfortunately, the built-in documented support for the Matlab toolbars is limited to adding icon buttons via the <b><i>uipushtool</i></b> and <b><i>uitoggletool</i></b> functions, and new toolbars containing them via the <b><i>uitoolbar</i></b> function. In this post I will introduce several additional customizations that rely on undocumented features.<br />
This article will only describe figure toolbars. However, much of the discussion is also relevant to the desktop (Command Window) toolbars and interested users can adapt it accordingly.</p>
<h3 id="Matlab">Accessing toolbar buttons &#8211; undo/redo</h3>
<p>Let&#8217;s start by adding undo/redo buttons to the existing figure toolbar. I am unclear why such an elementary feature was not included in the default figure toolbar, but this is a fact that can easily be remedied. In <a target="_blank" href="/articles/uiundo-matlab-undocumented-undo-redo-manager/">another post</a> I describe <b><i>uiundo</i></b>, Matlab&#8217;s semi-documented support for undo/redo functionality, but for the present let&#8217;s assume we already have this functionality set up.<br />
First, let&#8217;s prepare our icons, which are basically a green-filled triangle icon and its mirror image:</p>
<pre lang="matlab">
% Load the Redo icon
icon = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif');
[cdata,map] = imread(icon);
% Convert white pixels into a transparent background
map(find(map(:,1)+map(:,2)+map(:,3)==3)) = NaN;
% Convert into 3D RGB-space
cdataRedo = ind2rgb(cdata,map);
cdataUndo = cdataRedo(:,[16:-1:1],:);
</pre>
<p>Now let&#8217;s add these icons to the default figure toolbar:</p>
<pre lang="matlab">
% Add the icon (and its mirror image = undo) to the latest toolbar
hUndo = uipushtool('cdata',cdataUndo, 'tooltip','undo', 'ClickedCallback','uiundo(gcbf,''execUndo'')');
hRedo = uipushtool('cdata',cdataRedo, 'tooltip','redo', 'ClickedCallback','uiundo(gcbf,''execRedo'')');
</pre>
<p><center><figure style="width: 296px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo buttons" src="https://undocumentedmatlab.com/images/uiundo4.png" title="Undo/redo buttons" width="296" height="199" /><figcaption class="wp-caption-text">Undo/redo buttons</figcaption></figure></center><br />
In the preceding screenshot, since no figure toolbar was previously shown, <b><i>uipushtool</i></b> added the undo and redo buttons to a new toolbar. Had the figure toolbar been visible, then the buttons would have been added to its right end. Since undo/redo buttons are normally requested near the left end of toolbars, we need to rearrange the toolbar buttons:</p>
<pre lang="matlab">
hToolbar = findall(hFig,'tag','FigureToolBar');
%hToolbar = get(hUndo,'Parent');  % an alternative
hButtons = findall(hToolbar);
set(hToolbar,'children',hButtons([4:end-4,2,3,end-3:end]));
set(hUndo,'Separator','on');
</pre>
<p><center><figure style="width: 302px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo buttons in their expected positions" src="https://undocumentedmatlab.com/images/uiundo5.png" title="Undo/redo buttons in their expected positions" width="302" height="199" /><figcaption class="wp-caption-text">Undo/redo buttons in their expected positions</figcaption></figure></center><br />
We would normally preserve hUndo and hRedo, and modify their <i>Tooltip</i> and <i>Visible</i>/<i>Enable</i> properties in run-time, based on the availability and name of the latest undo/redo actions:</p>
<pre lang="matlab">
% Retrieve redo/undo object
undoObj = getappdata(hFig,'uitools_FigureToolManager');
if isempty(undoObj)
   undoObj = uitools.FigureToolManager(hFig);
   setappdata(hFig,'uitools_FigureToolManager',undoObj);
end
% Customize the toolbar buttons
latestUndoAction = undoObj.CommandManager.peekundo;
if isempty(latestUndoAction)
   set(hUndo, 'Tooltip','', 'Enable','off');
else
   tooltipStr = ['undo' latestUndoAction.Name];
   set(hUndo, 'Tooltip',tooltipStr, 'Enable','on');
end
</pre>
<p>We can easily adapt the method I have just shown to modify/update existing toolbar icons: hiding/disabling them etc. based on the application needs at run-time.</p>
<h3 id="Java">Adding non-button toolbar components &#8211; undo dropdown</h3>
<p>A more advanced customization is required if we wish to present the undo/redo actions in a drop-down (combo-box). Unfortunately, since Matlab only enables adding <b><i>uipushtool</i></b>s and <b><i>uitoggletool</i></b>s to toolbars, we need to use a Java component. The drawback of using such a component is that it is inaccessible via the toolbar&#8217;s Children property (implementation of the drop-down callback function is left as an exercise to the reader):</p>
<pre lang="matlab">
% Add undo dropdown list to the toolbar
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
if ~isempty(jToolbar)
   undoActions = get(undoObj.CommandManager.UndoStack,'Name');
   jCombo = javax.swing.JComboBox(undoActions(end:-1:1));
   set(jCombo, 'ActionPerformedCallback', @myUndoCallbackFcn);
   jToolbar(1).add(jCombo,5); %5th position, after printer icon
   jToolbar(1).repaint;
   jToolbar(1).revalidate;
end
% Drop-down (combo-box) callback function
function myUndoCallbackFcn(hCombo,hEvent)
   itemIndex = get(hCombo,'SelectedIndex');  % 0=topmost item
   itemName  = get(hCombo,'SelectedItem');
   % user processing needs to be placed here
end
</pre>
<p><center><figure style="width: 382px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo dropdown list" src="https://undocumentedmatlab.com/images/uiundo6.png" title="Undo dropdown list" width="382" height="221" /><figcaption class="wp-caption-text">Undo dropdown list</figcaption></figure></center><br />
Note that the javax.swing.JComboBox constructor accepts a cell-array of strings (undoActions in the snippet above). A user-defined dropdownlist might be constructed as follows (also see a <a rel="nofollow" target="_blank" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/160674#405575">related CSSM thread</a>):</p>
<pre lang="matlab">
...
dropdownStrings = {'here', 'there', 'everywhere'};
jCombo = javax.swing.JComboBox(dropdownStrings);
set(jCombo, 'ActionPerformedCallback', @myUndoCallbackFcn);
jToolbar(1).addSeparator;
jToolbar(1).add(jCombo);  % at end, following a separator mark
jToolbar(1).repaint;
jToolbar(1).revalidate;
...
</pre>
<p>A similar approach can be used to add checkboxes, radio-buttons and other non-button controls.<br />
In next week&#8217;s post I will describe how the toolbar can be customized using undocumented functionality to achieve a non-default background, a floating toolbar (&#8220;palette&#8221;) effect and other interesting customizations. If you have any specific toolbar-related request, I&#8217;ll be happy to hear in the comments section below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/figure-toolbar-components">Figure toolbar components</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/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar" rel="bookmark" title="Adding a search box to figure toolbar">Adding a search box to figure toolbar </a> <small>An interactive search-box can easily be added to a Matlab figure toolbar for enhanced user experience. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/figure-toolbar-components/feed</wfw:commentRss>
			<slash:comments>50</slash:comments>
		
		
			</item>
		<item>
		<title>Setting system tray popup messages</title>
		<link>https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-system-tray-popup-messages</link>
					<comments>https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 31 Mar 2009 23:09:25 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=130</guid>

					<description><![CDATA[<p>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages">Setting system tray popup messages</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-system-tray-icons" rel="bookmark" title="Setting system tray icons">Setting system tray icons </a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-help-popup-contents" rel="bookmark" title="Customizing help popup contents">Customizing help popup contents </a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</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/sending-email-text-messages-from-matlab" rel="bookmark" title="Sending email/text messages from Matlab">Sending email/text messages from Matlab </a> <small>Sending emails and SMS (text) messages from Matlab is easy, once you know a few quirks....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Continuing my previous post about <a href="/articles/setting-system-tray-icons/" target="_blank">setting system-tray icons</a>, I will now show how to set informational popup messages next to these icons.<br />
Asynchronous informational messages can be presented next to the sys-tray icon, in a fashion similar to what we came to expect from modern programs. This could be used to indicate some unexpected event that was detected, or the end of a complex calculation phase. The message title, text and severity icon are all customizable.<br />
Unfortunately, the Java method used to display messages, java.awt.TrayIcon.displayMessage(), expects an object of type java.awt.TrayIcon.MessageType, which is an enumeration within the TrayIcon class. However, Matlab&#8217;s dot-notation does not recognize what should have been the following correct notation, so we need to resort to Java reflection:</p>
<blockquote>
<pre><span style="color: #000000;">&gt;&gt; trayIcon.displayMessage(</span><span style="color: #800080;">'title'</span>,<span style="color: #800080;">'info msg'</span><span style="color: #000000;">,TrayIcon.MessageType.INFO);</span>
<span style="color: #ff0000;">??? No appropriate method or public field MessageType for class java.awt.TrayIcon</span>
<span style="color: #000000;">&gt;&gt; trayIconClasses = trayIcon.getClass.getClasses;
&gt;&gt; trayIconClasses(1)</span>
ans =
class java.awt.TrayIcon$MessageType	&lt;= hurray!!!
<span style="color: #000000;">&gt;&gt; MessageTypes = trayIconClasses(1).getEnumConstants</span>
MessageTypes =
java.awt.TrayIcon$MessageType[]:
    [java.awt.TrayIcon$MessageType]	&lt;= 1: ERROR
    [java.awt.TrayIcon$MessageType]	&lt;= 2: WARNING
    [java.awt.TrayIcon$MessageType]	&lt;= 3: INFO
    [java.awt.TrayIcon$MessageType]	&lt;= 4: NONE
<span style="color: #000000;">&gt;&gt; trayIcon.displayMessage(</span><span style="color: #800080;">'title'</span>,<span style="color: #800080;">'info msg'</span><span style="color: #000000;">,MessageTypes(3));</span>
</pre>
</blockquote>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/systray1.png" alt="systray INFO message" title="systray INFO message" /></center><br />
and another example, now with a WARNING icon:<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/systray2.png" alt="systray WARNING message" title="systray WARNING message" /></center><br />
If the title string is left empty, then neither title nor the severity icon will be displayed. The message can still be manually dismissed by clicking within its boundaries:<br />
<center><br />
<img decoding="async" src="https://undocumentedmatlab.com/images/systray4.png" alt="systray messages without a title (hence also without a severity icon)" title="systray messages without a title (hence also without a severity icon)" /><br />
<img decoding="async" src="https://undocumentedmatlab.com/images/systray7.png" alt="systray messages without a title (hence also without a severity icon)" title="systray messages without a title (hence also without a severity icon)" /><br />
</center><br />
Informational popup messages are automatically aligned and positioned by the system. Messages are automatically dismissed by the system after some time, if not dismissed by the user first. The exact time is determined by system and user activity and other such external factors. Informational messages replace one another, if the previous message has still not been cleared by the user.<br />
I have created a utility function called SYSTRAY, which is a convenience function that facilitates the setup and update of system tray icons and messages. SYSTRAY (with source code) can be <a rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/23299" target="_blank">downloaded from the File Exchange</a>.<br />
I would be happy to hear if and how you&#8217;re using the new system-tray functionality in your application &#8211; let me know below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages">Setting system tray popup messages</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-system-tray-icons" rel="bookmark" title="Setting system tray icons">Setting system tray icons </a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-help-popup-contents" rel="bookmark" title="Customizing help popup contents">Customizing help popup contents </a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</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/sending-email-text-messages-from-matlab" rel="bookmark" title="Sending email/text messages from Matlab">Sending email/text messages from Matlab </a> <small>Sending emails and SMS (text) messages from Matlab is easy, once you know a few quirks....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages/feed</wfw:commentRss>
			<slash:comments>21</slash:comments>
		
		
			</item>
		<item>
		<title>Setting system tray icons</title>
		<link>https://undocumentedmatlab.com/articles/setting-system-tray-icons?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-system-tray-icons</link>
					<comments>https://undocumentedmatlab.com/articles/setting-system-tray-icons#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 24 Mar 2009 20:47:54 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=67</guid>

					<description><![CDATA[<p>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-system-tray-icons">Setting system tray icons</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-system-tray-popup-messages" rel="bookmark" title="Setting system tray popup messages">Setting system tray popup messages </a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<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/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>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Java 1.6, included in Matlab releases since Matlab 7.5 (R2007b), enables programmatic access to system tray icons on such systems that support this functionality (Windows, Linux and possibly others).  If the SystemTray object indicates that it <em>isSupported()</em>, then a TrayIcon can be added, along with an associated tooltip and popup menu:</p>
<blockquote>
<pre>sysTray = java.awt.SystemTray.getSystemTray;
if (sysTray.isSupported)
   myIcon = fullfile(matlabroot,<span style="color: #800080;">'/toolbox/matlab/icons/matlabicon.gif'</span>);
   iconImage = java.awt.Toolkit.getDefaultToolkit.createImage(myIcon);
   trayIcon = java.awt.TrayIcon(iconImage, <span style="color: #800080;">'initial tooltip'</span>);
   trayIcon.setToolTip(<span style="color: #800080;">'click this icon for applicative context menu'</span>);
end</pre>
</blockquote>
<p><center><figure id="attachment_68" aria-describedby="caption-attachment-68" style="width: 209px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" class="size-full wp-image-68" title="sample system tray icon" src="https://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/systray5.png" alt="sample system tray icon" width="209" height="40" /><figcaption id="caption-attachment-68" class="wp-caption-text">sample system tray icon</figcaption></figure></center><br />
The icon image can be made to automatically resize to the system-tray dimensions, using the trayIcon.<em>setImageAutoSize(true)</em> method (by default the icon image will maintain its original size, getting cropped or appearing small as the case may be).<br />
Of course, after initial setup, all the sys-tray icon&#8217;s properties (icon image, popup, tooltip etc.) can be modified with convenient set methods (<em>setImage(), setPopupMenu(), setTooltip()</em>) or via Matlab&#8217;s <em>set()</em> function.<br />
Icon popup menus are very similar in concept to Matlab uicontextmenus. Unfortunately, they need to be programmed separately since Java does not accept uicontextmenu handles. This is actually quite easy, as the following code snippet shows:</p>
<blockquote>
<pre><span style="color: #008000;">% Prepare the context menu
</span>menuItem1 = java.awt.MenuItem(<span style="color: #800080;">'action #1'</span>);
menuItem2 = java.awt.MenuItem(<span style="color: #800080;">'action #2'</span>);
menuItem3 = java.awt.MenuItem(<span style="color: #800080;">'action #3'</span>);
<span style="color: #008000;">% Set the menu items' callbacks</span>
set(menuItem1,<span style="color: #800080;">'ActionPerformedCallback'</span>,@myFunc1);
set(menuItem2,<span style="color: #800080;">'ActionPerformedCallback'</span>,{@myfunc2,data1,data2});
set(menuItem3,<span style="color: #800080;">'ActionPerformedCallback'</span>,<span style="color: #800080;">'disp(''action #3...'')'</span>);
<span style="color: #008000;">% Disable one of the menu items</span>
menuItem2.setEnabled(0);        <span style="color: #008000;">% or: set(menuItem2,'Enabled','off');</span>
<span style="color: #008000;">% Add all menu items to the context menu (with internal </span><span style="color: #008000;">separator)
</span>jmenu = java.awt.PopupMenu;
jmenu.add(menuItem1);
jmenu.add(menuItem2);
jmenu.addSeparator;
jmenu.add(menuItem3);
<span style="color: #008000;">% Finally, attach the context menu to the icon</span>
trayIcon.setPopupMenu(jmenu);    <span style="color: #008000;">% or: set(trayIcon,'PopupMenu',jmenu);</span></pre>
</blockquote>
<p><center><figure id="attachment_69" aria-describedby="caption-attachment-69" style="width: 108px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" class="size-full wp-image-69" title="Tray icon context (right-click) menu" src="https://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/systray3.png" alt="Tray icon context (right-click) menu" width="108" height="98" /><figcaption id="caption-attachment-69" class="wp-caption-text">Tray icon context (right-click) menu</figcaption></figure></center><br />
Unfortunately, neither the icon tooltip nor its popup menu supports HTML. The reason is that SystemTray is actually not part of Swing at all. The system-tray functionality resides in the java.awt package, and does not inherit <a href="/articles/html-support-in-matlab-uicomponents/">javax.swing.JLabel&#8217;s (and Matlab uicontrols) support for HTML</a>.<br />
I have created a utility function called SYSTRAY, which is a convenience function that facilitates the setup and update of system tray icons. <a href="http://www.mathworks.com/matlabcentral/fileexchange/23299" target="_blank" rel="nofollow">SYSTRAY (with source code) can be downloaded from the File Exchange</a>.<br />
In <a href="/articles/setting-system-tray-popup-messages/" target="_blank">a separate post</a>, I shall detail how informational pop-up messages can be attached to system-tray icons. This requires a bit of Java-hacking, so is beyond the scope of a single blog post.<br />
Please note the new <a href="/todo/" target="_blank">TODO page</a>, which details my future posts. I would be happy to hear your requests for new topics, or telling me which topics you&#8217;d like to see earlier than others.<br />
<strong><u>Addendum (May 15, 2009)</u>: A kind reader today left a comment on <a target="_noblank" href="/articles/uisplitpane/comment-page-1/#comment-658">another post of this blog</a> with a solution for some reported Java exceptions when using systray in Matlab R2008b onward.</strong></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-system-tray-icons">Setting system tray icons</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-system-tray-popup-messages" rel="bookmark" title="Setting system tray popup messages">Setting system tray popup messages </a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li>
<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/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>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/setting-system-tray-icons/feed</wfw:commentRss>
			<slash:comments>15</slash:comments>
		
		
			</item>
	</channel>
</rss>
