<?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>Undocumented Matlab &#187; HTML</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/html/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 02 Feb 2012 00:24:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Multi-line tooltips</title><link>http://undocumentedmatlab.com/blog/multi-line-tooltips/</link> <comments>http://undocumentedmatlab.com/blog/multi-line-tooltips/#comments</comments> <pubDate>Thu, 03 Nov 2011 02:05:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></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[Pure Matlab]]></category> <category><![CDATA[Tooltip]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2518</guid> <description><![CDATA[Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I often use tooltips in my Matlab GUIs. They are a fantastically intuitive and unobtrusive visual helper for users to understand the GUI. In this blog, I have already posted several articles about how to tweak tooltip contents (<a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">here</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">here</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/">here</a>). Today I would like to expand on one of the aspects that were already covered, namely that of multi-line tooltips.</p><h3 id="Basic">Basic multi-line tooltips</h3><p>The basic multi-line tooltip consists of a simple string that includes the newline character. for example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'this is line 1'</span> <span style="color: #33f;">10</span> <span style="color:#A020F0;">'and this is line 2'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p>Or better (platform independent):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'this is line 1\nand this is line 2'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="A simple multi-line tooltip string" src="http://UndocumentedMatlab.com/images/tooltip_multi-line1.png" title="A simple multi-line tooltip string" width="120" /><p
class="wp-caption-text">A simple multi-line tooltip string</p></div></center></p><p>As can be seen, this is very simple to set up. Unfortunately, the font cannot be customized. Which leads us to:</p><h3 id="HTML">HTML-rendered multi-line tooltips</h3><p>Tooltips, like most other Matlab controls, <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">supports HTML rendering</a>. This is a natural corollary of the fact that Java Swing, on which all Matlab controls are based, <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/html.html">automatically supports HTML</a>. All we need to do is to add &#8216;<code>&lt;HTML&gt;</code>&#8216; at the very beginning of our tooltip string, and then we can embed HTML tags within the rest of the string:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'tooltipString'</span>, <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;line #1&lt;/b&gt;&lt;br /&gt;&lt;i&gt;&lt;font color=&quot;red&quot;&gt;line#2&lt;/font&gt;&lt;/i&gt;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Multi-line HTML'ed tooltip" src="http://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/html2.png" title="Multi-line HTML'ed tooltip" width="54" /><p
class="wp-caption-text">Multi-line HTML'ed tooltip</p></div></center></p><p>And a more sophisticated example, used within my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">createTable utility</a> on the File Exchange:</p><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="A more complex multi-line HTML-based tooltip" src="http://UndocumentedMatlab.com/images/table.png" title="A more complex multi-line HTML-based tooltip" width="597" /><p
class="wp-caption-text">A more complex multi-line HTML-based tooltip</p></div></center></p><p>Note that there is no need to close the final HTML tags in the tooltip string, although it&#8217;s not an error to do so (as I have done above).</p><h3 id="Problem">The problem with formatted multi-line tooltip</h3><p>Unfortunately, none of these two methods work when we wish to display formatted multi-line tooltips. For example, suppose that we wish to display struct data in a tooltip:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myData = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>,<span style="color: #0000FF;">pi</span>, <span style="color:#A020F0;">'b'</span>,-<span style="color: #33f;">4</span>, <span style="color:#A020F0;">'very_long_field_name'</span>,<span style="color:#A020F0;">'short txt'</span><span style="color: #080;">&#41;</span>
myData = 
                       a<span style="color: #F0F;">:</span> <span style="color: #33f;">3.14159265358979</span>
                       b<span style="color: #F0F;">:</span> -<span style="color: #33f;">4</span>
    very_long_field_name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'short txt'</span>
&nbsp;
&gt;&gt; myDataStr = <span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp(myData)'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, myDataStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line2.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>If we try to use HTML, the result looks even worse, because if the HTML-renderer detects a newline character embedded in the string, it simply uses the regular (non-HTML) renderer:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Failure to parse a string using HTML" src="http://UndocumentedMatlab.com/images/tooltip_multi-line3.png" title="Failure to parse a string using HTML" width="200" /><p
class="wp-caption-text">Failure to parse a string using HTML</p></div></center></p><p>Even if we fix this by replacing all line-separators with &#8216;<code>&lt;br/&gt;</code>&#8216;, we still get a badly-formatted tooltip, because HTML ignores all white spaces:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myDataStr2 = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>myDataStr, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\n'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'&lt;br /&gt;'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line4.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>(you can see that it&#8217;s HTML-formatted by the fact that the tooltip contents do not have internal margins like in the non-HTML tooltip above)</p><h3 id="Fixed-width">Fixed-width font multi-line tooltips</h3><p>We now recall the HTML <code>&lt;pre&gt;</code> tag, which tells HTML not to ignore white-spaces. In most web browsers, it also defaults to using a fixed-width font. Unfortunately, this is not the case here:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;'</span> myDataStr2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 250px"><img
alt="Still not quite right..." src="http://UndocumentedMatlab.com/images/tooltip_multi-line5.png" title="Still not quite right..." width="240" /><p
class="wp-caption-text">Still not quite right...</p></div></center></p><p>Which brings us to the final customization of the day, namely explicitly forcing the tooltip to use a fixed-width font:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;&lt;font face=&quot;courier new&quot;&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 330px"><img
alt="Finally - a well-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line6.png" title="Finally - a well-formatted multi-line tooltip" width="320" /><p
class="wp-caption-text">Finally - a well-formatted multi-line tooltip</p></div></center></p><p>Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.</html></pre><p></html></pre><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/multi-line-tooltips/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Uitable cell colors</title><link>http://undocumentedmatlab.com/blog/uitable-cell-colors/</link> <comments>http://undocumentedmatlab.com/blog/uitable-cell-colors/#comments</comments> <pubDate>Thu, 27 Oct 2011 01:51:53 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2476</guid> <description><![CDATA[A few Java-based customizations can transform a plain-looking data table into a lively colored one.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A client recently asked me to develop an application that will help discover technical indicators for trading on the stock market. The application was very data-intensive and the analysis naturally required visual presentations of number-crunching results in a readable manner. One of the requirements was to present numeric results in a data table, so we naturally use <i><b>uitable</b></i> for this.</p><p>Today I will show how using some not-so-complex Java we can transform the standard Matlab <i><b>uitable</b></i> into something much more useful.</p><h3 id="basic">First pass &#8211; basic data table</h3><p>We start by displaying the data in a simple <i><b>uitable</b></i>. The essence of the relevant code snippet was something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">headers = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Periods'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Any period&lt;br /&gt;returns&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg return&lt;br /&gt;signal&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Gain/draw&lt;br /&gt;ratio&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Random&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Signal&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Payout'</span>, <span style="color:#A020F0;">'% p-value'</span><span style="color: #080;">&#125;</span>;
hTable = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Data'</span>,data, <span style="color:#A020F0;">'ColumnEditable'</span>,<span style="color: #0000FF;">false</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnName'</span>,headers, <span style="color:#A020F0;">'RowName'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'numeric'</span>,<span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'bank'</span><span style="color: #080;">&#125;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnWidth'</span>,<span style="color:#A020F0;">'auto'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'Position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,.75,<span style="color: #33f;">1</span>,.25<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Plain data table - so boooooring..." src="http://UndocumentedMatlab.com/images/uitable_plain1.png" title="Plain data table - so boooooring..." width="596" /><p
class="wp-caption-text">Plain data table - so boooooring...</p></div></center></p><p>We can see from this simple example how I have used HTML to format the header labels into two rows, to enable compact columns. I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">described</a> using HTML formatting in Matlab controls in several articles on this website. I have not done this here, but you can easily use HTML formatting for other effect such as superscript (&lt;sup&gt;), subscript (&lt;sub&gt;), bold (&lt;b&gt;), italic (&lt;i&gt;), font sizes and colors (&lt;font&gt;) and other standard HTML effects.</p><p>Even with the multi-line headers, the default column width appears to be too wide. This is apparently an internal Matlab bug, not taking HTML into consideration. This causes only part of the information to be displayed on screen, and requires the user to either scroll right and left, or to manually resize the columns.</p><h3 id="auto-resizing">Second pass &#8211; auto-resizing that actually works&#8230;</h3><p>To solve the auto-resizing issue, we resort to a bit of Java magic powder. We start by using the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a> to get the table&#8217;s underlying Java reference handle. This is the containing scrollpane, and we are interested in the actual data table inside. We then use the <i>setAutoResizeMode</i>, as <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setAutoResizeMode%28int%29">described</a> in the official Java documentation.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jScroll = findjobj<span style="color: #080;">&#40;</span>hTable<span style="color: #080;">&#41;</span>;
jTable = jScroll.<span style="">getViewport</span>.<span style="">getView</span>;
jTable.<span style="">setAutoResizeMode</span><span style="color: #080;">&#40;</span>jTable.<span style="">AUTO_RESIZE_SUBSEQUENT_COLUMNS</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Auto-resized columns that actually work" src="http://UndocumentedMatlab.com/images/uitable_plain2.png" title="Auto-resized columns that actually work" width="596" /><p
class="wp-caption-text">Auto-resized columns that actually work</p></div></center></p><h3 id="colors">Third pass &#8211; adding colors</h3><p>There are still quite a few other customizations needed here: enable <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">sorting</a>; remove the border outline; set a white background; set row (rather than cell) selection and several other fixes that may seem trivial by themselves but together giver a much more stylish look to the table&#8217;s look and feel. I&#8217;ll skip these for now (interested readers can read all about them, and more, in my detailed <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-customization-report/"><i><b>uitable</b></i> customization report</a>).</p><p>One of the more important customizations is to add colors depending on the data. In my client&#8217;s case, there were three requirements:</p><ul><li>data that could be positive or negative should be colored in <b><font
color="green">green</font></b> or <b><font
color="red">red</font></b> foreground color respectively</li><li>large payouts (abs &gt; 2) should be colored in <b><font
color="blue">blue</font></b></li><li>data rows that have statistical significance (%p-value &lt; 5) should have a <span
style="background-color:#ffff00;">yellow background highlight</span></li></ul><p>While we could easily use HTML or CSS formatting to do this, this would be bad for performance in a large data table, may cause some issues when editing table cells or sorting columns. I chose to use the alternative method of <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer">cell renderers</a>.</p><p><code>ColoredFieldCellRenderer</code> is a simple table cell renderer that enables setting cell-specific foreground/background colors and tooltip messages (it also has a few other goodies like smart text alignment etc.). This requires some Java knowledge to program, but in this case you can simply <a
href="http://UndocumentedMatlab.com/files/ColoredFieldCellRenderer.zip">download</a> the ColoredFieldCellRenderer.zip file and use it, even if you don&#8217;t know Java. The source code is included in the zip file, for anyone who is interested.</p><p>After using <i><b>javaaddpath</b></i> to add the zip file to the dynamic Java classpath (you can add it to the static <i>classpath.txt</i> file instead), the contained Java class file is available for use in Matlab. We configure it according to our data and then assign it to all our table&#8217;s columns.</p><p>Java savvy readers might complain that the data-processing should perhaps be done in the renderer class rather than in Matlab. I have kept it in Matlab because this would enable very easy modification of the highlighting algorithm, without any need to modify the generic Java renderer class.</p><p>Unfortunately, in the new <i><b>uitable</b></i> design (the version available since R2008a), JIDE and Matlab have apparently broken the <a
target="_blank" rel="nofollow" href="http://java.sun.com/products/jfc/tsc/articles/architecture">standard MVC approach</a> by using a table model that not only controls the data but also sets the table&#8217;s appearance (row-striping background colors, for example), and disregards column cell-renderers. In order for our custom cell renderer to have any effect, we must therefore replace Matlab&#8217;s standard <code>DefaultUIStyleTableModel</code> with a simple <code>DefaultTableModel</code>. This in itself is not enough – we also need to ensure that the <i><b>uitable</b></i> has an empty <strong>ColumnFormat</strong> property, because if it is non-empty then it overrides our cell-renderer.</p><p>In the following code, note that Java row and column indices start at 0, not at 1 like in Matlab. We need to be careful about indices when programming java in Matlab.</p><p>The rest of the code should be pretty much self explanatory (again &#8211; more details can be found in the above-mentioned report):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Initialize our custom cell renderer class object</span>
javaaddpath<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ColoredFieldCellRenderer.zip'</span><span style="color: #080;">&#41;</span>;
cr = ColoredFieldCellRenderer<span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span>.<span style="">white</span><span style="color: #080;">&#41;</span>;
cr.<span style="">setDisabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% to bg-color the entire column</span>
&nbsp;
<span style="color: #228B22;">% Set specific cell colors (background and/or foreground)</span>
<span style="color: #0000FF;">for</span> rowIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">size</span><span style="color: #080;">&#40;</span>data,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Red/greed foreground color for the numeric data</span>
  <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">2</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
    <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &lt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% red</span>
    <span style="color: #0000FF;">elseif</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &gt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0.5</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% green</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Yellow background for significant rows based on p-value</span>
  <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">12</span><span style="color: #080;">&#41;</span> &lt; = <span style="color: #33f;">5</span> <span style="color: #F0F;">&amp;&amp;</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>~=<span style="color: #33f;">0</span>
    <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
      cr.<span style="">setCellBgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% yellow</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Bold blue foreground for significant payouts</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">abs</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt;= <span style="color: #33f;">2</span>
    cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,<span style="color: #33f;">10</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% blue</span>
&nbsp;
    <span style="color: #228B22;">% Note: the following could easily be done in the renderer, just like the colors</span>
    boldPayoutStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;'</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'%.2f'</span><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">'&lt;/b&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span>;
    <span style="color: #228B22;">%jTable.setValueAt(boldPayoutStr,rowIdx-1,10);  % this is no good: overridden when table model is replaced below...</span>
    dataCells<span style="color: #080;">&#123;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#125;</span> = boldPayoutStr;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Replace Matlab's table model with something more renderer-friendly...</span>
jTable.<span style="">setModel</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">table</span>.<span style="">DefaultTableModel</span><span style="color: #080;">&#40;</span>dataCells,headers<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTable,<span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Finally assign the renderer object to all the table columns</span>
<span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
  jTable.<span style="">getColumnModel</span>.<span style="">getColumn</span><span style="color: #080;">&#40;</span>colIdx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">setCellRenderer</span><span style="color: #080;">&#40;</span>cr<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 600px"><img
alt="Finally something lively!" src="http://UndocumentedMatlab.com/images/uitable_colored.png" title="Finally something lively!" width="590" /><p
class="wp-caption-text">Finally something lively!</p></div></center></p><p>This may not take a beauty contest prize, but you must admit that it now looks more useful than the original table at the top of this article.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-cell-colors/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Uitable customization report</title><link>http://undocumentedmatlab.com/blog/uitable-customization-report/</link> <comments>http://undocumentedmatlab.com/blog/uitable-customization-report/#comments</comments> <pubDate>Wed, 03 Aug 2011 18:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitable report]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2396</guid> <description><![CDATA[In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and [...]<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In last week&#8217;s report about <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/"><i><b>uitable</b></i> sorting</a>, I offered a report that I have written which covers <i><b>uitable</b></i> customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and so in today&#8217;s post I will discuss in a bit more detail the highlights of what can be achieved to customize Matlab <i><b>uitable</b></i>s. For the fine details, well, <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=ymasoftware@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">get the report</a>&#8230;</p><h3 id="Introduction">1. Introduction</h3><p>Here I discuss the evolution of the <i><b>uitable</b></i> control over the past decade, from its initially semi-documented status to today. I explain the similarities and differences between the control&#8217;s versions and explain how they can both be accessed today.</p><p>I also provide references to online resources for both Matlab&#8217;s <i><b>uitable</b></i>&#8216;s underlying Java components, as well as multiple alternatives using different technologies, that have been used and reported over the years.</p><h3 id="Customization">2. Customizing uitable</h3><p>In this section I explore the sub-component hierarchy of the <i><b>uitable</b></i> control (both new and old). I show how the scrollbar sub-components can be accessed (this will be used in section 4 below), as well as the table header and data grid.<br
/><center><div
class="wp-caption aligncenter" style="width: 322px"><img
alt="annotated uitable sub-components" src="http://undocumentedmatlab.com/images/uitable_new2b.png" title="annotated uitable sub-components" width="312" /><p
class="wp-caption-text">annotated <i><b>uitable</b></i> sub-components</p></div></center></p><p>I explain how individual cells can be modified without requiring the entire data set to be updated. This is very important in large data sets, to prevent flicker and improve performance.</p><p>I show how HTML can be used to format data cell contents (even images) and tooltips:<br
/><center><div
class="wp-caption aligncenter" style="width: 259px"><img
alt="uitable with HTML cell contents and tooltip" src="http://undocumentedmatlab.com/images/uitable2.png" title="uitable with HTML cell contents and tooltip" width="249" /><p
class="wp-caption-text"><i><b>uitable</b></i> with HTML cell contents and tooltip</p></div></center></p><p>I then explain the role of the cell-renderer in the visual appearance of the cell, and of cell-editors in the way that cells interact with the user for data modification. I explain such customizations from the simple (setting a column&#8217;s background color) to the complex (cell-specific tooltips and colors; color-selection cell-editor):<br
/><center><div
class="wp-caption aligncenter" style="width: 590px"><img
alt="uitable with a non-standard cell-renderer" src="http://undocumentedmatlab.com/images/uitable5b.png" title="uitable with a non-standard cell-renderer" width="288" /><img
alt="uitable with a non-standard cell-editor" src="http://undocumentedmatlab.com/images/uitable_lookup.png" title="uitable with a non-standard cell-editor" width="280" /><p
class="wp-caption-text"><i><b>uitable</b></i> with a non-standard cell-renderer (left) and cell-editor (right)</p></div><br
/><div
class="wp-caption aligncenter" style="width: 318px"><img
alt="uitable with custom CellRenderer and CellEditor" src="http://UndocumentedMatlab.com/images/ColorCellEditor_Renderer.png" title="uitable with custom CellRenderer and CellEditor" width="308" /><p
class="wp-caption-text"><i><b>uitable</b></i> with custom CellRenderer and CellEditor</p></div></center></p><h3 id="Callbacks">3. Table callbacks</h3><p>This section presents the different callback properties that are settable in the old and new <i><b>uitable</b></i>, for events such as cell selection, data modification, key press, and mouse click. The discussion includes a working code example for validating user input and reverting invalid edits. The section also includes a discussion of how to avoid and overcome problems that may occur with the callback execution.</p><h3 id="Scrollbars">4. Customizing scrollbars, column widths and selection behavior</h3><p>This section explains how to control the scrollbars behavior. For example, hiding the horizontal (bottom) scrollbar, automatically displaying it when the data width is larger than the table width. I also show how to control the column widths.</p><p>I then show how to customize the data selection policy: Can multiple cells be selected? perhaps only one cell at a time? or maybe a single large interval of cells? &#8211; this is all customizable. I then explain how the selection can be done and accessed programmatically. Ever wanted to set the cursor on cell A1 after some system event has occurred? &#8211; this will show you how to do it. Ever wanted to use non-standard selection colors (background/foreground)? &#8211; this can also be done.</p><h3 id="Sorting">5. Data sorting</h3><p>Table sorting was discussed in last week&#8217;s <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">article</a>. This section expands on that article, and explains how the sorting can be customized, controlled, and accessed programmatically, and how sorted rows can be retrieved by the user.<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Multi-column sorting with blue sort-order numbers" src="http://UndocumentedMatlab.com/images/JIDE_Table_sort3a.png" title="Multi-column sorting with blue sort-order numbers" width="260" /><p
class="wp-caption-text">Multi-column sorting with blue sort-order numbers</p></div></center></p><h3 id="Filtering">6. Data filtering</h3><p>Data filtering is the ability to filter only a specified sub-set of rows for display (just like in Excel). This section explains how to do it (it&#8217;s so easy!).<br
/><center><div
class="wp-caption aligncenter" style="width: 418px"><img
alt="uitable data filtering" src="http://UndocumentedMatlab.com/images/004x013b.png" title="uitable data filtering" width="408" /><p
class="wp-caption-text"><i><b>uitable</b></i> data filtering</p></div></center></p><h3 id="JIDE">7. JIDE customizations</h3><p>The new <i><b>uitable</b></i> is based on an underlying JIDE table. This sectino explains how we can use this to our advantage for some simple and useful. Customization.</p><p>For example: have you wondered some time why is it that columns can only be resized by dragging the tiny divider in the table header? Why can&#8217;t the columns and rows be resized by dragging the grid lines? Well, it turns out that they can, with just a tiny bit of JIDE magic powder, explained here:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Resizing columns" src="http://UndocumentedMatlab.com/images/004x014.png" title="Resizing columns" width="260" /><p
class="wp-caption-text">Resizing columns</p></div></center></p><p>Similarly, this section explains how we can use JIDE to merge together adjacent cells:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Example of two table cell-spans (1x2 and 2x2)" src="http://UndocumentedMatlab.com/images/004x016.png" title="Example of two table cell-spans (1x2 and 2x2)" width="260" /><p
class="wp-caption-text">Example of two table cell-spans (1x2 and 2x2)</p></div></center></p><h3 id="Structure">8. Controlling the table structure (adding/removing rows)</h3><p>This section discusses the matter of dynamically adding and removing table rows. While this is easy to do in the old <i><b>uitable</b></i>, this is unfortunately not the case in the new <i><b>uitable</b></i>.</p><h3 id="Final">9. Final remarks</h3><p>Here I present a workaround for a long-time table bug. Also, I present my <i><b>createTable</b></i> utility that wraps table creation in Matlab:<br
/><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" src="http://UndocumentedMatlab.com/images/table.png" title="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" width="597" /><p
class="wp-caption-text"><i><b>createTable</b></i> utility screenshot (note the action buttons, sortable columns, and customized CellEditor)</p></div></center></p><h3 id="JIDE-Grids">Appendix – JIDE Grids</h3><p>Finally, this appendix presents an overview of the wide array of components provided by JIDE and available in Matlab. <i><b>uitable</b></i> uses only one of these components (the <code>SortableTable</code>). In fact, there are many more such controls that we can use in our GUIs.</p><p>These include a wide selection of combo-box (drop-down) controls &#8211; calculator, file/folder selection, date selection, color selection, multi-elements selection etc. In addition, a very wide selection of lists, trees and table types is available.</p><p>Also included is a set of specialized editbox controls for accepting IP addresses and credit card numbers:<br
/><center><div
class="wp-caption aligncenter" style="width: 360px"><img
alt="IP address entry box" src="http://UndocumentedMatlab.com/images/JIDE_IPTextField.png" title="IP address entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_invalid.png" title="credit-card entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_Visa.png" title="credit-card entry box" width="100" /><p
class="wp-caption-text">IP address and credit-card entry boxes</p></div></center></p><p>While not explaining all these controls in detail (this could take hundreds of pages), this section does say a few words on each of them, and includes links to online resources for further exploration.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-customization-report/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Uitable sorting</title><link>http://undocumentedmatlab.com/blog/uitable-sorting/</link> <comments>http://undocumentedmatlab.com/blog/uitable-sorting/#comments</comments> <pubDate>Tue, 26 Jul 2011 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2391</guid> <description><![CDATA[Matlab's uitables can be sortable using simple undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/uitable.html"><i><b>uitable</b></i></a> is probably the most complex basic GUI controls available in Matlab. It displays data in a table within a figure, with settable properties as with any other Matlab Handle-Graphics (HG) control. After many years in which the <i><b>uitable</b></i> was available but <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> and not officially supported in Matlab, it finally became fully documented and supported in R2008a (aka Matlab 7.6). At that time its internal implementation has changed from a MathWorks-developed Java table to a <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a>-based Java table (another JIDE-derived table was described <a
target="_blank" href="http://undocumentedmatlab.com/blog/jide-property-grids/">here</a> last year). Since R2008a, both versions of <i><b>uitable</b></i> are available &#8211; the old version is available by adding the &#8216;v0&#8242; input arg.</p><p>Matlab&#8217;s <i><b>uitable</b></i> exposes only a very limited subset of functionalities and properties to the user. Numerous other functionalities are available by accessing the underlying Java table and hidden Matlab properties. Today I will describe a very <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/1880-uitable-can-the-headers-be-made-clickable">common need</a> in GUI tables, that for some unknown reason is missing in Matlab&#8217;s <i><b>uitable</b></i>: Sorting table data columns.</p><p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/">explained</a> how we can modify table headers of an ActiveX table control to display sorting icons. In that case, sorting was built-in the control, and the question was just how to display the sorting arrow icon. Unfortunately, Matlab&#8217;s <i><b>uitable</b></i> does not have sorting built-in, although it&#8217;s quite easy to add it, as I shall now show.</p><h3 id="Old">Old uitable sorting</h3><p>The old <i><b>uitable</b></i> is the default control used until R2007b, or that can be selected with the &#8216;v0&#8242; input arg since R2008a. It was based on an internal MathWorks extension of the standard Java Swing <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> &#8211; a class called <code>com.mathworks.widgets.spreadsheet.SpreadsheetTable</code>.</p><p>Users will normally try to sort columns by clicking the header. This has been a deficiency of JTable for ages. To solve this for the old (pre-R2008a) <i><b>uitable</b></i>, download one of several available JTable sorter classes, or my TableSorter class (available <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">here</a>).  Add the TableSorter.jar file to your static java classpath (via <code>edit('classpath.txt')</code>) or your dynamic classpath (<code>javaaddpath('TableSorter.jar')</code>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
<span style="color: #080;">&#91;</span>mtable,hcontainer<span style="color: #080;">&#93;</span> = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% discard the 'v0' in R2007b and earlier</span>
jtable = mtable.<span style="">getTable</span>;   <span style="color: #228B22;">% or: get(mtable,'table');</span>
&nbsp;
<span style="color: #228B22;">% We want to use sorter, not data model...</span>
<span style="color: #228B22;">% Unfortunately, UitablePeer expects DefaultTableModel not TableSorter so we need a modified UitablePeer class</span>
<span style="color: #228B22;">% But UitablePeer is a Matlab class, so use a modified TableSorter &amp; attach it to the Model</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'TableSorter'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Add TableSorter as TableModel listener</span>
   sorter = TableSorter<span style="color: #080;">&#40;</span>jtable.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   jtable.<span style="">setModel</span><span style="color: #080;">&#40;</span>sorter<span style="color: #080;">&#41;</span>;
   sorter.<span style="">setTableHeader</span><span style="color: #080;">&#40;</span>jtable.<span style="">getTableHeader</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Set the header tooltip (with sorting instructions)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to sort up; &lt;b&gt;Shift-click&lt;/b&gt; to sort down&lt;br /&gt;&amp;nbsp;...&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Set the header tooltip (no sorting instructions...)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to select entire column&lt;br /&gt;&amp;nbsp;&lt;b&gt;Ctrl-click&lt;/b&gt; (or &lt;b&gt;Shift-click&lt;/b&gt;) to select multiple columns&amp;nbsp;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="Sorted uitable - old version" src="http://www.mathworks.com/matlabcentral/fx_files/14225/1/table.png" title="Sorted uitable - old version" width="597" /><p
class="wp-caption-text">Sorted <i><b>uitable</b></i> - old version</p></div></center></p><h3 id="New">New uitable sorting</h3><p>The new <i><b>uitable</b></i> is based on JIDE&#8217;s <code>com.jidesoft.grid.SortableTable</code> and so has built-in sorting support – all you need to do is to turn it on. First get the underlying Java object using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
mtable = uitable<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Data'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'ColumnName'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
jscrollpane = findjobj<span style="color: #080;">&#40;</span>mtable<span style="color: #080;">&#41;</span>;
jtable = jscrollpane.<span style="">getViewport</span>.<span style="">getView</span>;
&nbsp;
<span style="color: #228B22;">% Now turn the JIDE sorting on</span>
jtable.<span style="">setSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;		<span style="color: #228B22;">% or: set(jtable,'Sortable','on');</span>
jtable.<span style="">setAutoResort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setMultiColumnSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setPreserveSelectionsAfterSorting</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note: the Matlab <code>mtable</code> handle has a hidden <b>Sortable</b> property, but it has no effect – use the Java property mentioned above instead. I assume that the hidden <b>Sortable</b> property was meant to implement the sorting behavior in R2008a, but MathWorks never got around to actually implement it, and so it remains this way to this day.</p><h3 id="Report">A more detailed report</h3><p>I have prepared a 30-page report about using and customizing Matlab&#8217;s <i><b>uitable</b></i>, which greatly expands on the above. This report is available for a small fee <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=ymasoftware@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">here</a> (please allow up to 48 hours for email delivery). The report includes the following:</p><ul><li>comparison of the old vs. the new <i><b>uitable</b></i> implementations</li><li>description of the <i><b>uitable</b></i> properties and callbacks</li><li>alternatives to <i><b>uitable</b></i> using a variety of technologies</li><li>updating a specific cell&#8217;s value</li><li>setting background and foreground colors for a cell or column</li><li>using dedicated cell renderer and editor components</li><li>HTML processing</li><li>setting dynamic cell-specific tooltip</li><li>setting dynamic cell-specific drop-down selection options</li><li>using a color-selection drop-down for cells</li><li>customizing scrollbars</li><li>customizing column widths and resizing</li><li>customizing selection behavior</li><li>data sorting (expansion of today&#8217;s article)</li><li>data filtering (similar to Excel&#8217;s data filtering control)</li><li>merging table cells</li><li>programmatically adding/removing rows</li><li>numerous links to online resources</li><li>overview of the JIDE grids package, which contains numerous extremely useful GUI controls and components</li></ul><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-sorting/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>Uitab colors, icons and images</title><link>http://undocumentedmatlab.com/blog/uitab-colors-icons-images/</link> <comments>http://undocumentedmatlab.com/blog/uitab-colors-icons-images/#comments</comments> <pubDate>Wed, 10 Nov 2010 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <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[Matlab's semi-documented tab panels can be customized using some undocumented hacks<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few months ago I published a post about <a
target="_blank" href="http://undocumentedmatlab.com/blog/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="http://undocumentedmatlab.com/blog/customizing-uicontrol-border/#comment-21714">asked a related question</a> earlier today, so I decided it&#8217;s about time I fulfilled this promise.</p><p>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><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Prevent an annoying warning msg</span>
<span style="color: #0000FF;">warning</span> off MATLAB<span style="color: #F0F;">:</span>uitabgroup<span style="color: #F0F;">:</span>OldVersion
&nbsp;
<span style="color: #228B22;">% Prepare a tab-group consisting of two tabs</span>
hTabGroup = uitabgroup; <span style="color: #0000FF;">drawnow</span>;
tab1 = uitab<span style="color: #080;">&#40;</span>hTabGroup, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Panel 1'</span><span style="color: #080;">&#41;</span>;
a = <span style="color: #0000FF;">axes</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>, tab1<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">surf</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">peaks</span><span style="color: #080;">&#41;</span>;
tab2 = uitab<span style="color: #080;">&#40;</span>hTabGroup, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Panel 2'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span>tab2, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Close'</span>, <span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">'close(gcbf)'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the underlying Java reference (use hidden property)</span>
jTabGroup = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hTabGroup<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'JTabbedPane'</span><span style="color: #080;">&#41;</span>;</pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Equivalent manners to set a red tab foreground:</span>
jTabGroup.<span style="">setForegroundAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1.0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% tab #1</span>
jTabGroup.<span style="">setTitleAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color:#A020F0;">'&lt;html&gt;&lt;font color=&quot;red&quot;&gt;&lt;i&gt;Panel 2'</span><span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setForeground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span>.<span style="">red</span><span style="color: #080;">&#41;</span>;</pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Equivalent manners to set a yellow tab background:</span>
jTabGroup.<span style="">setTitleAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color:#A020F0;">'&lt;html&gt;&lt;div style=&quot;background:#ffff00;&quot;&gt;Panel 1'</span><span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setTitleAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color:#A020F0;">'&lt;html&gt;&lt;div style=&quot;background:yellow;&quot;&gt;Panel 1'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 350px"><img
alt="uitabgroup with non-default forground and background tab colors and fonts" src="http://UndocumentedMatlab.com/images/uitabgroup5_bgcolor.png" title="uitabgroup with non-default forground and background tab colors and fonts" width="340" height="265" /><p
class="wp-caption-text"><i><b>uitabgroup</b></i> with non-default forground and background tab colors and fonts</p></div></center></p><p>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><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Add an icon to tab #1 (=second tab)</span>
icon = javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'C:\Yair\save.gif'</span><span style="color: #080;">&#41;</span>;
jLabel = javax.<span style="">swing</span>.<span style="">JLabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tab #2'</span><span style="color: #080;">&#41;</span>;
jLabel.<span style="">setIcon</span><span style="color: #080;">&#40;</span>icon<span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setTabComponentAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,jLabel<span style="color: #080;">&#41;</span>;	<span style="color: #228B22;">% Tab #1 = second tab</span>
&nbsp;
<span style="color: #228B22;">% Note: icon is automatically grayed when label is disabled</span>
jLabel.<span style="">setEnabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setEnabledAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% disable only tab #1</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 302px"><img
alt="tab with a custom icon (enabled)" src="http://UndocumentedMatlab.com/images/uitabgroup6_icon.png" title="tab with a custom icon (enabled)" width="292" height="159" /><br/><img
alt="tab with a custom icon (enabled)" src="http://UndocumentedMatlab.com/images/uitabgroup6_icon2.png" title="tab with a custom icon (disabled)" width="292" height="159" /><p
class="wp-caption-text">tab with a custom icon (enabled &#038; disabled)</p></div></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><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% First let's load the close icon</span>
jarFile = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'/java/jar/mwt.jar'</span><span style="color: #080;">&#41;</span>;
iconsFolder = <span style="color:#A020F0;">'/com/mathworks/mwt/resources/'</span>;
iconURI = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'jar:file:/'</span> jarFile <span style="color:#A020F0;">'!'</span> iconsFolder <span style="color:#A020F0;">'closebox.gif'</span><span style="color: #080;">&#93;</span>;
icon = javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span>java.<span style="">net</span>.<span style="">URL</span><span style="color: #080;">&#40;</span>iconURI<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Now let's prepare the close button: icon, size and callback</span>
jCloseButton = handle<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span>,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
jCloseButton.<span style="">setIcon</span><span style="color: #080;">&#40;</span>icon<span style="color: #080;">&#41;</span>;
jCloseButton.<span style="">setPreferredSize</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#40;</span><span style="color: #33f;">15</span>,<span style="color: #33f;">15</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCloseButton.<span style="">setMaximumSize</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#40;</span><span style="color: #33f;">15</span>,<span style="color: #33f;">15</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCloseButton.<span style="">setSize</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#40;</span><span style="color: #33f;">15</span>,<span style="color: #33f;">15</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jCloseButton, <span style="color:#A020F0;">'ActionPerformedCallback'</span>,@<span style="color: #080;">&#40;</span>h,e<span style="color: #080;">&#41;</span><span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>tab2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Now let's prepare a tab panel with our label and close button</span>
jPanel = javax.<span style="">swing</span>.<span style="">JPanel</span>;	<span style="color: #228B22;">% default layout = FlowLayout</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jPanel.<span style="">getLayout</span>, <span style="color:#A020F0;">'Hgap'</span>,<span style="color: #33f;">0</span>, <span style="color:#A020F0;">'Vgap'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% default gap = 5px</span>
jLabel = javax.<span style="">swing</span>.<span style="">JLabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tab #2'</span><span style="color: #080;">&#41;</span>;
jPanel.<span style="">add</span><span style="color: #080;">&#40;</span>jLabel<span style="color: #080;">&#41;</span>;
jPanel.<span style="">add</span><span style="color: #080;">&#40;</span>jCloseButton<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Now attach this tab panel as the tab-group's 2nd component</span>
jTabGroup.<span style="">setTabComponentAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,jPanel<span style="color: #080;">&#41;</span>;	<span style="color: #228B22;">% Tab #1 = second tab</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 369px"><img
alt="tab with an attached close button" src="http://UndocumentedMatlab.com/images/uitabgroup8_close_button.png" title="tab with an attached close button" width="359" height="185" /><p
class="wp-caption-text">tab with an attached close button</p></div></center></p><p>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="http://undocumentedmatlab.com/blog/uitab-colors-icons-images/#respond">comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitab-colors-icons-images/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Customizing uitree nodes &#8211; part 1</title><link>http://undocumentedmatlab.com/blog/customizing-uitree-nodes/</link> <comments>http://undocumentedmatlab.com/blog/customizing-uitree-nodes/#comments</comments> <pubDate>Wed, 25 Aug 2010 18:00:20 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Icons]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1835</guid> <description><![CDATA[This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/interesting-uitree-utility/' rel='bookmark' title='An interesting uitree utility'>An interesting uitree utility</a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In my previous posts, I introduced the semi-documented <a
target="_blank" href="http://UndocumentedMatlab.com/blog/uitree/"><i><b>uitree</b></i> function</a> that enables displaying data in a hierarchical (tree) control in Matlab GUI, and showed how it can be <a
target="_blank" href="http://UndocumentedMatlab.com/blog/customizing-uitree/">customized</a>. Today, I will continue by describing how specific <i><b>uitree</b></i> nodes can be customized.</p><p>To start the discussion, let&#8217;s re-create last week&#8217;s simple <i><b>uitree</b></i>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Fruits</span>
fruits = uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Fruits'</span>, <span style="color:#A020F0;">'Fruits'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
fruits.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Apple'</span>,  <span style="color:#A020F0;">'Apple'</span>,  <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
fruits.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Pear'</span>,   <span style="color:#A020F0;">'Pear'</span>,   <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
fruits.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Banana'</span>, <span style="color:#A020F0;">'Banana'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
fruits.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Orange'</span>, <span style="color:#A020F0;">'Orange'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Vegetables</span>
veggies = uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Veggies'</span>, <span style="color:#A020F0;">'Vegetables'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
veggies.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Potato'</span>, <span style="color:#A020F0;">'Potato'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
veggies.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Tomato'</span>, <span style="color:#A020F0;">'Tomato'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
veggies.<span style="">add</span><span style="color: #080;">&#40;</span>uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Carrot'</span>, <span style="color:#A020F0;">'Carrot'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Root node</span>
root = uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Food'</span>, <span style="color:#A020F0;">'Food'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
root.<span style="">add</span><span style="color: #080;">&#40;</span>veggies<span style="color: #080;">&#41;</span>;
root.<span style="">add</span><span style="color: #080;">&#40;</span>fruits<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Tree</span>
<span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">300</span>,<span style="color: #33f;">300</span>,<span style="color: #33f;">150</span>,<span style="color: #33f;">150</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
mtree = uitree<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'Root'</span>, root<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 360px"><img
alt="User-created tree" src="http://UndocumentedMatlab.com/images/uitree7a.png" title="User-created tree" width="158" height="232" /> &nbsp;&nbsp; <img
alt="User-created tree" src="http://UndocumentedMatlab.com/images/uitree7b.png" title="User-created tree" width="158" height="232" /><p
class="wp-caption-text">User-created tree</p></div></center></p><h3 id="labels">Labels</h3><p>Node labels (descriptions) can be set using their <strong>Name</strong> property (the second <i><b>uitreenode</b></i> data argument). Note that the horizontal space allotted for displaying the node name will not change until the node is collapsed or expanded. So, if the new name requires more than the existing space, it will be displayed as something like &#8220;abc…&#8221;, until the node is expanded or collapsed.</p><p>Node names share the same <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">HTML support feature</a> as all Java Swing labels. Therefore, we can specify font size/face/color, bold, italic, underline, super-/sub-script etc.:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">txt1 = <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;&lt;u&gt;&lt;i&gt;abra&lt;/i&gt;&lt;/u&gt;'</span>;
txt2 = <span style="color:#A020F0;">'&lt;font color=&quot;red&quot;&gt;&lt;sup&gt;kadabra&lt;/html&gt;'</span>;
node.<span style="">setName</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>txt1,txt2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 160px"><img
alt="HTML-enriched tree nodes" src="http://UndocumentedMatlab.com/images/uitree2.jpg" title="HTML-enriched tree nodes" width="116" height="50" /><p
class="wp-caption-text">HTML-enriched tree nodes</p></div></center></p><h3 id="icons">Icons</h3><p>Tree-node icons can be specified during node creation, as the third data argument to <i><b>uitreenode</b></i>, which accepts an icon-path (a string):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">iconPath = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'/toolbox/matlab/icons/greenarrowicon.gif'</span><span style="color: #080;">&#41;</span>;
node = uitreenode<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>,value,name,iconPath,isLeaf<span style="color: #080;">&#41;</span>;</pre></div></div><p>Tree node icons can also be created or modified programmatically in run-time, using Matlab&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/im2java.html">im2java</a> function. Icons can also be loaded from existing files as follows (real-life programs should check and possibly update jImage&#8217;s size to 16 pixels, before setting the node icon, otherwise the icon might get badly cropped; also note the tree-refreshing action):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jImage = java.<span style="">awt</span>.<span style="">Toolkit</span>.<span style="">getDefaultToolkit</span>.<span style="">createImage</span><span style="color: #080;">&#40;</span>iconPath<span style="color: #080;">&#41;</span>;
veggies.<span style="">setIcon</span><span style="color: #080;">&#40;</span>jImage<span style="color: #080;">&#41;</span>;
veggies.<span style="">setIcon</span><span style="color: #080;">&#40;</span>im2java<span style="color: #080;">&#40;</span><span style="color: #0000FF;">imread</span><span style="color: #080;">&#40;</span>iconPath<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% an alternative</span>
&nbsp;
<span style="color: #228B22;">% refresh the veggies node (and all its children)</span>
mtree.<span style="">reloadNode</span><span style="color: #080;">&#40;</span>veggies<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 168px"><img
alt="Setting node icon" src="http://UndocumentedMatlab.com/images/uitree7c.png" title="Setting node icon" width="158" height="232" /><p
class="wp-caption-text">Setting node icon</p></div></center></p><h3 id="behavior">Behavior</h3><p>Nodes can be modified from leaf (non-expandable) to parent behavior (=expandable) by setting their <strong>LeafNode</strong> property (a related property is <strong>AllowsChildren</strong>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>node,<span style="color:#A020F0;">'LeafNode'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% =expandable</span>
node.<span style="">setLeafNode</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% an alternative</span></pre></div></div><p>One of the questions I was <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/#comment-388">asked</a> was how to &#8220;disable&#8221; a specific tree node. One way would be to modify the tree&#8217;s <strong>ExpandFcn</strong> callback. Another way is to use a combination of HTML rendering and the node&#8217;s <strong>AllowsChildren</strong> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">label = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>veggies.<span style="">getName</span><span style="color: #080;">&#41;</span>;
veggies.<span style="">setName</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;font color=&quot;gray&quot;&gt;'</span> label<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
veggies.<span style="">setAllowsChildren</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
t.<span style="">reloadNode</span><span style="color: #080;">&#40;</span>veggies<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 168px"><img
alt="Disabled node" src="http://UndocumentedMatlab.com/images/uitree7d.png" title="Disabled node" width="158" height="232" /><p
class="wp-caption-text">Disabled node</p></div></center></p><p>Another possible behavioral customization is <a
target="_blank" href="http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/">adding a context-menu to a uitree</a>. We can set node-specific tooltips using similar means.</p><p>Answering a reader&#8217;s <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-uitree/#comment-15781">request</a> from last week, tree nodes icons can be used to present checkboxes, radio buttons and other similar node-specific controls. This can actually be done in several ways, that will be explored in next week&#8217;s article.</p><p>There are numerous other possible customizations &#8211; if readers are interested, perhaps I will describe some of them in future articles. If you have any special request, please post a comment <a
href="http://undocumentedmatlab.com/blog/customizing-uitree-nodes/#respond">below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/interesting-uitree-utility/' rel='bookmark' title='An interesting uitree utility'>An interesting uitree utility</a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-uitree-nodes/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Syntax highlighted labels &amp; panels</title><link>http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/</link> <comments>http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/#comments</comments> <pubDate>Wed, 14 Jul 2010 16:02:26 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1738</guid> <description><![CDATA[Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few weeks ago, a reader of my article about <a
target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#comment-10760">rich Matlab editbox contents</a> asked whether it is possible to display syntax-highlighted contents, i.e. contents whose color changes based on its underlying text, often called <i>syntax hilite</i> in affection. I gave a very specific answer in a reply comment, which I expand in today&#8217;s full-length article.<br
/></p><p>Matlab has two built-in Java classes that can present syntax-highlighted text: <b>SyntaxTextLabel</b> presents single-line labels, while <b>SyntaxTextPane</b> presents a multi-line editor pane. Both of these classes support C, HTML/XML, Java and Matlab syntax highlighting, as well as standard plaint-text. Some related <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a> classes are also described.</p><h3 id="SyntaxTextLabel">SyntaxTextLabel</h3><p>SyntaxTextLabel is used to display a syntax-highlighted single-line text label according to the specified programming language: C_STYLE, HTML_STYLE, JAVA_STYLE, PLAIN_STYLE and of course M_STYLE for Matlab code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">str = <span style="color:#A020F0;">'for id=1:3, set(h(id),'</span><span style="color:#A020F0;">'string'</span><span style="color:#A020F0;">',num2str(id)); end  % Matlab code'</span>;
codeType = com.<span style="">mathworks</span>.<span style="">widgets</span>.<span style="">SyntaxTextLabel</span>.<span style="">M_STYLE</span>;
jCodeLabel = com.<span style="">mathworks</span>.<span style="">widgets</span>.<span style="">SyntaxTextLabel</span><span style="color: #080;">&#40;</span>str,codeType<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#91;</span>jhLabel,hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jCodeLabel,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">300</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 330px"><img
alt="SyntaxTextLabels (different code styles)" src="http://UndocumentedMatlab.com/images/SyntaxTextLabel.png" title="SyntaxTextLabels (different code styles)" width="320" height="95" /><p
class="wp-caption-text">SyntaxTextLabels (different code styles)</p></div></center></p><h3 id="StyledLabel">StyledLabel</h3><p>More flexibility in the displayed label styles can be achieved with <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">HTML/CSS</a>, and the bundled JIDE class <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabel.html">com.jidesoft.swing.StyledLabel</a> provides even more flexibility:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">import java.<span style="">awt</span>.*
import com.<span style="">jidesoft</span>.<span style="">swing</span>.*
str = <span style="color:#A020F0;">'Mixed Underlined Strikethrough Super and Subscript combo Styles'</span>;
com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJUtilities</span>.<span style="">initJIDE</span>;
jStyledLabel = StyledLabel<span style="color: #080;">&#40;</span>str<span style="color: #080;">&#41;</span>; 
styles = <span style="color: #080;">&#91;</span>StyleRange<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">5</span>,  Font.<span style="">BOLD</span>,   Color.<span style="">BLUE</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">6</span>,<span style="color: #33f;">10</span>, Font.<span style="">PLAIN</span>,StyleRange.<span style="">STYLE_UNDERLINED</span><span style="color: #080;">&#41;</span>,<span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">17</span>,<span style="color: #33f;">13</span>,Font.<span style="">PLAIN</span>,  Color.<span style="">RED</span>,   <span style="color: #F0F;">...</span>
                           <span style="">StyleRange</span>.<span style="">STYLE_STRIKE_THROUGH</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">31</span>,<span style="color: #33f;">5</span>, Font.<span style="">PLAIN</span>,  Color.<span style="">BLUE</span>,  <span style="color: #F0F;">...</span>
                           <span style="">StyleRange</span>.<span style="">STYLE_SUPERSCRIPT</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">37</span>,<span style="color: #33f;">3</span>, Font.<span style="">ITALIC</span>, Color.<span style="">BLACK</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">41</span>,<span style="color: #33f;">9</span>, Font.<span style="">PLAIN</span>,  Color.<span style="">BLUE</span>,  <span style="color: #F0F;">...</span>
                           <span style="">StyleRange</span>.<span style="">STYLE_SUBSCRIPT</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
          <span style="">StyleRange</span><span style="color: #080;">&#40;</span><span style="color: #33f;">51</span>,<span style="color: #33f;">5</span>, Font.<span style="">PLAIN</span>,  StyleRange.<span style="">STYLE_WAVED</span> + <span style="color: #F0F;">...</span>
                           <span style="">StyleRange</span>.<span style="">STYLE_STRIKE_THROUGH</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>;
jStyledLabel.<span style="">setStyleRanges</span><span style="color: #080;">&#40;</span>styles<span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhLabel,hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jStyledLabel,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">300</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
alt="JIDE StyledLabel (different font styles)" src="http://UndocumentedMatlab.com/images/StyledLabel.png" title="JIDE StyledLabel (different font styles)" width="300" height="20" /><p
class="wp-caption-text">JIDE StyledLabel (different font styles)</p></div></center></p><p>StyledLabels have subclasses that can be used to present styled text in tables, trees or lists. JIDE also provides the convenient <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabelBuilder.html">StyledLabelBuilder</a>,  which enables easy multi-style text construction.</p><p>Finally, JIDE provides the <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/ClickThroughStyledLabel.html">ClickThroughStyledLabel</a>, a StyledLabel extension that allows setting a target component, so that mouse clicks on the label will actually trigger the target component. This can be useful in forms where components have adjacent descriptive labels.</p><h3 id="SyntaxTextPane">SyntaxTextPane</h3><p>Multi-line syntax-highlighted code can be displayed with Matlab&#8217;s SyntaxTextPane class. SyntaxTextPane uses <a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/MIME">MIME</a> types rather than styles for syntax-highlighting, but the end-result appears similar:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jCodePane = com.<span style="">mathworks</span>.<span style="">widgets</span>.<span style="">SyntaxTextPane</span>;
codeType = jCodePane.<span style="">M_MIME_TYPE</span>;  <span style="color: #228B22;">% ='text/m-MATLAB'</span>
jCodePane.<span style="">setContentType</span><span style="color: #080;">&#40;</span>codeType<span style="color: #080;">&#41;</span>
str = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'% create a file for output\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'!touch testFile.txt\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'fid = fopen('</span><span style="color:#A020F0;">'testFile.txt'</span><span style="color:#A020F0;">', '</span><span style="color:#A020F0;">'w'</span><span style="color:#A020F0;">');\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'for i=1:10\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'    % Unterminated string:\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'    fprintf(fid,'</span><span style="color:#A020F0;">'%6.2f \\n, i);\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'end'</span><span style="color: #080;">&#93;</span>;
str = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>str,<span style="color:#A020F0;">'%'</span>,<span style="color:#A020F0;">'%%'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCodePane.<span style="">setText</span><span style="color: #080;">&#40;</span>str<span style="color: #080;">&#41;</span>
jScrollPane = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJScrollPane</span><span style="color: #080;">&#40;</span>jCodePane<span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhPanel,hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jScrollPane,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">300</span>,<span style="color: #33f;">100</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 340px"><img
alt="SyntaxTextPane panel (Matlab MIME type)" src="http://UndocumentedMatlab.com/images/SyntaxTextPane.png" title="SyntaxTextPane panel (Matlab MIME type)" width="330" height="202" /><p
class="wp-caption-text">SyntaxTextPane panel (Matlab MIME type)</p></div></center></p><p>The nice thing about SyntaxTextPane is that it syntax-highlights on-the-fly as you type or edit in the SyntaxTextPane (assuming you have not disabled editing with the <em>setEditable</em>(flag) method). This is exactly the behavior we have come to expect in the full-blown Matlab editor, and can now be embedded as a simple panel within our GUI.</p><p>Despite its misleadingly-simple look, SyntaxTextPane actually has most capabilities of the full-blown editor, not just syntax highlighting. This includes multiple undo/redo actions; smart indentation/commenting; automatic indication of corresponding block elements (if-end, for-end, etc. – also known as delimiter matching); search/replace, drag-and-drop and cut-copy-paste support; and many more.</p><p>Interested readers can use the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object"><i><b>uiinspect</b></i></a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26947-checkclass"><i><b>checkClass</b></i></a> utilities to explore the full capabilities offered by SyntaxTextPane. In this respect it would be helpful to also look at its super-class (SyntaxTextPaneBase) and the related SyntaxTextPaneUtilities class.</p><h3 id="Summary">Summary</h3><p>These Java classes are examples of built-in classes that can be used in Matlab applications, enabling a much richer GUI experience than possible using the standard (documented/supported) Matlab widgets.</p><p>As I have shown above, using these classes is extremely easy, and requires absolutely no Java knowledge. On the flip side, these internal Matlab classes may easily break in any future Matlab release, so be extra careful when deciding to use them. Future articles in this website will describe other similarly-useful built-in classes.</p><p>Have you found any other useful built-in Matlab class? If so, please <a
href="#respond">post a comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>GUI integrated HTML panel</title><link>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/</link> <comments>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/#comments</comments> <pubDate>Tue, 15 Dec 2009 12:30:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=865</guid> <description><![CDATA[Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/' rel='bookmark' title='HTML support in Matlab uicomponents'>HTML support in Matlab uicomponents</a> <small>Matlab uicomponents support HTML and CSS, enabling colored items, superscript/subscript, fonts, bold/italic/underline and many other modifications...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, I explained how a <a
target="_blank" href="http://undocumentedmatlab.com/blog/gui-integrated-browser-control/">browser control can be integrated in Matlab GUI applications</a>. Sometimes we only need to display simple HTML, for which a full browser seems like overkill. Moreover, we may wish to edit the displayed contents, which cannot be done using the browser control. The solution is to use a standard Java Swing <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html">JEditorPane</a> control, which is an editable HTML-aware control.</p><p>Oddly enough, it was only yesterday that Mikhail, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/author/99871">a known Matlab Java specialist on the CSSM newsgroup</a>, posted an example for this as answer to <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1903516/matlab-displaying-markup-html-or-other-format/1903990#1903990">a question on the StackOverflow</a> forum (slightly edited for clarity):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">mytext = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;body&gt;&lt;table border=&quot;1&quot;&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;tr&gt;&lt;th&gt;Month&lt;/th&gt;&lt;th&gt;Savings&lt;/th&gt;&lt;/tr&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;tr&gt;&lt;td&gt;January&lt;/td&gt;&lt;td&gt;$100&lt;/td&gt;&lt;/tr&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #228B22;">% Create a figure with a scrollable JEditorPane</span>
hfig = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
je = javax.<span style="">swing</span>.<span style="">JEditorPane</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span>, mytext<span style="color: #080;">&#41;</span>;
jp = javax.<span style="">swing</span>.<span style="">JScrollPane</span><span style="color: #080;">&#40;</span>je<span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>hcomponent, hcontainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jp, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, hfig<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hcontainer, <span style="color:#A020F0;">'units'</span>, <span style="color:#A020F0;">'normalized'</span>, <span style="color:#A020F0;">'position'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Turn anti-aliasing on (R2006a, Java 5.0)</span>
java.<span style="">lang</span>.<span style="">System</span>.<span style="">setProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'awt.useSystemAAFontSettings'</span>, <span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
je.<span style="">setFont</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Font</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Arial'</span>, java.<span style="">awt</span>.<span style="">Font</span>.<span style="">PLAIN</span>, <span style="color: #33f;">13</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JEditorPane</span>.<span style="">HONOR_DISPLAY_PROPERTIES</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% This only works on Java 1.5 (Matlab R14SP2 to R2007a):</span>
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">SwingUtilities2</span>.<span style="">AA_TEXT_PROPERTY_KEY</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 443px"><img
alt="Editable HTML-aware JEditorPane" src="http://UndocumentedMatlab.com/images/JEditorPane.png" title="Editable HTML-aware JEditorPane" width="433" height="184" /><p
class="wp-caption-text">Editable HTML-aware JEditorPane</p></div></center></p><p>Mikhail&#8217;s code included setting SwingUtilities2&#8242;s AA_TEXT_PROPERTY_KEY property for anti-aliased fonts. Unfortunately, <a
target="_blank" rel="nofollow" href="http://www.jroller.com/gfx/entry/be_ready_for_java_se">SwingUtilities2 was an unsupported and undocumented internal class in Java 1.5</a> (undocumented/unsupported by Sun, not MathWorks for a change&#8230;) and completely disappeared in Java 1.6 (which is bundled with Matlab R2007b onward). Therefore, SwingUtilities2 and its antialias property can only be used on Matlab releases R14SP2 (7.0.4) through R2007a (7.4) &#8211; other Matlab versions will throw an error.</p><p>Alternately, use JIDE&#8217;s AA_TEXT_PROPERTY_KEY (JIDE is bundled with Matlab and this is supported even on new Matlab releases &#8211; I will present JIDE in future articles).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">property = com.<span style="">jidesoft</span>.<span style="">swing</span>.<span style="">JideSwingUtilities</span>.<span style="">AA_TEXT_PROPERTY_KEY</span>;
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>property, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Or, simply add the following switch to your <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C">java.opt file</a>:</p><pre>-Dswing.aatext=true</pre><p>With this switch, you no longer need to set anti-aliasing separately for each component. It is entirely harmless to set this switch even on Matlab/Java versions that do not support it (the switch is simply ignored in these cases).</p><p>Note that while JEditorPane&#8217;s support for HTML is extensive, it is incomplete. It also does not contain a JavaScript engine or other web-related features we have come to expect in a browser. For the more complex stuff we can use the browser control as explained in last week&#8217;s article.</p><p>Matlab&#8217;s own multi-line editbox <b><i>uicontrol</i></b> uses JEditorPane (or actually its derived-class <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html">JTextPane</a>) as an underlying component. This means that the simple-looking Matlab editbox is actually a powerful HTML-aware component. In order to use these hidden undocumented features we need the editbox&#8217;s underlying JTextPane handle. This is done using <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">the <b><i>FindJObj</i></b> utility</a>, which will be described in my next article. Following that, I will show how to <a
target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/">customize Matlab&#8217;s dull-looking editbox</a> into something much more powerful. Here&#8217;s a sample, to help you stay tuned:</p><p><center><div
class="wp-caption aligncenter" style="width: 260px"><img
alt="HTML contents in a regular Matlab editbox" src="http://UndocumentedMatlab.com/images/editbox10.png" title="HTML contents in a regular Matlab editbox" width="250" height="75" /><p
class="wp-caption-text">HTML contents in a regular Matlab editbox</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/' rel='bookmark' title='HTML support in Matlab uicomponents'>HTML support in Matlab uicomponents</a> <small>Matlab uicomponents support HTML and CSS, enabling colored items, superscript/subscript, fonts, bold/italic/underline and many other modifications...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>GUI integrated browser control</title><link>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/</link> <comments>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/#comments</comments> <pubDate>Wed, 09 Dec 2009 15:35:21 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=847</guid> <description><![CDATA[A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, I <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-help-popup-contents/">described the built-in PopupPanel object</a>, and showed how it can be used to present popup messages with HTML content and even entire webpages. I explained that PopupPanel uses an internal browser object to achieve this. In fact, Matlab&#8217;s browser object predates PopupPanel by many years and quite a few releases. This browser object can be used as a stand-alone component that we can easily embed in our Matlab GUI applications.</p><p>Here is a simple example in which a Matlab Listbox uicontrol is used to select the contents of an adjacent browser component:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a blank figure window</span>
f=<span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'Browser GUI demo'</span>,<span style="color:#A020F0;">'Num'</span>,<span style="color:#A020F0;">'off'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the browser object on the right</span>
jObject = com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">html</span>.<span style="">HTMLBrowserPanel</span>;
<span style="color: #080;">&#91;</span>browser,container<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jObject, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, f<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>container, <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.3</span>,<span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.65</span>,<span style="color: #33f;">0.9</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the URLs listbox on the left</span>
urls = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'www.cnn.com'</span>,<span style="color:#A020F0;">'www.bbc.co.uk'</span>,<span style="color:#A020F0;">'myLocalwebpage.html'</span>,<span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'www.Mathworks.com'</span>, <span style="color:#A020F0;">'UndocumentedMatlab.com'</span><span style="color: #080;">&#125;</span>;
hListbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'listbox'</span>, <span style="color:#A020F0;">'string'</span>,urls, <span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.9</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'userdata'</span>,browser<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Set the listbox's callback to update the browser contents</span>
cbStr=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'strs = get(gcbo,'</span><span style="color:#A020F0;">'string'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'url = strs{get(gcbo,'</span><span style="color:#A020F0;">'value'</span><span style="color:#A020F0;">')};'</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser = get(gcbo,'</span><span style="color:#A020F0;">'userdata'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'msg=['</span><span style="color:#A020F0;">'&lt;html&gt;&lt;h2&gt;Loading '</span><span style="color:#A020F0;">' url '</span><span style="color:#A020F0;">' - please wait'</span><span style="color:#A020F0;">'];'</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser.setHtmlText(msg); pause(0.1); drawnow;'</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser.setCurrentLocation(url);'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hListbox,<span style="color:#A020F0;">'Callback'</span>,cbStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/Browser.png" target="_blank"><img
alt="Browser object integrated in Matlab GUI (click for large image)" src="http://UndocumentedMatlab.com/images/Browser.png" title="Browser object integrated in Matlab GUI (click for large image)" width="450" height="329" /></a><p
class="wp-caption-text">Browser object integrated in Matlab GUI</p></div></center></p><p>In this simple example, we can see how the Java browser object can easily be controlled by Matlab. Specifically, we use two modes of the browser: first we present an HTML message (&#8216;<b>Loading www.cnn.com &#8211; please wait</b>&#8216;) and then replacing this content with the actual webpage, if accessible. If the webpage is not accessible, an error message is displayed:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/Browser_404.png" target="_blank"><img
alt="Browser message when webpage is missing (click for large image)" src="http://UndocumentedMatlab.com/images/Browser_404.png" title="Browser message when webpage is missing (click for large image)" width="450" height="165" /></a><p
class="wp-caption-text">Browser message when webpage is missing</p></div></center></p><p>We can easily expand this simple example to display any HTML message or webpage, in a seamless integration within our GUI.</p><p>Now, who ever said that Matlab GUI looks static or boring???</p><p>In an unrelated note, I would like to extend good wishes to Ken Orr, who has left the Mathworks Desktop development team to join Apple <a
target="_blank" rel="nofollow" href="http://explodingpixels.wordpress.com/2009/12/04/heading-to-apple/">a few days ago</a>. You probably know Ken from his good work on the Desktop and the official <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/">Matlab Desktop blog</a>. Hopefully, in his new position Ken will be able to influence Mac Java in a way that will reduce the <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2009/08/31/pouncing-on-snow-leopard/">numerous recurring issues that afflict Matlab Mac releases</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Customizing help popup contents</title><link>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/</link> <comments>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/#comments</comments> <pubDate>Mon, 30 Nov 2009 16:37:30 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=819</guid> <description><![CDATA[The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-popup-messages/' rel='bookmark' title='Setting system tray popup messages'>Setting system tray popup messages</a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, I was <a
target="_blank" href="http://undocumentedmatlab.com/blog/context-sensitive-help/#comment-4691">asked by a reader</a> how to programmatically display the popup help window and customize it with arbitrary contents. This help window displays the <b><i>doc</i></b>-page associated with the current Command Window or Editor text.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Help popup" src="http://UndocumentedMatlab.com/images/HelpPopup2.png" title="Help popup" width="450" height="334" /><p
class="wp-caption-text">Help popup</p></div></center></p><p>To programmatically display this help popup, a <a
target="_blank" rel="nofollow" href="http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/">modeless MJDialog Java window</a>, we need to run the following on Matlab releases that support these windows (R2007b onward):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
jClassName = <span style="color:#A020F0;">'com.mathworks.mlwidgets.help.HelpPopup'</span>;
jPosition = java.<span style="">awt</span>.<span style="color: #0000FF;">Rectangle</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">300</span><span style="color: #080;">&#41;</span>;
helpTopic = <span style="color:#A020F0;">'surf'</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'showHelp'</span>,jClassName,jTextArea,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,jPosition,helpTopic<span style="color: #080;">&#41;</span>;</pre></div></div><p>Where:<br
/> 1) jPosition sets popup’s pixel size and position (X,Y,Width,Height). Remember that Java counts from the top down (contrary to Matlab) and is 0-based. Therefore, Rectangle(0,0,400,300) is a 400×300 window at the screen’s top-left corner.<br
/> 2) helpTopic is the help topic of your choice (the output of the <b><i>doc</i></b> function). To display arbitrary text, you can create a simple .m file that only has a main help comment with your arbitrary text, which will be presented in the popup.<br
/> 3) on R2011b, we could simplify the above code snippet with the following replacement:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">helpUtils.<span style="">errorDocCallback</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'surf'</span><span style="color: #080;">&#41;</span></pre></div></div><p>4) on R2007b, we need to use the equivalent but more cumbersome <b><i>awtinvoke</i></b> function instead of <b><i>javaMethodEDT</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jniSig = <span style="color:#A020F0;">'showHelp(Ljavax.swing.JComponent;Lcom.mathworks.mwswing.binding.KeyStrokeList;Ljava.awt.Rectangle;Ljava.lang.String;)'</span>;
<span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jClassName,jniSig,jTextArea,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,jPosition,helpTopic<span style="color: #080;">&#41;</span>;</pre></div></div><p>For example, if we had a sample.m file with the following contents:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> sample
<span style="color: #228B22;">% The text in this function's main comment will be presented in the</span>
<span style="color: #228B22;">% help popup. &lt;a href=&quot;http://UndocumentedMatlab.com&quot;&gt;Hyperlinks&lt;/a&gt;</span>
<span style="color: #228B22;">% are supported, but unfortunately not full-fledged HTML.</span></pre></div></div><p>Then we would get this result:<br
/><center><div
class="wp-caption aligncenter" style="width: 410px"><img
alt="User-created arbitrary text" src="http://UndocumentedMatlab.com/images/HelpPopup3.png" title="User-created arbitrary text" width="400" height="194" /><p
class="wp-caption-text">User-created arbitrary text</p></div></center></p><p>Well, it does get the message across, but looks rather dull. It would be nice if this could be improved to provide full-scale HTML support. Unfortunately, <a
rel="nofollow" target="_blank" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/doc.html">Matlab documentation</a> says this cannot be done:</p><p><q>The <b>doc</b> function is intended only for reference pages supplied by The MathWorks. The exception is the <b>doc</b> UserCreatedClassName syntax. <b>doc</b> does not display HTML files you create yourself. To display HTML files for functions you create, use the web function.</q></p><p>Luckily for us, there is an undocumented back-door to do this: The idea is to search all visible Java windows for the HelpPopup, then move down its component hierarchy to the internal web browser (a com.mathworks.mlwidgets.html.HTMLBrowserPanel object), then update the content with our arbitrary HTML or a webpage URL:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Find the Help popup window</span>
jWindows = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJDialog</span>.<span style="">getWindows</span>;
jPopup = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">for</span> idx=<span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jWindows<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'HelpPopup'</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>.<span style="">isVisible</span>
      jPopup = jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">break</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Update the popup with selected HTML</span>
html=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'Full HTML support: &lt;b&gt;&lt;font color=red&gt;bold&lt;/font&gt;&lt;/b&gt;, '</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'&lt;i&gt;italic&lt;/i&gt;, &lt;a href=&quot;matlab:dir&quot;&gt;hyperlink&lt;/a&gt;, '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'symbols (&amp;#8704;&amp;#946;) etc.'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>jPopup<span style="color: #080;">&#41;</span>
  browser = jPopup.<span style="">getContentPane</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
  browser.<span style="">setHtmlText</span><span style="color: #080;">&#40;</span>html<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 237px"><img
alt="Help popup with HTML content" src="http://UndocumentedMatlab.com/images/HelpPopup4.png" title="Help popup with HTML content" width="227" height="117" /><p
class="wp-caption-text">Help popup with HTML content</p></div></center></p><p>We can display HTML content and highlight certain keywords using the <i>setHtmlTextAndHighlightKeywords</i> method:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">browser.<span style="">setHtmlTextAndHighlightKeywords</span><span style="color: #080;">&#40;</span>html,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'support'</span>,<span style="color:#A020F0;">'symbols'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 237px"><img
alt="HTML content with highlighting" src="http://UndocumentedMatlab.com/images/HelpPopup5.png" title="HTML content with highlighting" width="227" height="117" /><p
class="wp-caption-text">HTML content with highlighting</p></div></center></p><p>Instead of specifying the HTML content, we can point this browser to a web-page URL (no need for the &#8220;http://&#8221; prefix):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">browser.<span style="">setCurrentLocation</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'UndocumentedMatlab.com'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/HelpPopup6.png"><img
alt="Help popup browser displaying a URL web-page" src="http://UndocumentedMatlab.com/images/HelpPopup6.png" title="Help popup browser displaying a URL web-page" width="450" height="380" /></a><p
class="wp-caption-text">Help popup browser displaying a URL web-page</p></div></center></p><p>The HTMLBrowserPanel includes a full-fledged browser (which may be different across Matlab releases/platforms). This browser supports HTML, CSS, JavaScript and other web-rendering aspect that we would expect from a modern browser. Being a full-fledged browser, we have some control over its appearance e.g., <i>addAddressBox(1,1)</i> and other internal methods. Interested readers may use my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object">UiInspect utility</a> to explore these options.</p><p>As a technical note, HTMLBrowserPanel is actually only a JPanel that contains the actual Mozilla browser. Luckily for us, MathWorks extended this panel class with the useful methods presented above, that forward the user requests to the actual internal browser. This way, we don&#8217;t need to get the actual browser reference (although you can, of course).</p><p>I have created a utility function that encapsulates all the above, and enables display of Matlab doc pages, as well as arbitrary text, HTML or webpages. This <b><i> popupPanel</i></b> utility can now be <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25975-popuppanel-displays-a-popup-panel-with-specified-help-topic-html-or-webpage">downloaded from the Matlab File Exchange</a>.</p><p>An interesting exercise left for the readers, is adapting the main heavy-weight documentation window to display user-created HTML help pages. This can be achieved by means very similar to those shown in this article.</p><p>Of course, as the official documentation states, we could always use the fully-supported <b><i>web</i></b> function to display our HTML or URLs. Under the hood, <b><i>web</i></b> uses exactly the same HTMLBrowserPanel as out HelpPopup. The benefit of using the methods shown here is the use of a lightweight undecorated popup window which looks well-integrated with the existing Matlab help.</p><p>Please note that the HelpPopup implementation might change without warning between Matlab releases. An entirely separate, although related, implementation is Matlab&#8217;s built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/context-sensitive-help/">context-sensitive help system</a>, which I described some months ago. That implementation did not rely on Java and worked on much earlier Matlab releases.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-popup-messages/' rel='bookmark' title='Setting system tray popup messages'>Setting system tray popup messages</a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 02:48:49 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/html/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.380s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328348927; expires=Sun, 03-Feb-2013 09:48:47 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 09:48:49 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 10:48:49 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               65f15ab473d469f9b2f910a0011389cc
Content-Encoding:   gzip
-->
