<?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/category/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>Toolbar button labels</title>
		<link>https://undocumentedmatlab.com/articles/toolbar-button-labels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toolbar-button-labels</link>
					<comments>https://undocumentedmatlab.com/articles/toolbar-button-labels#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 17:34:17 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7270</guid>

					<description><![CDATA[<p>GUI toolbar button labels can easily be set and customized using underlying Java components. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/toolbar-button-labels">Toolbar button 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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</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/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>
<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>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I was recently asked by a client to add a few buttons labeled &#8220;1&#8221;-&#8220;4&#8221; to a GUI toolbar. I thought: How hard could that be? Simply <a href="/articles/figure-toolbar-components" target="_blank">get the toolbar&#8217;s handle from the figure</a>, then use the builtin <i><b>uipushtool</b></i> function to add a new button, specifying the label in the <b>String</b> property, right?<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/toolbar_labels.png" alt="Labeled toolbar buttons" title="Labeled toolbar buttons" width="80%" style="max-width:612px;" height="140" /></center><br />
Well, not so fast it seems:</p>
<div class="wp_syntax">
<div class="code">
<pre class="matlab" style="font-family:monospace;">hToolbar = findall<span style="color: #080;">(</span>hFig, <span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'FigureToolBar'</span><span style="color: #080;">)</span>;  <span style="color: #228B22;">% get the figure's toolbar handle</span>
uipushtool<span style="color: #080;">(</span>hToolbar, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">)</span>;               <span style="color: #228B22;">% add a pushbutton to the toolbar</span>
<span style="color: #FF0000;">Error using <u>uipushtool</u>
There is no String property on the PushTool class. </pre>
</div>
</div>
<p>Apparently, for some unknown reason, standard Matlab only enables us to set the icon (<b>CData</b>) of a toolbar control, but not a text label.<br />
Once again, Java to the rescue: <span id="more-7270"></span><br />
We first get the Java toolbar reference handle, then add the button in standard Matlab (using <i><b>uipushtool</b></i>, <i><b>uitoggletool</b></i> and their kin). We can now access the Java toolbar&#8217;s last component, relying on the fact that Matlab always adds new buttons at the end of the toolbar. Note that we need to use a short <i><b>drawnow</b></i> to ensure that the toolbar is fully re-rendered, otherwise we&#8217;d get an invalid Java handle. Finally, once we have this reference handle to the underlying Java button component, we can set and customize its label text and appearance (font face, border, size, alignment etc.):</p>
<pre lang="matlab">
hToolbar = findall(hFig, 'tag','FigureToolBar');     % get the figure's toolbar handle
jToolbar = hToolbar.JavaContainer.getComponentPeer;  % get the toolbar's Java handle
for buttonIdx = 1 : 4
    % First create the toolbar button using standard Matlab code
    label = num2str(buttonIdx);  % create a string label
    uipushtool(hToolbar, 'ClickedCallback',{@myCallback,analysisIdx}, 'TooltipString',['Run analysis #' label]);
    % Get the Java reference handle to the newly-created button
    drawnow; pause(0.01);  % allow the GUI time to re-render the toolbar
    jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
    % Set the button's label
    jButton.setText(label)
end
</pre>
<p>The standard Matlab toolbar button size (23&#215;23 pixels) is too small to display more than a few characters. To display a longer label, we need to widen the button:</p>
<pre lang="matlab">
% Make the button wider than the standard 23 pixels
newSize = java.awt.Dimension(50, jButton.getHeight);
jButton.setMaximumSize(newSize)
jButton.setPreferredSize(newSize)
jButton.setSize(newSize)
</pre>
<p>Using a text label does not prevent us from also displaying an icon: In addition to the text label, we can also display a standard icon (by setting the button&#8217;s <b>CData</b> property in standard Matlab). This icon will be displayed to the left of the text label. You can widen the button, as shown in the code snippet above, to make space for both the icon and the label. If you want to move the label to a different location relative to the icon, simply modify the Java component&#8217;s HorizontalTextPosition property:</p>
<pre lang="matlab">
jButton.setHorizontalTextPosition(jButton.RIGHT);   % label right of icon (=default)
jButton.setHorizontalTextPosition(jButton.CENTER);  % label on top of icon
jButton.setHorizontalTextPosition(jButton.LEFT);    % label left of icon
</pre>
<p>In summary, here&#8217;s the code snippet that generated the screenshot above:</p>
<pre lang="matlab">
% Get the Matlab & Java handles to the figure's toolbar
hToolbar = findall(hFig, 'tag','FigureToolBar');     % get the figure's toolbar handle
jToolbar = hToolbar.JavaContainer.getComponentPeer;  % get the toolbar's Java handle
% Button #1: label only, no icon, 23x23 pixels
h1 = uipushtool(hToolbar);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('1')
% Create the icon CData from an icon file
graphIcon = fullfile(matlabroot,'/toolbox/matlab/icons/plotpicker-plot.gif');
[graphImg,map] = imread(graphIcon);
map(map(:,1)+map(:,2)+map(:,3)==3) = NaN;  % Convert white pixels => transparent background
cdata = ind2rgb(graphImg,map);
% Button #2: label centered on top of icon, 23x23 pixels
h2 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('2')
jButton.setHorizontalTextPosition(jButton.CENTER)
% Button #3: label on right of icon, 50x23 pixels
h3 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('3...')
d = java.awt.Dimension(50, jButton.getHeight);
jButton.setMaximumSize(d); jButton.setPreferredSize(d); jButton.setSize(d)
% Button #4: label on left of icon, 70x23 pixels
h4 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('and 4:')
jButton.setHorizontalTextPosition(jButton.LEFT)
d = java.awt.Dimension(70, jButton.getHeight);
jButton.setMaximumSize(d); jButton.setPreferredSize(d); jButton.setSize(d)
</pre>
<p>Many additional toolbar customizations can be found <a href="/articles/tag/toolbar" target="_blank">here</a> and in my book &#8220;<a href="/books/matlab-java" target="_blank"><b>Undocumented Secrets of MATLAB-Java Programming</b></a>&#8220;. If you&#8217;d like me to design a professional-looking GUI for you, please <a href="/consulting" target="_blank">contact me</a>.<br />
Caveat emptor: all this only works with the regular Java-based GUI figures, not web-based (&#8220;App-Designer&#8221;) uifigures.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/toolbar-button-labels">Toolbar button 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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</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/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>
<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>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/toolbar-button-labels/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Icon images &#038; text in Matlab uicontrols</title>
		<link>https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=icon-images-in-matlab-uicontrols</link>
					<comments>https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 28 Sep 2016 10:28:04 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6687</guid>

					<description><![CDATA[<p>HTML can be used to add image icons to Matlab listbox and popup (drop-down) controls. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols">Icon images &amp; text in Matlab uicontrols</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/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>
<li><a href="https://undocumentedmatlab.com/articles/animated-busy-spinning-icon" rel="bookmark" title="Animated busy (spinning) icon">Animated busy (spinning) icon </a> <small>An animated spinning icon label can easily be embedded in Matlab GUI. ...</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/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>One of my consulting clients recently asked me if I knew any builtin Matlab GUI control that could display a list of <a href="http://www.mathworks.com/help/matlab/ref/colormap.html#inputarg_name" rel="nofollow" target="_blank">colormap names</a> alongside their respective image icons, in a listbox or popup menu (drop-down/combo-box):<br />
<center><figure style="width: 520px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" alt="Matlab listbox with icon images" src="https://undocumentedmatlab.com/images/html_listbox.png" title="Matlab listbox with icon images" width="270" height="200" /> &nbsp; <img decoding="async" alt="Matlab popup menu (dropdown/combobox) with icon images" src="https://undocumentedmatlab.com/images/html_popup.png" title="Matlab popup menu (dropdown/combobox) with icon images" width="224" height="264" /><figcaption class="wp-caption-text">Matlab listbox (left) & popup menu (right) with icon images</figcaption></figure></center><br />
My initial thought was that this should surely be possible, since <b>Colormap</b> is a documented figure property, that should therefore be listed inside the <a href="http://www.mathworks.com/help/matlab/ref/inspect.html" rel="nofollow" target="_blank">inspector window</a>, and should therefore have an associated builtin Java control for the dropdown (just like other inspector controls, which are part of the <code>com.mathworks.mlwidgets</code> package, or possibly as a standalone control in the <code>com.mathworks.mwswing</code> package). To my surprise it turns out that for some unknown reason MathWorks neglected to add the <b>Colormap</b> property (and associated Java controls) to the inspector. This property is fully documented and all, just like <b>Color</b> and other standard figure properties, but unlike them <b>Colormap</b> can only be modified programmatically, not via the inspector window. Matlab does provide the related <i><b>colormapeditor</b></i> function and associated dialog window, but I would have expected a simple drop-down of the standard builtin colormaps to be available in the inspector. Anyway, this turned out to be a dead-end.<br />
It turns out that we can relatively easily implement the requested listbox/combo-box using a bit of HTML magic, <a href="/articles/aligning-uicontrol-contents" target="_blank">as I explained</a> last week. The basic idea is for each of the listbox/combobox items to be an HTML string that contains both an &lt;img&gt; tag for the icon and the item label text. For example, such a string might contain something like this (parula is Matlab&#8217;s default colormap in HG2, starting in R2014b):</p>
<pre lang="xml"><html><img decoding="async" src="http://www.mathworks.com/help/matlab/ref/colormap_parula.png">parula</pre>
<p><center><figure style="width: 434px" class="wp-caption aligncenter"><img decoding="async" alt="parula colormap image" src="http://www.mathworks.com/help/matlab/ref/colormap_parula.png" title="parula colormap image" width="434" height="27" /><figcaption class="wp-caption-text">parula colormap image</figcaption></figure></center><br />
<span id="more-6687"></span>Of course, it would be a bit inefficient for each of the icons to be fetched from the internet. Luckily, the full set of Matlab documentation is typically installed on the local computer as part of the standard Matlab installation, beneath the <i><b>docroot</b></i> folder (e.g., <i>C:\Program Files\Matlab\R2016b\help</i>). In our specific case, the parula colormap image is located in:</p>
<pre lang="matlab">imageFilename = [docroot, '/matlab/ref/colormap_parula.png']</pre>
<p>Note that for a local image to be accepted by HTML, it needs to follow certain conventions. In our case, the HTML string for displaying the above image is:</p>
<pre lang="xml"><html><img decoding="async" src="file:///C:/Program%20Files/Matlab/R2016b/help/matlab/ref/colormap_parula.png">parula</pre>
<blockquote><p><u>Warning</u>: it&#8217;s easy when dealing with HTML images in Matlab to get the format confused, resulting in a red-x icon. I <a href="/articles/images-in-matlab-uicontrols-and-labels" target="_blank">discussed this issue</a> some 4 years ago, which is still relevant.</p></blockquote>
<p>How can we get the list of available builtin colormaps? The standard Matlab way of doing this would be something like this:</p>
<pre lang="matlab">
>> possibleColormaps = set(gcf,'Colormap')
possibleColormaps =
     {}
</pre>
<p>but as we can see, for some unknown reason (probably another MathWorks omission), Matlab does not list the names of its available builtin colormaps.<br />
Fortunately, all the builtin colormaps have image filenames that follow the same convention, which make it easy to get this list by simply listing the names of the relevant files, from which we can easily create the necessary HTML strings:</p>
<pre lang="matlab">
>> iconFiles = dir([docroot, '/matlab/ref/colormap_*.png']);
>> colormapNames = regexprep({iconFiles.name}, '.*_(.*).png', '$1')
colormapNames =
  Columns 1 through 9
    'autumn'    'bone'    'colorcube'    'cool'    'copper'    'flag'    'gray'    'hot'    'hsv'
  Columns 10 through 18
    'jet'    'lines'    'parula'    'pink'    'prism'    'spring'    'summer'    'white'    'winter'
>> htmlStrings = strcat('<html><img decoding="async" width=200 height=10 src="file:///C:/Program%20Files/Matlab/R2016a/help/matlab/ref/colormap_', colormapNames', '.png">', colormapNames')
str =
    '<html><img decoding="async" width=200 height=10 src="file:///C:/Program%20Files/Matlab/R2016a/help/matlab/ref/colormap_autumn.png">autumn'
    '<html><img decoding="async" width=200 height=10 src="file:///C:/Program%20Files/Matlab/R2016a/help/matlab/ref/colormap_bone.png">bone'
    '<html><img decoding="async" width=200 height=10 src="file:///C:/Program%20Files/Matlab/R2016a/help/matlab/ref/colormap_colorcube.png">colorcube'
    ...
>> hListbox = uicontrol(gcf, 'Style','listbox', 'Units','pixel', 'Pos',[10,10,270,200], 'String',htmlStrings);
>> hPopup   = uicontrol(gcf, 'Style','popup',   'Units','pixel', 'Pos',[10,500,270,20], 'String',htmlStrings);
</pre>
<p>&#8230;which results in the screenshots at the top of this post.<br />
Note how I scaled the images to 10px high (so that the labels would be shown and not cropped vertically) and 200px wide (so that it becomes narrower than the default 434px). There&#8217;s really no need in this case for the full 434&#215;27 image size &#8211; such flat images scale very nicely, even when their aspect ratio is not preserved. You can adjust the <code>height</code> and <code>width</code> values for a best fit with you GUI.<br />
Unfortunately, it seems that HTML strings are not supported in the new web-based uifigure controls. This is not really Matlab&#8217;s fault because the way to customize labels in HTML controls is via CSS: directly embedding HTML code in labels does not work (it&#8217;s a Java-Swing feature, not a browser feature). I really hope that either HTML or CSS processing will be enabled for web-based uicontrol in a future Matlab release, because until that time uifigure uicontrols will remain seriously deficient compared to standard figure uicontrols. Until then, if we must use uifigures and wish to customize our labels or listbox items, we can directly access the underlying web controls, <a href="/articles/customizing-uifigures-part-2" rel="nofollow" target="_blank">as Iliya explained here</a>.<br />
<span id="rant"></span><br />
A blog reader <a href="/articles/using-pure-java-gui-in-deployed-matlab-apps#comment-389045" target="_blank">recently complained</a> that I&#8217;m abusing Swing and basically making Matlab work in unnatural ways, &#8220;<i>something it was never meant to be</i>&#8220;. I feel that using HTML as I&#8217;ve shown last week and in this post would fall under the same category in his eyes. To him and to others who complain I say that I have absolutely no remorse about doing this. When I purchase anything I have the full rights (within the scope of the license) to adapt it in whatever way fits my needs. As a software developer and manager for over 25 years, I&#8217;ve developed in dozens of programming languages and environments, and I still enjoy [ab]using Matlab. Matlab is a great environment to get things done quickly and if this sometimes requires a bit of HTML or Java hacks that make some people cringe, then that&#8217;s their problem, not mine &#8211; I&#8217;m content with being able to do in Matlab [nearly] everything I want, quickly, and move on to the next project. As long as it gets the job done, that&#8217;s fine by me. If this makes me more of an engineer than a computer scientist, then so be it.<br />
On the flip side, I say to those who claim that Matlab is lacking in this or that aspect, that in most likelihood the limitation is only in their minds, not in Matlab &#8211; we can do amazing stuff with Matlab if we just open our minds, and possibly use some undocumented hacks. I&#8217;m not saying that Matlab has no limitations, I&#8217;m just saying that in most cases they can be overcome if we took the time and trouble to look for a solution. Matlab is a great tool and yet many people are not aware of its potential. Blaming Matlab for its failings is just an easy excuse in many cases. Of course, MathWorks could help my crusade on this subject by enabling useful features such as easy GUI component customizations&#8230;<br />
On this sad day, I wish you all <i>Shanah Tova!</i></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols">Icon images &amp; text in Matlab uicontrols</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/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>
<li><a href="https://undocumentedmatlab.com/articles/animated-busy-spinning-icon" rel="bookmark" title="Animated busy (spinning) icon">Animated busy (spinning) icon </a> <small>An animated spinning icon label can easily be embedded in Matlab GUI. ...</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/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/icon-images-in-matlab-uicontrols/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Programmatic shortcuts manipulation &#8211; part 1</title>
		<link>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programmatic-shortcuts-manipulation-part-1</link>
					<comments>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 23 Dec 2015 11:08:05 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6146</guid>

					<description><![CDATA[<p>Matlab Desktop shortcuts can be programmatically accessed and customized. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1">Programmatic shortcuts manipulation &#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/programmatic-shortcuts-manipulation-part-2" rel="bookmark" title="Programmatic shortcuts manipulation &#8211; part 2">Programmatic shortcuts manipulation &#8211; part 2 </a> <small>Non-standard shortcut controls and customizations can easily be added to the Matlab desktop. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/command-window-text-manipulation" rel="bookmark" title="Command Window text manipulation">Command Window text manipulation </a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plot-type-selection-components" rel="bookmark" title="Plot-type selection components">Plot-type selection components </a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>User-defined shortcuts can interactively be added to the Matlab Desktop to enable easy access to often-used scripts (e.g., clearing the console, running a certain program, initializing data etc.). Similarly, we can place shortcuts in the help browser to quickly access often-used pages. Unfortunately, both of these shortcut functionalities, like many other functionalities of the Matlab Desktop and related tools (Editor, Browser, Profiler etc.), have no documented programmatic access.<br />
Such programmatic access is often useful. For example, a large company for which I consult is using centralized updates to users&#8217; shortcuts, in order to manage and expose new features for all Matlab users from a central location. It is easy to send updates and manage a few users, but when your organization has dozens of Matlab users, centralized management becomes a necessity. It&#8217;s a pity that companies need to resort to external consultants and undocumented hacks to achieve this, but I&#8217;m not complaining since it keeps me occupied&#8230;<br />
<center><figure style="width: 320px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Shortcuts in Matlab R2012a and earlier" src="https://undocumentedmatlab.com/images/Shortcuts1_R2010a.png" title="Shortcuts in Matlab R2012a and earlier" width="320" height="190" /><figcaption class="wp-caption-text">Shortcuts in Matlab R2012a and earlier</figcaption></figure><br />
<figure style="width: 585px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Shortcuts in Matlab R2012b and newer" src="https://undocumentedmatlab.com/images/Shortcuts1_R2016a.png" title="Shortcuts in Matlab R2012b and newer" width="585" height="180" /><figcaption class="wp-caption-text">Shortcuts in Matlab R2012b and newer</figcaption></figure><br />
</center><br />
Today&#8217;s post will describe &#8220;regular&#8221; shortcuts &#8211; those that are simple clickable buttons. Next week I will show how we can extend this to incorporate other types of shortcut controls, as well as some advanced customizations.<br />
<span id="more-6146"></span></p>
<h3 id="xml">The <i>shortcuts.xml</i> file</h3>
<p>It turns out that the shortcults toolbar (on R2012a and earlier) or toolstrip group (on R2012b onward) is a reflection of the contents of the <i>[prefdir &#8216;\shortcuts.xml&#8217;]</i> file (depending on your version, the file might be named somewhat differently, i.e. <i>shortcuts_2.xml</i>). This file can be edited in any text editor, Matlab&#8217;s editor included. So a very easy way to programmatically affect the shortcuts is to update this file. Here is a sample of this file:</p>
<pre lang='xml'>
<?xml version="1.0" encoding="utf-8"?>
<favoritesroot version="2">
   <title>My Shortcuts</title>
   <favoritecategory>
      <name>Help Browser Favorites</name>
      <favorite>
         <label>Help Using the Desktop</label>
         <icon>Help icon</icon>
         <callback>helpview([docroot '/mapfiles/matlab_env.map'], 'matlabenvironment_desktop');</callback>
         <editable>true</editable>
      </favorite>
   </favoritecategory>
   <favorite>
      <label>CSSM</label>
      <icon>Help icon</icon>
      <callback>disp('No callback specified for this shortcut')</callback>
      <editable>true</editable>
   </favorite>
   <favorite>
      <label>UndocML</label>
      <icon>MATLAB icon</icon>
      <callback>web('undocumentedMatlab.com')</callback>
      <editable>true</editable>
   </favorite>
   <favorite>
      <label>My favorite program</label>
      <icon>C:\Yair\program\icon.gif</icon>
      <callback>cd('C:\Yair\program'); myProgram(123);</callback>
      <editable>true</editable>
   </favorite>
   ...
</favoritesroot>
</pre>
<p>The file is only loaded once during Matlab startup, so any changes made to it will only take effect after Matlab restarts.</p>
<h3 id="in-session">Updating the shortcuts in the current Matlab session</h3>
<p>We can update the shortcuts directly, in the current Matlab session, using the builtin <code>com.mathworks.mlwidgets.shortcuts.­ShortcutUtils</code> class. This class has existed largely unchanged for numerous releases (at least as far back as R2008b).<br />
For example, to add a new shortcut to the toolbar:</p>
<pre lang='matlab'>
name = 'My New Shortcut';
cbstr = 'disp(''My New Shortcut'')';  % will be eval'ed when clicked
iconfile = 'c:\path\to\icon.gif';  % default icon if it is not found
isEditable = 'true';
scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
category = scUtils.getDefaultToolbarCategoryName;
scUtils.addShortcutToBottom(name,cbstr,iconfile,category,isEditable);
</pre>
<p>The shortcut&#8217;s icon can either be set to a specific icon filepath (e.g., &#8216;C:\Yair\program\icon.jpg&#8217;), or to one of the predefined names: &#8216;Help icon&#8217;, &#8216;Standard icon&#8217;, &#8216;MATLAB icon&#8217; or &#8216;Simulink icon&#8217;. The <code>editable</code> parameter does not seem to have a visible effect that I could see.<br />
The category name can either be set to the default name using <code>scUtils.getDefaultToolbarCategoryName</code> (&#8216;Shortcuts&#8217; on English-based Matlab R2012b onward), or it can be set to any other name (e.g., &#8216;My programs&#8217;). To add a shortcut to the Help Browser (also known as a &#8220;Favorite&#8221;), simply set the category to <code>scUtils.getDefaultHelpCategoryName</code> (=&#8217;Help Browser Favorites&#8217; on English-based Matlab installations); to add the shortcut to the &#8216;Start&#8217; button, set the category to &#8216;Shortcuts&#8217;. When you use a non-default category name on R2012a and earlier, you will only see the shortcuts via Matlab&#8217;s &#8220;Start&#8221; button (as seen in the screenshot below); on R2012b onward you will see it as a new category group within the Shortcuts toolstrip (as seen in the screenshot above). For example:</p>
<pre lang='matlab'>
scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
scUtils.addShortcutToBottom('clear', 'clear; clc', 'Standard icon', 'Special commands', 'true');
</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Custom category in Matlab R2010a" src="https://undocumentedmatlab.com/images/Shortcuts2_R2010a.png" title="Custom category in Matlab R2010a" width="450" height="527" /><figcaption class="wp-caption-text">Custom category in Matlab R2010a</figcaption></figure></center><br />
To remove a shortcut, use the <i>removeShortcut(category,shortcutName)</i> method (note: this method does not complain if the specified shortcut does not exist):</p>
<pre lang='matlab'>scUtils.removeShortcut('Shortcuts', 'My New Shortcut');</pre>
<p>The <i>addShortcutToBottom()</i> method does not override existing shortcuts. Therefore, to ensure that we don&#8217;t add duplicate shortcuts, we must first remove the possibly-existing shortcut using <i>removeShortcut()</i> before adding it. Since <i>removeShortcut()</i> does not complain if the specific shortcut is not found, we can safely use it without having to loop over all the existing shortcuts. Alternately, we could loop over all existing category shortcuts checking their label, and adding a new shortcut only if it is not already found, as follows:</p>
<pre lang='matlab'>
scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
category = scUtils.getDefaultToolbarCategoryName;
scVector = scUtils.getShortcutsByCategory(category);
scArray = scVector.toArray;  % Java array
foundFlag = 0;
for scIdx = 1:length(scArray)
   scName = char(scArray(scIdx));
   if strcmp(scName, 'My New Shortcut')
      foundFlag = 1; break;
      % alternatively: scUtils.removeShortcut(category, scName);
   end
end
if ~foundFlag
   scUtils.addShortcutToBottom(scName, callbackString, iconString, category, 'true');
end
</pre>
<p>As noted above, we can add categories by simply specifying a new category name in the call to <i>scUtils.addShortcutToBottom()</i>. We can also add and remove categories directly, as follows (beware: when removing a category, it is removed together with all its contents):</p>
<pre lang='matlab'>
scUtils.addNewCategory('category name');
scUtils.removeShortcut('category name', []);  % entire category will be deleted
</pre>
<h3 id="FEX">Shortcut tools on the Matlab File Exchange</h3>
<p>Following <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/2802233/programmatically-configure-matlab">my advice on StackOverflow</a> back in 2010, Richie Cotton wrapped the code snippets above in a user-friendly utility (set of independent Matlab functions) that can now be found on the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/27567-shortcut-tools">Matlab File Exchange</a> and on <a target="_blank" rel="nofollow" href="http://4dpiecharts.com/2010/08/23/a-shortcut-to-success/">his blog</a>. Richie tested his toolbox on Matlab releases as old as R2008b, but the functionality may also work on even older releases.</p>
<h3 id="panel">Shortcuts panel embedded in Matlab GUI</h3>
<p>Shortcuts are normally visible in the toolbar and the Matlab start menu (R2012a and earlier) or the Matlab Desktop&#8217;s toolstrip (R2012b onward). However, using <code>com.mathworks.mlwidgets.shortcuts.ShortcutTreePanel</code>, the schortcuts can also be displayed in any user GUI, complete with right-click context-menu:</p>
<pre lang='matlab'>
jShortcuts = com.mathworks.mlwidgets.shortcuts.ShortcutTreePanel;
[jhShortcuts,hPanel] = javacomponent(jShortcuts, [10,10,300,200], gcf);
</pre>
<p><center><figure style="width: 323px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Shortcuts panel in Matlab figure GUI" src="https://undocumentedmatlab.com/images/Shortcuts3.png" title="Shortcuts panel in Matlab figure GUI" width="323" height="296" /><figcaption class="wp-caption-text">Shortcuts panel in Matlab figure GUI</figcaption></figure></center></p>
<h3 id="followup">Stay tuned&#8230;</h3>
<p>Next week I will expand the discussion of Matlab shortcuts with the following improvements:</p>
<ol>
<li>Displaying non-standard controls as shortcuts: checkboxes, drop-downs (combo-boxes) and toggle-buttons</li>
<li>Customizing the shortcut tooltip (replacing the default tooltip that simply repeats the callback string)</li>
<li>Customizing the shortcut callback (rather than using an <i><b>eval</b></i>-ed callback string)</li>
<li>Enabling/disabling shortcuts in run-time</li>
</ol>
<p>Merry Christmas everyone!</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1">Programmatic shortcuts manipulation &#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/programmatic-shortcuts-manipulation-part-2" rel="bookmark" title="Programmatic shortcuts manipulation &#8211; part 2">Programmatic shortcuts manipulation &#8211; part 2 </a> <small>Non-standard shortcut controls and customizations can easily be added to the Matlab desktop. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/command-window-text-manipulation" rel="bookmark" title="Command Window text manipulation">Command Window text manipulation </a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plot-type-selection-components" rel="bookmark" title="Plot-type selection components">Plot-type selection components </a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/r2009b-keyboard-bindings" rel="bookmark" title="R2009b keyboard bindings">R2009b keyboard bindings </a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing Matlab uipanels</title>
		<link>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-matlab-uipanels</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Feb 2015 12:24:50 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[HG2]]></category>
		<category><![CDATA[uipanel]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=5595</guid>

					<description><![CDATA[<p>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels">Customizing Matlab uipanels</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-uicontrol-border" rel="bookmark" title="Customizing uicontrol border">Customizing uicontrol border </a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</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/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>The major innovation in Matlab release R2014b was the introduction of the new handle-based graphics system (<a target="_blank" href="/articles/hg2-update">HG2</a>). However, this release also included a few other improvements to graphics/GUI that should not be overlooked. The most notable is that <i><b>uitab</b></i>s are finally officially documented/supported, following a decade or being undocumented (well, undocumented in the official sense, since I took the time to document this functionality in <a target="_blank" href="/articles/tab-panels-uitab-and-relatives">this blog</a> and in my <a target="_blank" href="/books/matlab-java">Matlab-Java book</a>).<br />
A less-visible improvement occurred with <i><b>uipanel</b></i>s: Panels are very important containers when designing GUIs. They enable a visual grouping of related controls and introduce order to an otherwise complex GUI. Unfortunately, until R2014b panels were drawn at the canvas level, and did not use a standard Java Swing controls like other uicontrols. This made it impossible to customize <i><b>uipanel</b></i>s in a similar manner to other GUI uicontrols (<a target="_blank" href="/articles/button-customization">example</a>).<br />
In R2014b, <i><b>uipanel</b></i>s have finally become standard Java Swing controls, a <code>com.mathworks.hg.peer.ui.UIPanelPeer$UIPanelJPanel</code> component that extends Swing&#8217;s standard <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html">javax.swing.JPanel</a></code> and Matlab&#8217;s ubiquitous <code>com.mathworks.mwswing.MJPanel</code>. This means that we can finally customize it in various ways that are not available in plain Matlab.<br />
We start the discussion with a simple Matlab code snippet. It is deliberately simple, since I wish to demonstrate only the panel aspects:</p>
<pre lang='matlab'>
figure('Menubar','none', 'Color','w');
hPanel = uipanel('Title','Panel title', 'Units','norm', 'Pos',[.1,.1,.6,.7]);
hButton = uicontrol('String','Click!', 'Parent',hPanel);
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Standard Matlab uipanel" src="https://undocumentedmatlab.com/images/uipanel1.gif" title="Standard Matlab uipanel" width="175" height="140" /><figcaption class="wp-caption-text">Standard Matlab <i><b>uipanel</b></i></figcaption></figure></center><br />
Notice the default &#8216;etchedin&#8217; panel border, which I hate (note the broken edges at the corners). Luckily, Swing includes a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/border.html">wide range of alternative borders</a> that we can use. I&#8217;ve already <a target="_blank" href="/articles/customizing-uicontrol-border">demonstrated</a> customizing Matlab uicontrols with Java borders back in 2010 (has it really been that long? wow!). In R2014b we can finally do something similar to <i><b>uipanel</b></i>s:<br />
<span id="more-5595"></span><br />
The first step is to get the <i><b>uipanel</b></i>&#8216;s underlying Java component&#8217;s reference. We can do this using my <a target="_blank" href="/articles/findjobj-find-underlying-java-object"><i><b>findjobj</b></i> utility</a>, but in the specific case of <i><b>uipanel</b></i> we are lucky to have a direct shortcut by using the panel&#8217;s undocumented hidden property <b>JavaFrame</b> and its <b>PrintableComponent</b> property:</p>
<pre lang='matlab'>
>> jPanel = hPanel.JavaFrame.getPrintableComponent
jPanel =
com.mathworks.hg.peer.ui.UIPanelPeer$UIPanelJPanel[,0,0,97x74,...]
</pre>
<p>Let&#8217;s now take a look at the <code>jPanel</code>&#8216;s border:</p>
<pre lang='matlab'>
>> jPanel.getBorder
ans =
com.mathworks.hg.peer.ui.borders.TitledBorder@25cd9b97
>> jPanel.getBorder.get
                Border: [1x1 com.mathworks.hg.peer.ui.borders.EtchedBorderWithThickness]
          BorderOpaque: 0
                 Class: [1x1 java.lang.Class]
                 Title: 'Panel title'
            TitleColor: [1x1 java.awt.Color]
             TitleFont: [1x1 java.awt.Font]
    TitleJustification: 1
         TitlePosition: 2
</pre>
<p>Ok, simple enough. Let&#8217;s replace the border&#8217;s <code>EtchedBorderWithThickness</code> with something more appealing. We start with a simple red <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/LineBorder.html">LineBorder</a></code> having rounded corners and 1px width:</p>
<pre lang='matlab'>
jColor = java.awt.Color.red;  % or: java.awt.Color(1,0,0)
jNewBorder = javax.swing.border.LineBorder(jColor, 1, true);  % red, 1px, rounded=true
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Rounded-corners LineBorder" src="https://undocumentedmatlab.com/images/uipanel2.gif" title="Rounded-corners LineBorder" width="175" height="140" /><figcaption class="wp-caption-text">Rounded-corners LineBorder</figcaption></figure></center><br />
Or maybe a thicker non-rounded orange border:</p>
<pre lang='matlab'>
jColor = java.awt.Color(1,0.5,0);
jNewBorder = javax.swing.border.LineBorder(jColor, 3, false);  % orange, 3px, rounded=false
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Another LineBorder example" src="https://undocumentedmatlab.com/images/uipanel3.gif" title="Another LineBorder example" width="175" height="140" /><figcaption class="wp-caption-text">Another LineBorder example</figcaption></figure></center><br />
Or maybe a <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/MatteBorder.html">MatteBorder</a></code> with colored insets:</p>
<pre lang='matlab'>
jColor = java.awt.Color(0,0.3,0.8);  % light-blue
jNewBorder = javax.swing.border.MatteBorder(2,5,8,11,jColor)  % top,left,bottom,right, color
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="MatteBorder with solid insets" src="https://undocumentedmatlab.com/images/uipanel4.gif" title="MatteBorder with solid insets" width="175" height="140" /><figcaption class="wp-caption-text">MatteBorder with solid insets</figcaption></figure></center><br />
<code>MatteBorder</code> can also use an icon (rather than a solid color) to fill the border insets. First, let&#8217;s load the icon. We can either load a file directly from disk, or use one of Matlab&#8217;s standard icons. Here are both of these alternatives:</p>
<pre lang='matlab'>
% Alternative #1: load from disk file
icon = javax.swing.ImageIcon('C:\Yair\star.gif');
% Alternative #2: load a Matlab resource file
jarFile = fullfile(matlabroot,'/java/jar/mlwidgets.jar');
iconsFolder = '/com/mathworks/mlwidgets/graphics/resources/';
iconURI = ['jar:file:/' jarFile '!' iconsFolder 'favorite_hoverover.png'];  % 14x14 px
icon = javax.swing.ImageIcon(java.net.URL(iconURI));
</pre>
<p>We can now pass this icon reference to <code>MatteBorder</code>&#8216;s constructor:</p>
<pre lang='matlab'>
w = icon.getIconWidth;
h = icon.getIconHeight;
jNewBorder = javax.swing.border.MatteBorder(h,w,h,w,icon)  % top,left,bottom,right, icon
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="MatteBorder with icon insets" src="https://undocumentedmatlab.com/images/uipanel5.gif" title="MatteBorder with icon insets" width="175" height="140" /><figcaption class="wp-caption-text">MatteBorder with icon insets</figcaption></figure></center><br />
Additional useful Swing borders can be found in the list of classes implementing the <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/Border.html"><code>Border</code> interface</a>, or via the <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html">BorderFactory</a></code> class. For example, let&#8217;s create a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html#createDashedBorder(java.awt.Paint,%20float,%20float,%20float,%20boolean)">dashed border</a> having a 3-2 ratio between the line lengths and the spacing:</p>
<pre lang='matlab'>
jColor = java.awt.Color.blue;  % or: java.awt.Color(0,0,1);
lineWidth = 1;
relativeLineLength = 3;
relativeSpacing = 2;
isRounded = false;
jNewBorder = javax.swing.BorderFactory.createDashedBorder(jColor,lineWidth,relativeLineLength,relativeSpacing,isRounded);
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="StrokeBorder (dashed)" src="https://undocumentedmatlab.com/images/uipanel6.gif" title="StrokeBorder (dashed)" width="175" height="140" /><figcaption class="wp-caption-text">StrokeBorder (dashed)</figcaption></figure></center><br />
After seeing all these possibilities, I think you&#8217;ll agree with me that Matlab&#8217;s standard <i><b>uipanel</b></i> borders look pale in comparison.<br />
Have you used any interesting borders in your Matlab GUI? Or have you customized your panels in some other nifty manner? If so, then please place a comment below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels">Customizing Matlab uipanels</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-uicontrol-border" rel="bookmark" title="Customizing uicontrol border">Customizing uicontrol border </a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</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/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing listbox/combobox items</title>
		<link>https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-listbox-combobox-items</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 17 Sep 2014 12:01:34 +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[UI controls]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=5029</guid>

					<description><![CDATA[<p>Matlab listboxes can be customized using custom Java cell-renderers. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items">Customizing listbox/combobox items</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-combobox-popups" rel="bookmark" title="Customizing combobox popups">Customizing combobox popups </a> <small>Matlab combobox (dropdown) popups can be customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3" rel="bookmark" title="Customizing menu items part 3">Customizing menu items part 3 </a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-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>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Last week I <a target="_blank" href="/articles/checkboxlist">wrote</a> about using a variety of techniques to customize listbox items with an attached checkbox icon. Some of these methods used a standard Matlab listbox uicontrol, others used other controls. Today I wish to extend the discussion and show how Matlab listbox and combobox (pop-up) items can be customized in a variety of ways.<br />
To add icons to listbox/combobox items, we could use standard HTML, as I&#8217;ve shown last week. This is the simplest method, requires no Java knowledge, and it usually works well. The problem is that when a listbox/combobox has numerous items (hundreds or more), it may start getting sluggish. In such case it is faster to use a dedicated Java cell-renderer that sets the icon, font, colors, tooltip and other aspects on an item-by-item basis. This runs faster and enables far greater customizability than what is possible with HTML. The drawback is that it requires some Java programming. No free lunch&#8230;<br />
Listbox and combobox cell-renderers need to extend <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/ListCellRenderer.html"><code>javax.swing.ListCellRenderer</code></a>, similarly to <a target="_blank" href="/articles/uitable-cell-colors#colors">uitable cell-renderers</a>. This is basically a simple Java class that minimally contains just an empty constructor and a <i>getListCellRendererComponent()</i> method with a predefined signature. <i>getListCellRendererComponent()</i> is automatically called by the Swing render engine separately for each listbox item, and gets as input args a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html"><code>JList</code></a> reference, the item value (typically a string), an integer list index, a boolean flag indicating whether the item is currently selected, and another flag indicating whether the item is currently in focus. <i>getListCellRendererComponent()</i> uses these parameters to customize and return a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html"><code>java.awt.Component</code></a>, which is typically (but not necessarily) a standard Swing <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html"><code>JLabel</code></a>.<br />
Here is a simple example that displays a folder of icon files in a Matlab listbox and combobox. Each item is the filename, with a customization that if the file is an icon, then this icon is displayed next to the file name, otherwise the name appears in <font color="red"><i>red italic</i></font> without an icon. For illustration, we&#8217;ll use Matlab&#8217;s builtin icons folder: <i>%matlabroot%/toolbox/matlab/icons/</i>:<br />
<center><img loading="lazy" decoding="async" alt="Custom listbox cell-renderer" src="https://undocumentedmatlab.com/images/LabelListBoxRenderer.gif" title="Custom listbox cell-renderer" width="124" height="154" /> &nbsp;&nbsp; <img loading="lazy" decoding="async" alt="Custom combobox cell-renderer" src="https://undocumentedmatlab.com/images/LabelListBoxRenderer2.gif" title="Custom combobox cell-renderer" width="482" height="150" /></center><br />
<span id="more-5029"></span></p>
<h3 id="renderer">Creating the cell renderer</h3>
<p>We start by creating a custom <code>ListCellRenderer</code>. Place the following code in a file called <a target="_blank" href="http://files.undocumentedmatlab.com/LabelListBoxRenderer.java"><i>LabelListBoxRenderer.java</i></a>:</p>
<pre lang='java'>
import java.awt.*;
import javax.swing.*;
import java.util.Hashtable;
public class LabelListBoxRenderer extends JLabel implements ListCellRenderer
{
    private String folderPath;
    private final Hashtable<string,ImageIcon> iconsCache = new Hashtable<string,ImageIcon>();
    // Class constructors
    public LabelListBoxRenderer() {
        setOpaque(true);
        setHorizontalAlignment(LEFT);
        setVerticalAlignment(CENTER);
    }
    public LabelListBoxRenderer(String folderPath) {
        this();
        this.folderPath = folderPath;
    }
    // Return a label displaying both text and image.
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus)
    {
        String label = value.toString();
        setFont(list.getFont());
        if (isSelected) {
            // Selected cell item
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            // Unselected cell item
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        try {
            String iconFname = (folderPath + "/" + label).replace('\\', '/');
            ImageIcon icon = getFileIcon(iconFname);
            setIcon(icon);
            if (icon.getIconWidth() > 0) {
                // Cell item is a valid icon filename
                list.setToolTipText(iconFname + " (" + icon.getIconWidth() + " x " + icon.getIconHeight() + ")");
            } else {
                // Cell item is not an icon filename
                list.setToolTipText(iconFname + " (not an icon)");
                setFont(getFont().deriveFont(Font.ITALIC));
                setForeground(Color.red);
            }
        } catch (Exception e) {
            list.setToolTipText(e.getMessage());
        }
        //System.out.println(index + ": " + label);  // debug console printout
        setText(label);
        return this;
    }
    // Modify the folder path (default = current folder)
    public void setFolderPath(String folderPath) {
        this.folderPath = folderPath;
    }
    // Lazily load the file icons only as needed, later reuse cached data
    private ImageIcon getFileIcon(String filename) {
        ImageIcon icon;
        if (iconsCache.containsKey(filename)) {
            // Reuse cached data
            icon = iconsCache.get(filename);
        } else {
            // Lazily load the file icons only as needed
            icon = new ImageIcon(filename);
            iconsCache.put(filename, icon);  // store in cache for later use
        }
        return icon;
    }
}
</pre>
<p>In the code above, I&#8217;ve cached the <code>ImageIcons</code>, so that the actual disk file is only accessed once rather than repeatedly whenever the cell needs to be rendered. For even improved performance, we could also cache the tooltips and derived italic font so that they would not be regenerated each time (note that the list&#8217;s font is not the same as the cell component&#8217;s font) &#8211; I will leave this as an exercise to the reader.<br />
Next, compile this file (using the standard javac compiler or any Java IDE), ensuring that you target a JVM compatible with your Matlab (JVM 5 will work on R14SP2 onward, 6 on R2007b onward, and 7 only on R2013b or newer). For convenience, both the source and compiled files for the LabelListBoxRenderer class can be downloaded here: <a target="_blank" href="http://files.undocumentedmatlab.com/LabelListBoxRenderer.java"><i>LabelListBoxRenderer.java</i></a>, <a target="_blank" href="http://files.undocumentedmatlab.com/LabelListBoxRenderer.class"><i>LabelListBoxRenderer.class</i></a>.</p>
<h3 id="listbox">Using the cell-renderer in Matlab listboxes</h3>
<p>Now that we have a custom cell renderer, we should add the <i>LabelListBoxRenderer.class</i> file to Matlab&#8217;s Java classpath using the <i><b>javaaddpath</b></i> function and then use this class in Matlab:</p>
<pre lang='matlab'>
% Create the Matlab listbox
iconsFolder = fullfile(matlabroot, 'toolbox/matlab/icons');
files = dir(iconsFolder);
hListbox = uicontrol('Style','list', 'String',{files.name}, 'Position',[10,10,120,150]);
% Find the uicontrol's underlying Java component
jScrollpane = findjobj(hListbox);
jListbox = jScrollpane.getViewport.getView;
% Update the listbox's cell-renderer
javaaddpath 'C:\Yair\Undocumented Matlab\Work\'   % location of my LabelListBoxRenderer.class
jRenderer = LabelListBoxRenderer(iconsFolder);
jListbox.setCellRenderer(jRenderer);
% Give the icons some space...
jListbox.setFixedCellHeight(18);
</pre>
<p><center><img loading="lazy" decoding="async" alt="Custom listbox cell-renderer" src="https://undocumentedmatlab.com/images/LabelListBoxRenderer.gif" title="Custom listbox cell-renderer" width="124" height="154" /></center><br />
Wasn&#8217;t too painful was it?</p>
<h3 id="combobox">Using the cell-renderer in Matlab combo-boxes</h3>
<p>Customizing Matlab combo-boxes is just as easy, and uses the same LabelListBoxRenderer class:</p>
<pre lang='matlab'>
% Create the Matlab combobox
iconsFolder = fullfile(matlabroot, 'toolbox/matlab/icons');
files = dir(iconsFolder);
hCombobox = uicontrol('Style','popup', 'String',{files.name}, 'Position',[10,10,120,150]);
% Find the uicontrol's underlying Java component
jCombobox = findjobj(hCombobox);  % no scroll-pane for combos
% Update the combobox's cell-renderer
javaaddpath 'C:\Yair\Undocumented Matlab\Work\'   % location of my LabelListBoxRenderer.class
jRenderer = LabelListBoxRenderer(iconsFolder);
jCombobox.setRenderer(jRenderer);  % Note: not setCellRenderer()
% Give the icons some space...
jCombobox.setFixedCellHeight(18);
% Make the drop-down list shorter than the default (=20 items)
jCombobox.setMaximumRowCount(8);
</pre>
<p><center><img loading="lazy" decoding="async" alt="Custom combobox cell-renderer" src="https://undocumentedmatlab.com/images/LabelListBoxRenderer2.gif" title="Custom combobox cell-renderer" width="482" height="150" /></center><br />
For additional aspects of listbox and combobox customizations, refer to sections 6.6 and 6.7 of my <a target="_blank" href="/matlab-java-book">Matlab-Java programming book</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items">Customizing listbox/combobox items</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-combobox-popups" rel="bookmark" title="Customizing combobox popups">Customizing combobox popups </a> <small>Matlab combobox (dropdown) popups can be customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3" rel="bookmark" title="Customizing menu items part 3">Customizing menu items part 3 </a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars" rel="bookmark" title="Customizing listbox &amp; editbox scrollbars">Customizing listbox &amp; editbox scrollbars </a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-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>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Animated busy (spinning) icon</title>
		<link>https://undocumentedmatlab.com/articles/animated-busy-spinning-icon?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=animated-busy-spinning-icon</link>
					<comments>https://undocumentedmatlab.com/articles/animated-busy-spinning-icon#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 16 Apr 2014 08:00:53 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Internal component]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4769</guid>

					<description><![CDATA[<p>An animated spinning icon label can easily be embedded in Matlab GUI. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/animated-busy-spinning-icon">Animated busy (spinning) icon</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/displaying-animated-gifs" rel="bookmark" title="Displaying animated GIFs">Displaying animated GIFs </a> <small>It is possible to easily display animated images in Matlab figure windows. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/font-selection-components" rel="bookmark" title="Font selection components">Font selection components </a> <small>Several built-in components enable programmatic font selection in Matlab GUI - this article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitable-sorting" rel="bookmark" title="Uitable sorting">Uitable sorting </a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab includes a wide variety of internal widgets (GUI components) that could be very useful in our GUIs. One such widget is an animated spinning icon, which is often used by Matlab itself and numerous toolboxes to illustrate a running task:<br />
<center><figure style="width: 400px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Sample usage of an animated spinning icon" src="https://undocumentedmatlab.com/images/BusyAffordance1c.png" title="Sample usage of an animated spinning icon" width="400" height="326" /><figcaption class="wp-caption-text">Sample usage of an animated spinning icon</figcaption></figure></center><br />
One of the internal widgets that are readily-available for use in our Matlab GUI and displays a similar (but not identical) spinning icon is <code>BusyAffordance</code>, which is included in the built-in <code>com.mathworks.widgets</code> package. <code>BusyAffordance</code> creates a visible panel with an animated spinning icon and optional text label as long as the panel&#8217;s object is in the “started” mode (the mode can be started/stopped numerous times).<br />
<center><figure style="width: 120px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Animated spinner icon" src="https://undocumentedmatlab.com/images/spinner.gif" title="Animated spinner icon" width="16" height="16" /><figcaption class="wp-caption-text">Animated spinner icon</figcaption></figure></center><br />
The usage is very simple:<span id="more-4769"></span></p>
<pre lang='matlab'>
try
    % R2010a and newer
    iconsClassName = 'com.mathworks.widgets.BusyAffordance$AffordanceSize';
    iconsSizeEnums = javaMethod('values',iconsClassName);
    SIZE_32x32 = iconsSizeEnums(2);  % (1) = 16x16,  (2) = 32x32
    jObj = com.mathworks.widgets.BusyAffordance(SIZE_32x32, 'testing...');  % icon, label
catch
    % R2009b and earlier
    redColor   = java.awt.Color(1,0,0);
    blackColor = java.awt.Color(0,0,0);
    jObj = com.mathworks.widgets.BusyAffordance(redColor, blackColor);
end
jObj.setPaintsWhenStopped(true);  % default = false
jObj.useWhiteDots(false);         % default = false (true is good for dark backgrounds)
javacomponent(jObj.getComponent, [10,10,80,80], gcf);
jObj.start;
    % do some long operation...
jObj.stop;
jObj.setBusyText('All done!');
</pre>
<p>Note how I&#8217;ve used the <a target="_blank" href="/articles/javacomponent"><i><b>javacomponent</b></i> function</a> to place the <code>BusyAffordance</code> object onscreen, at position 10,10 of the current Matlab figure, and gave it an initial size of 80&#215;80 pixels.<br />
When the object is <i>stop</i>()ed, the icon and label are removed by default, but can be displayed un-animated (non-spinning) via the <b>PaintsWhenStopped</b> property:<br />
<center><figure style="width: 140px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="BusyAffordance started... (animated spinning icon)" src="https://undocumentedmatlab.com/images/BusyAffordance2c.png" title="BusyAffordance started... (animated spinning icon)" width="80" height="80" /><figcaption class="wp-caption-text">BusyAffordance started...<br />(animated spinning icon)</figcaption></figure>  &nbsp;&nbsp;&nbsp;  <figure style="width: 160px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="...stopped (PaintsWhenStopped = false)" src="https://undocumentedmatlab.com/images/BusyAffordance2a.png" title="...stopped (PaintsWhenStopped = false)" width="80" height="80" /><figcaption class="wp-caption-text">...stopped<br />(PaintsWhenStopped = false)</figcaption></figure>  &nbsp;&nbsp;&nbsp;  <figure style="width: 150px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="...stopped (PaintsWhenStopped = true)" src="https://undocumentedmatlab.com/images/BusyAffordance2b.png" title="...stopped (PaintsWhenStopped = true)" width="80" height="80" /><figcaption class="wp-caption-text">...stopped<br />(PaintsWhenStopped = true)</figcaption></figure></center><br />
The small-icon (16&#215;16) variant of the <code>BusyAffordance</code> control is used by another internal Matlab component, <code>ProgressBarDialog</code>. This control presents an animated progress-bar dialog window, similar to Matlab’s built-in <i><b>waitbar</b></i> function but with an animated busy icon:</p>
<pre lang='matlab'>
d = com.mathworks.mlwidgets.dialog.ProgressBarDialog.createProgressBar('test...', []);
d.setValue(0.75);                        % default = 0
d.setProgressStatusLabel('testing...');  % default = 'Please Wait'
d.setSpinnerVisible(true);               % default = true
d.setCircularProgressBar(false);         % default = false  (true means an indeterminate (looping) progress bar)
d.setCancelButtonVisible(true);          % default = true
d.setVisible(true);                      % default = false
</pre>
<p><center><figure style="width: 308px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="ProgressBarDialog with animated BusyAffordance" src="https://undocumentedmatlab.com/images/ProgressBarDialog.png" title="ProgressBarDialog with animated BusyAffordance" width="308" height="120" /><figcaption class="wp-caption-text">ProgressBarDialog with animated BusyAffordance</figcaption></figure></center><br />
For those interested, the 16&#215;16 animated GIF used here is <i>spinner.gif</i>, which can be found in <i>%matlabroot%/java/jar/mlwidgets.jar</i> (remember that JAR files are simply ZIP files, so they can be opened in WinZip/WinRar etc.). <code>BusyAffordance</code> also includes a 32&#215;32 icon which is not available as a separate icon file. Also, the <code>BusyAffordance</code> spin direction is reversed compared to the spinner icon. From this we learn that <code>BusyAffordance</code> probably creates its spinner image on-the-fly (programmatically), rather than use <i>spinner.gif</i>.<br />
For additional information on <code>BusyAffordance</code>, <code>com.mathworks.widgets</code> and other internal Matlab components, refer to chapter 5 in my <a target="_blank" href="/matlab-java-book">Matlab-Java programming book</a>.<br />
As with other internal Matlab components, I categorized this feature as &#8220;<i>High risk of breaking in a future release</i>&#8220;. Still, <code>BusyAffordance</code> has existed in its present form since R2010a (and with a slightly different interface for years before then), so there&#8217;s a good chance that it will continue as-is in the foreseeable future. Then again, it might be removed as early as the next upcoming release, without prior warning&#8230;<br />
More advanced animated busy indications, including automated percentage and time-remaining labels, can be specified using <a target="_blank" rel="nofollow" href="http://code.google.com/p/jbusycomponent"><code>JBusyComponent</code></a>, which is a <a target="_blank" rel="nofollow" href="https://java.net/projects/JXLayer">JXLayer</a> decorator that can be applied to any displayable component. In R2013b or newer that use Java7, we can also use the built-in <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html#animating"><code>JLayer</code></a> class. We can also simply embed the animated GIF image directly in our GUI, <a target="_blank" href="/articles/displaying-animated-gifs">as I explained here</a>.<br />
In the past I have already shown how to use other internal components in Matlab&#8217;s <code>com.mathworks.widgets</code> package, including <a target="_blank" href="/articles/syntax-highlighted-labels-panels">syntax-highlighted labels and text panes</a> that can be embedded in our Matlab GUI.<br />
Have you used <code>BusyAffordance</code> or some other internal Matlab component in a neat way in your GUI? if so, please do tell us about it in <a href="/articles/animated-busy-spinning-icon#respond">a comment below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/animated-busy-spinning-icon">Animated busy (spinning) icon</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/displaying-animated-gifs" rel="bookmark" title="Displaying animated GIFs">Displaying animated GIFs </a> <small>It is possible to easily display animated images in Matlab figure windows. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/font-selection-components" rel="bookmark" title="Font selection components">Font selection components </a> <small>Several built-in components enable programmatic font selection in Matlab GUI - this article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uitable-sorting" rel="bookmark" title="Uitable sorting">Uitable sorting </a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/animated-busy-spinning-icon/feed</wfw:commentRss>
			<slash:comments>51</slash:comments>
		
		
			</item>
		<item>
		<title>Rich-contents log panel</title>
		<link>https://undocumentedmatlab.com/articles/rich-contents-log-panel?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rich-contents-log-panel</link>
					<comments>https://undocumentedmatlab.com/articles/rich-contents-log-panel#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 18 Sep 2013 14:00:16 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4180</guid>

					<description><![CDATA[<p>Matlab listboxes and editboxes can be used to display rich-contents HTML-formatted strings, which is ideal for log panels. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/rich-contents-log-panel">Rich-contents log panel</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/rich-matlab-editbox-contents" rel="bookmark" title="Rich Matlab editbox contents">Rich Matlab editbox contents </a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/aligning-uicontrol-contents" rel="bookmark" title="Aligning uicontrol contents">Aligning uicontrol contents </a> <small>Matlab uicontrols can often be customized using plain HTML/CSS, without need for advanced Java. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/gui-integrated-html-panel" rel="bookmark" title="GUI integrated HTML panel">GUI integrated HTML panel </a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</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>I often include a log panel in Matlab applications that process data. By sending messages to the log panel, I can avoid msgbox popups and Command Window messages that interfere with the regular workflow. In Matlab, such log panels typically use a simple listbox or editbox control. The problem with this is that all text messages in the log panel look the same. Matlab does not have a documented way to highlight specific messages or words. Well, this has never stopped us before, has it? 🙂</p>
<h3 id="listbox">The listbox solution</h3>
<p>I have often noted in past articles that <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">most Matlab uicontrols support HTML formatting</a>. We can use a listbox control for our log panel, and use HTML formatting for the various log messages, based on their severity or content. For example:<br />
<figure id="attachment_27" aria-describedby="caption-attachment-27" style="width: 100px" class="wp-caption alignright"><img loading="lazy" decoding="async" class="size-full wp-image-27" title="Listbox with HTML colored items" src="https://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/html11.png" alt="Listbox with HTML'ed items" width="70" height="70" /><figcaption id="caption-attachment-27" class="wp-caption-text">Listbox with HTML colored items</figcaption></figure></p>
<pre lang='matlab'>
uicontrol('Style','list', 'Position',[10,10,70,70], 'String', ...
   {'<html><font color="red">Hello</font></html>', 'world', ...
    '<html><font style="font-family:impact;color:green"><i>What a', ...   % note: </i></font></html> are not needed
    '<html><font color="blue" face="Comic Sans MS">nice day!</font>'});   % note: </html> is not needed
</pre>
<p>Rich listboxes such as this are very easy to set up and use. As I just showed, all it takes is sending an HTML string to a regular listbox. The down-side is that listboxes only display single-line messages, so if our message is too long we need to manually split it into separate listbox lines, which is rather inconvenient. Using HTML &lt;br/&gt; does not work since the allocated line height remains unchanged. We can fix this by playing around with the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/">underlying Java object</a>&#8216;s RowHeight property, but that again is rather inconvenient.</p>
<h3 id="editbox">The editbox solution</h3>
<p>Instead of a listbox, I often use a multi-line editbox. Unfortunately, HTML is not as-easy to use in multi-line editbox contents, but as I have <a target="_blank" href="/articles/rich-matlab-editbox-contents/">shown before</a>, it is indeed possible and actually quite powerful. In fact, I am using such an editbox-based log panel&#8217;s in my online presentation at the <a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900">MATLAB Computational Finance Virtual Conference</a> tomorrow (Thursday Sep 19, 2013 at 2pm EST / 8pm CEST, see details below):<br />
<center><figure style="width: 650px" class="wp-caption aligncenter"><a target="_blank" href="/images/demo_screenshot2.png"><img loading="lazy" decoding="async" alt="Real-time trading system demo in Matlab (click to zoom)" src="https://undocumentedmatlab.com/images/demo_screenshot2.png" title="Real-time trading system demo in Matlab (click to zoom)" width="650" height="398" /></a><figcaption class="wp-caption-text">Real-time trading system demo in Matlab (click to zoom)</figcaption></figure></center><br />
We can see in the screenshot that some log messages (the red warning message) can span multiple lines. Moreover, the panels can be interactively dragged/resized, making the multi-line log messages &#8220;flow&#8221; based on available space. So using an editbox is preferable to a listbox. The solution implemented in the demo is quite simple:<br />
<span id="more-4180"></span><br />
First we define the logging utility function (the icon filenames may need to be changed based on your Matlab release):</p>
<pre lang="matlab">
function logMessage(jEditbox,text,severity)
   % Ensure we have an HTML-ready editbox
   HTMLclassname = 'javax.swing.text.html.HTMLEditorKit';
   if ~isa(jEditbox.getEditorKit,HTMLclassname)
      jEditbox.setContentType('text/html');
   end
   % Parse the severity and prepare the HTML message segment
   if nargin < 3,  severity='info';  end
   switch lower(severity(1))
      case 'i',  icon = 'greenarrowicon.gif'; color='gray';
      case 'w',  icon = 'demoicon.gif';       color='black';
      otherwise, icon = 'warning.gif';        color='red';
   end
   icon = fullfile(matlabroot,'toolbox/matlab/icons',icon);
   iconTxt =['<img decoding="async" src="file:///',icon,'" height=16 width=16/>'];
   msgTxt = ['&nbsp;<font color=',color,'>',text,'</font>'];
   newText = [iconTxt,msgTxt];
   endPosition = jEditbox.getDocument.getLength;
   if endPosition>0, newText=['<br />' newText];  end
   % Place the HTML message segment at the bottom of the editbox
   currentHTML = char(jEditbox.getText);
   jEditbox.setText(strrep(currentHTML,[60 '/body>'],newText));
   endPosition = jEditbox.getDocument.getLength;
   jEditbox.setCaretPosition(endPosition); % end of content
end
</pre>
<p>Next, we initialize the log panel editbox and store the editbox&#8217;s underlying Java component (<code>jEditbox</code>) using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> utility:</p>
<pre lang='matlab'>
% Prepare the log editbox
hLogPanel = uicontrol('style','edit', 'max',5, 'Parent',hLeftBottomPanel, 'Units','norm', 'Position',[0,0.2,1,0.8], 'Background','w');
% Get the underlying Java editbox, which is contained within a scroll-panel
jScrollPanel = findjobj(hLogPanel);
try
    jScrollPanel.setVerticalScrollBarPolicy(jScrollPanel.java.VERTICAL_SCROLLBAR_AS_NEEDED);
    jScrollPanel = jScrollPanel.getViewport;
catch
    % may possibly already be the viewport, depending on release/platform etc.
end
jEditbox = handle(jScrollPanel.getView,'CallbackProperties');
</pre>
<p>Now, we can use this logging utility function to log messages in our application. For example:</p>
<pre lang="matlab">
logMessage(jEditbox, 'a regular info message...');
logMessage(jEditbox, 'a warning message...', 'warn');
logMessage(jEditbox, 'an error message!!!', 'error');
logMessage(jEditbox, 'a regular message again...', 'info');
</pre>
<p><center><figure style="width: 200px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Rich editbox contents (a log file)" src="https://undocumentedmatlab.com/images/editbox10.png" title="Rich editbox contents (a log file)" width="200" height="60" /><figcaption class="wp-caption-text">Rich editbox contents (a log file)</figcaption></figure></center><br />
HTML editboxes are normally editable, images included. In actual applications, we usually wish to prevent editing the displayed log. To do this, we simply call <code>jEditbox.<i>setEditable(false)</i></code>. Similarly, it is easy to set-up a Matlab callback-function to handle hyperlink clicks in the log panel (unlike what we might think, this is not handled automatically by the HTML processing engine):</p>
<pre lang='matlab'>
% Prevent user editing in the log-panel
jEditbox.setEditable(false);
% Set-up a Matlab callback function to handle hyperlink clicks
set(jEditbox,'HyperlinkUpdateCallback',@linkCallbackFcn);
</pre>
<h3 id="conf">About the MATLAB Computational Finance Virtual Conference and my presentation</h3>
<p>The MATLAB Computational Finance Virtual Conference is a one-day (Thursday Sep 19, 2013) event of hour-long presentations by industry professionals that showcase real-world examples demonstrating how financial-industry researchers and developers can excel at their jobs, improve their research and business processes, reduce costs, and mitigate risks by using Matlab. <a target="_blank" rel="nofollow" href="http://engage.vevent.com/registr_form.jsp?eid=1972&#038;seid=900&#038;language-code=en&#038;country-code=US&#038;page=1&#038;no-login=false">Registration</a> to the conference is free and it&#8217;s a virtual conference, so there&#8217;s no need for a tie and jacket and you&#8217;re welcome to join&#8230;<br />
<center><a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900"><img loading="lazy" decoding="async" alt="MATLAB Computational Finance Conference 2013" src="https://undocumentedmatlab.com/images/MATLAB_Computational_Finance_Conference_2013.jpg" title="MATLAB Computational Finance Conference 2013" width="600" height="112" /></a></center><br />
My presentation on &#8220;<i>A Real-Time Trading System in MATLAB</i>&#8220;, is scheduled for 2pm EST / 8pm CEST. Following a half-hour presentation, I will answer audience questions online. The presentation slides can be <a target="_blank" href="/files/Matlab%20real-time%20trading%20presentation.pdf">downloaded here</a>. <a target="_blank" rel="nofollow" href="https://www.youtube.com/watch?v=e4bdKcFl4GA">Here</a> is the recorded presentation video:<br />
<center><object id="vvq-17637-youtube-1" style="visibility: visible; display: block !important;" width="425" height="344" data="https://www.youtube.com/v/e4bdKcFl4GA&amp;rel=0&amp;fs=1&amp;showsearch=0&amp;showinfo=0" type="application/x-shockwave-flash"><param name="wmode" value="opaque" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /></object></center><br />
The demo system&#8217;s user interface showcases the hidden visualization and interactivity potential of Matlab for tracking order executions and charting financial time-series in real time. The undocumented features used in the demo include:</p>
<ul>
<li><a target="_blank" href="/articles/displaying-animated-gifs/">Animated GIF logo image</a> (rotating Dollar sign)</li>
<li><a target="_blank" href="/articles/uitable-customization-report/">Customized data table</a></li>
<li><a target="_blank" href="/articles/rich-matlab-editbox-contents/">Log box with rich HTML contents</a>: icons and color-coded messages (as shown above)</li>
<li><a target="_blank" href="/articles/uisplitpane/">Draggable (resizable) panel borders</a> (note that this feature does not work in the new <a target="_blank" href="/articles/hg2-update/">HG2</a> &#8211; I hope it will be fixed by the time that HG2 is released)</li>
<li><a target="_blank" href="/articles/minimize-maximize-figure-window/">Maximized figure window</a></li>
</ul>
<p>So, even if you&#8217;re not interested in real-time financial trading with Matlab, you might find it interesting to see the neat things that Matlab can do using a Java API interface and a few undocumented GUI tricks (such as the rich-contents log that I explained above).<br />
The demo source code is provided <a target="_blank" href="/files/Matlab%20trading%20demo.zip">here</a> (tradingDemo.m and supporting files). Note that this is provided as-is, free of charge but without any warranty or support. You would naturally need <a target="_blank" href="/ib-matlab/">IB-Matlab</a> and an Interactive Brokers account to run it. But you can reuse parts of the source code in your Matlab programs even without an IB account or IB-Matlab.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/rich-contents-log-panel">Rich-contents log panel</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/rich-matlab-editbox-contents" rel="bookmark" title="Rich Matlab editbox contents">Rich Matlab editbox contents </a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/aligning-uicontrol-contents" rel="bookmark" title="Aligning uicontrol contents">Aligning uicontrol contents </a> <small>Matlab uicontrols can often be customized using plain HTML/CSS, without need for advanced Java. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/gui-integrated-html-panel" rel="bookmark" title="GUI integrated HTML panel">GUI integrated HTML panel </a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</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/rich-contents-log-panel/feed</wfw:commentRss>
			<slash:comments>15</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 loading="lazy" 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 loading="lazy" 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>
	</channel>
</rss>
