<?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; UI controls</title> <atom:link href="http://undocumentedmatlab.com/blog/category/ui-controls/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Wed, 08 Feb 2012 18:40:25 +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>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>Tri-state checkbox</title><link>http://undocumentedmatlab.com/blog/tri-state-checkbox/</link> <comments>http://undocumentedmatlab.com/blog/tri-state-checkbox/#comments</comments> <pubDate>Wed, 19 Oct 2011 14:04: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[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2467</guid> <description><![CDATA[Matlab checkboxes can easily be made to support tri-state functionality.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>When presenting information visually in graphical user interfaces (GUIs), we often need to present and enable interaction with state data (such as On/Off). In most cases, we would naturally use a checkbox to present the information and enable interaction. But What can we do if the data has three possible states. For example, Yes/No/Maybe, or: Full/Empty/Partial, or: Up/Down/Undetermined ?</p><p>Until today, Matlab GUIs had to resort to using drop-down (aka combo-box or popup-menu) or radio-button controls to present such information. However, would it not be nicer if we could still use a checkbox? Outside Matlab, such a control is known as a tri-state checkbox and many modern GUI frameworks support it. Well, surprise surprise, so does Matlab (although you would never guess it from the documentation).</p><h3 id="CheckBoxTree">CheckBoxTree</h3><p>Last year, I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/#Built-in-classes">described</a> a built-in Matlab tree control whose nodes have tri-state checkboxes:</p><p><center><div
class="wp-caption aligncenter" style="width: 321px"><img
alt="a regular MJTree (left) and a CheckBoxTree (right)" src="http://UndocumentedMatlab.com/images/CheckBoxTree.png" title="a regular MJTree (left) and a CheckBoxTree (right)" width="311" height="218" /><p
class="wp-caption-text">a regular MJTree (left) and a CheckBoxTree (right)</p></div></center></p><p>Today I will show how we can use these checkboxes as independent GUI controls.</p><h3 id="uicontrol">Modifying the standard Matlab checkbox uicontrol</h3><p>In order to modify the standard Matlab checkbox <i><b>uicontrol</b></i>, we need to first get its underlying Java component reference. This is done using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We then update its UI wrapper to be the same as the <code>CheckBoxTree</code>&#8216;s checkbox control.</p><p>To programmatically set a mixed state we update the &#8216;selectionState&#8217; client property to <code>SelectionState.MIXED</code> (<code>SelectionState</code> also has the <code>SELECTED</code> and <code>NOT_SELECTED</code> values).</p><p>Here is an end-to-end example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hCB = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jCB = findjobj<span style="color: #080;">&#40;</span>hCB<span style="color: #080;">&#41;</span>;
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">TriStateButtonUI</span><span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
newState = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">SelectionState</span>.<span style="">MIXED</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, newState<span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="Matlab checkboxes displaying mixed states" src="http://UndocumentedMatlab.com/images/CheckBox_TriState.png" title="Matlab checkboxes displaying mixed states" width="289" /><p
class="wp-caption-text">Matlab checkboxes displaying mixed states</p></div></center></p><h3 id="independent">Displaying as an independent Java control</h3><p>Instead of retrofitting a standard Matlab <i><b>uicontrol</b></i> as described above, we can directly use the standard <code>javax.swing.JCheckBox</code> which does not normally support tri-state (it only has two states), displaying it in our GUI with the built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/javacomponent/"><i><b>javacomponent</b></i> function</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = javax.<span style="">swing</span>.<span style="">JCheckBox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'JCheckBox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</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>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
import com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.*
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>TriStateButtonUI<span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, SelectionState.<span style="">MIXED</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p>Note that instead of using <code>javax.swing.JCheckBox</code>, we could use the internal Matlab class <code>com.mathworks.mwswing.MJCheckBox</code>, which directly extends <code>JCheckBox</code> and adds mnemonic (shortcut-key) support, but is otherwise fully compatible with <code>JCheckBox</code>. Actually, it is <code>MJCheckBox</code> which is the underlying Java component of the standard Matlab checkbox <i><b>uicontrol</b></i>.</p><h3 id="alternatives">Alternative controls</h3><p>Now that we have seen that Matlab includes built-in support (well, at least support in the technical sense, not the official customer-support sense), would you be surprised to learn that it includes similar support in other internal components as well?</p><p>The internal Matlab class <code>com.mathworks.mwt.MWCheckbox</code> directly supports a tri-state (yes/no/maybe) checkbox, without any need to update its UI, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = com.<span style="">mathworks</span>.<span style="">mwt</span>.<span style="">MWCheckbox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MWCheckbox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</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>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
jCB.<span style="">setMixedState</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Retrieve the current state</span>
isMixedFlag = jCB.<span style="">isMixedState</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% true/false</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="MJCheckBox vs. MWCheckbox" src="http://UndocumentedMatlab.com/images/CheckBox_MWT_MWSwing.png" title="MJCheckBox vs. MWCheckbox" width="289" /><p
class="wp-caption-text">MJCheckBox vs. MWCheckbox</p></div></center></p><p>Note that the State property, which controls the standard selected/unselected state, is entirely independent from the MixedState property. Both State and MixedState are boolean properties, so to get the actual checkbox state we need to query both of these properties.</p><p>Another internal Matlab class that we can use is JIDE&#8217;s <code>com.jidesoft.swing.TristateCheckBox</code> (which is pre-bundled in Matlab and is fully documented <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/TristateCheckBox.html">here</a>).</p><p>There are many other tri-state checkbox alternatives available online (for example, <a
target="_blank" rel="nofollow" href="https://forums.oracle.com/forums/search.jspa?threadID=&#038;q=%28tri-state+OR+tristate%29+AND+checkbox&#038;objID=c285&#038;dateRange=all&#038;userID=&#038;numResults=30&#038;rankBy=10001">here</a>, <a
target="_blank" rel="nofollow" href="http://www.javaspecialists.eu/archive/Issue145.html">here</a> and <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1263323/tristate-checkboxes-in-java">here</a>). We can easily include them in our Matlab GUI with the <i><b>javacomponent</b></i> function. Using external components we can be more certain of the compatibility issues in past and future Matlab releases. On the other hand, internal Matlab classes do have the advantage of being inherently accessible on all platforms of the same Matlab release, whereas non-Matlab components must be included in our deployment package.</p><p>Do you use any tri-state controls, either Matlab or external, in your work? If so, please share your experience in a <a
href="http://UndocumentedMatlab.com/blog/tri-state-checkbox/#respond">comment below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/tri-state-checkbox/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>uisplittool &amp; uitogglesplittool</title><link>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/</link> <comments>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/#comments</comments> <pubDate>Thu, 09 Dec 2010 00:06:33 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Figure window]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Toolbar]]></category> <category><![CDATA[uitools]]></category> <category><![CDATA[uiundo]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1994</guid> <description><![CDATA[Matlab's undocumented uisplittool and uitogglesplittool are powerful controls that can easily be added to Matlab toolbars - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/figure-toolbar-components/' rel='bookmark' title='Figure toolbar components'>Figure toolbar components</a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uiundo/' rel='bookmark' title='Customizing uiundo'>Customizing uiundo</a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab 7.6 (R2008a) and onward contain a reference to <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> in the javacomponent.m and %matlabroot%/bin/registry/hg.xml files. These are reported as built-in functions by the <i><b>which</b></i> function, although they have no corresponding m-file as other similar built-in functions (note the double &#8216;t&#8217;, as in <i>split-tool</i>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> uisplittool
built-in <span style="color: #080;">&#40;</span>C<span style="color: #F0F;">:</span>\Matlab\R2010b\toolbox\matlab\uitools\uisplittool<span style="color: #080;">&#41;</span></pre></div></div><p>These uitools are entirely undocumented, even today (R2010b). They puzzled me for a very long time. An acute reader (Jeremy Raymonds) suggested they are related to toolbars, like other uitools such as the <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>. This turned out to be the missing clue that unveiled these useful tools:</p><h3 id="Intro">So what are <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i>?</h3><p>Both <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> are basic Handle-Graphics building blocks used in Matlab toolbars, similarly to the well-documented <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>.</p><p><i><b>uisplittool</b></i> presents a simple drop-down, whereas <i><b>uitogglesplittool</b></i> presents a drop-down that is also selectable.</p><p>The Publish and Run controls on the Matlab Editor&#8217;s toolbar are examples of <i><b>uisplittool</b></i>, and so are the Brush / Select-Data control on the figure toolbar, and the plot-selection drop-down on the Matlab Desktop&#8217;s Workspace toolbar:<br
/><center><div
class="wp-caption aligncenter" style="width: 371px"><img
alt="uisplittool in action in the Matlab Desktop" src="http://UndocumentedMatlab.com/images/uisplittool.png" title="uisplittool in action in the Matlab Desktop" width="361" height="221" /><p
class="wp-caption-text"><i><b>uisplittool</b></i> in action in the Matlab Desktop</p></div></center></p><h3 id="Appearance">Adding <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar</h3><p>Adding a <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar is done in a similar manner to adding <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hToolbar = findall<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'FigureToolBar'</span><span style="color: #080;">&#41;</span>;
hUndo=uisplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar<span style="color: #080;">&#41;</span>;       <span style="color: #228B22;">% uisplittool</span>
hRedo=uitogglesplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% uitogglesplittool</span></pre></div></div><p>Like <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>, <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> also have unique <strong>Type</strong> property values, &#8216;uisplittool&#8217; and &#8216;uitogglesplittool&#8217; respectively. The handles can also be tested using the built-in <i><b>isa</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">isa</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hUndo<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'uisplittool'</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% or: 'uitogglesplittool'</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">1</span>
&gt;&gt; <span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hUndo<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
uisplittool</pre></div></div><p>Just as with <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s, the new buttons have an empty button-face appearance, until we fix their <strong>CData</strong>, <strong>Tooltip</strong> and similar settable properties:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Load the Redo icon</span>
icon = <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>;
<span style="color: #080;">&#91;</span>cdata,map<span style="color: #080;">&#93;</span> = <span style="color: #0000FF;">imread</span><span style="color: #080;">&#40;</span>icon<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Convert white pixels into a transparent background</span>
map<span style="color: #080;">&#40;</span><span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>+map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>+map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>==<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">NaN</span>;
&nbsp;
<span style="color: #228B22;">% Convert into 3D RGB-space</span>
cdataRedo = ind2rgb<span style="color: #080;">&#40;</span>cdata,map<span style="color: #080;">&#41;</span>;
cdataUndo = cdataRedo<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">16</span><span style="color: #F0F;">:</span>-<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the icon (and its mirror image = undo) to latest toolbar</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hUndo, <span style="color:#A020F0;">'cdata'</span>,cdataUndo, <span style="color:#A020F0;">'tooltip'</span>,<span style="color:#A020F0;">'undo'</span>,<span style="color:#A020F0;">'Separator'</span>,<span style="color:#A020F0;">'on'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ClickedCallback'</span>,<span style="color:#A020F0;">'uiundo(gcbf,'</span><span style="color:#A020F0;">'execUndo'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRedo, <span style="color:#A020F0;">'cdata'</span>,cdataRedo, <span style="color:#A020F0;">'tooltip'</span>,<span style="color:#A020F0;">'redo'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ClickedCallback'</span>,<span style="color:#A020F0;">'uiundo(gcbf,'</span><span style="color:#A020F0;">'execRedo'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="User-created uisplittool &amp; uitogglesplittool toolbar buttons" src="http://UndocumentedMatlab.com/images/uisplittool2b2.png" title="User-created uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="107" /><p
class="wp-caption-text">User-created <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons</p></div></center></p><p>Note that the controls can be created with these properties in a single command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hUndo = uisplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar, <span style="color:#A020F0;">'cdata'</span>,cdataRedo, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Arranging">Re-arranging the toolbar controls placement</h3><p>Let us now re-arrange our toolbar buttons. Unfortunately, a bug causes <i><b>uisplittool</b></i>s and <i><b>uitogglesplittool</b></i>s to always be placed flush-left when the toolbar&#8217;s children are re-arranged (anyone at TMW reading this in time for the R2011a bug-parade selection?).</p><p>So, we can&#8217;t re-arrange the buttons at the HG-children level. Luckily, we can re-arrange directly at the Java level (note that until now, the entire discussion of <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> was purely Matlab-based):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jToolbar = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hToolbar,<span style="color:#A020F0;">'JavaContainer'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'ComponentPeer'</span><span style="color: #080;">&#41;</span>;
jButtons = jToolbar.<span style="">getComponents</span>;
<span style="color: #0000FF;">for</span> buttonId = <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#41;</span>-<span style="color: #33f;">3</span> <span style="color: #F0F;">:</span> -<span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">7</span>  <span style="color: #228B22;">% end-to-front</span>
   jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>buttonId<span style="color: #080;">&#41;</span>, buttonId+<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">5</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% Separator</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>end-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">6</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% Undo</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">7</span><span style="color: #080;">&#41;</span>;     <span style="color: #228B22;">% Redo</span>
jToolbar.<span style="">revalidate</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" src="http://UndocumentedMatlab.com/images/uisplittool2c2.png" title="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="106" /><p
class="wp-caption-text">Re-arranged <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons<br/>(not as simple as it may sound)</p></div></center></p><p>Next week, I will combine the information in this article, with <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/uiundo/">last year&#8217;s articles</a> about <i><b>uiundo</b></i>, and show how we can create a dynamic figure toolbar drop-down of undo/redo events. Here is a preview to whet your appetite:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="undo/redo buttons implemented using uisplittool" src="http://UndocumentedMatlab.com/images/uisplittool4.png" title="undo/redo buttons implemented using uisplittool" width="450" height="226" /><p
class="wp-caption-text">undo/redo buttons implemented using <i><b>uisplittool</b></i></p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/figure-toolbar-components/' rel='bookmark' title='Figure toolbar components'>Figure toolbar components</a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uiundo/' rel='bookmark' title='Customizing uiundo'>Customizing uiundo</a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Panel-level uicontrols</title><link>http://undocumentedmatlab.com/blog/panel-level-uicontrols/</link> <comments>http://undocumentedmatlab.com/blog/panel-level-uicontrols/#comments</comments> <pubDate>Wed, 24 Nov 2010 18:00:53 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[TitleHandle]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1976</guid> <description><![CDATA[Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control<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/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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In one of my larger Matlab applications &#8211; <em>Integration-lab Debriefing System</em> (IDS, which shall be described in a future dedicated article) &#8211; I wanted to present a panel-level checkbox that applies to the entire panel contents. In my particular case, the IDS <i><b>uipanel</b></i> contained a Java table (another future topic) with multiple selectable rows, and I needed a global checkbox that selects all (or none) of them at once:</p><p><center><div
class="wp-caption aligncenter" style="width: 138px"><img
alt='Panel-level ("Objects") checkbox' src="http://UndocumentedMatlab.com/images/IDS_Main_Tabs.png" title='Panel-level ("Objects") checkbox' width="128" height="107" /><p
class="wp-caption-text">Panel-level ("Objects") checkbox</p></div></center></p><p>One way to do this is to calculate the checkbox&#8217;s desired position relative to the <i><b>uipanel</b></i> and place a regular checkbox-style <i><b>uicontrol</b></i> there. The checkbox can even be made a child of the <i><b>uipanel</b></i> with &#8216;normalized&#8217; units, thereby moving and resizing it together with its <i><b>uipanel</b></i> parent when the later is moved or resized.</p><p>But there&#8217;s a much simpler method that want to share. It relies on the undocumented fact that the <i><b>uipanel</b></i>&#8216;s title label is a simple hidden <i><b>uicontrol</b></i> child of the <i><b>uipanel</b></i> handle. This <i><b>uicontrol</b></i> handle can be found and simply transformed from a &#8216;style&#8217;='text&#8217; control into a &#8216;style&#8217;='checkbox&#8217; control, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Prepare the panel</span>
hPanel = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.4</span>,<span style="color: #33f;">0.4</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Objects'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the title label's handle</span>
<span style="color: #0000FF;">warning</span> off MATLAB<span style="color: #F0F;">:</span>Uipanel<span style="color: #F0F;">:</span>HiddenImplementation  <span style="color: #228B22;">% turn off warning</span>
hTitle = <span style="color: #0000FF;">setdiff</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hPanel<span style="color: #080;">&#41;</span>,hPanel<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% retrieve title handle</span>
hTitle = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hPanel,<span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative, uses hidden prop</span>
&nbsp;
<span style="color: #228B22;">% Modify the uicontrol style; add 20 pixel space for the checkbox</span>
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hTitle,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTitle, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">1</span>, <span style="color:#A020F0;">'pos'</span>,newPos<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 130px"><img
alt="Panel-level checkbox" src="http://UndocumentedMatlab.com/images/IDS_Main_Checkbox.png" title="Panel-level checkbox" width="120" height="70" /><p
class="wp-caption-text">Panel-level checkbox</p></div></center></p><p>Note that we can retrieve the title handle using either the <i><b>uipanel</b></i>&#8216;s hidden property <b>TitleHandle</b>, or by scanning the panel&#8217;s children using <i><b>findall</b></i>. I prefer the <b>TitleHandle</b> approach because it does not require modification (minor as it might be) when the panel already contains other children.</p><p>The down-side is that since <b>TitleHandle</b> is an undocumented hidden property, it may change its name, its behavior or even be removed in some future Matlab release. In fact, there&#8217;s even a Matlab warning about this, unless you turn it off using:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">warning</span> off MATLAB<span style="color: #F0F;">:</span>Uipanel<span style="color: #F0F;">:</span>HiddenImplementation</pre></div></div><p>We can use this approach for more complex options panels, as the following example illustrates. Here, we set radio-button controls rather than a checkbox control, and also modify the title color to blue:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Main panel and basic alternative control</span>
hPanelMain = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.1,.1,.8,.8<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Title'</span>,<span style="color:#A020F0;">'Main'</span>, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;
hAlt1 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.1,.8,.5,.1<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Alternative #1'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative options panel #2</span>
hAlt2 = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.07,.4,.5,.35<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Alternative #2'</span><span style="color: #080;">&#41;</span>;
hAlt2Title = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt2, <span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt2Title,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAlt2Title, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'pos'</span>,newPos<span style="color: #080;">&#41;</span>;
hAlt2a = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt2, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.6,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Option 1'</span><span style="color: #080;">&#41;</span>;
hAlt2b = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt2, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.2,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Option 2'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative options panel #3</span>
hAlt3 = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.07,.05,.5,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Alternative #3'</span><span style="color: #080;">&#41;</span>;
hAlt3Title = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt3, <span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt3Title,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAlt3Title, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'pos'</span>,newPos, <span style="color:#A020F0;">'ForegroundColor'</span>,<span style="color:#A020F0;">'blue'</span><span style="color: #080;">&#41;</span>;
hAlt3a = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt3, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.5,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'popup'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Option 3a'</span>,<span style="color:#A020F0;">'Option 3b'</span>,<span style="color:#A020F0;">'Option 3c'</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: 431px"><img
alt="Advanced panel-level controls" src="http://UndocumentedMatlab.com/images/Panel_Controls.png" title="Advanced panel-level controls" width="421" height="373" /><p
class="wp-caption-text">Advanced panel-level controls</p></div></center></p><p>Note that since the hAlt2Title and hAlt3Title radio-buttons are children of their respective <i><b>uipanel</b></i> parents, we cannot use a simple <i><b>uibuttongroup</b></i> to group them in a mutual-exclusion group. Instead, we must use a dedicated callback function. This is actually quite easy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Set the callback for all relevant radio-buttons</span>
hButtonGroup = <span style="color: #080;">&#91;</span>hAlt1, hAlt2Title, hAlt3Title<span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButtonGroup, <span style="color:#A020F0;">'Callback'</span>, <span style="color: #080;">&#123;</span>@SelectionCb, hButtonGroup<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% This is the callback function that manages mutual exclusion</span>
<span style="color: #0000FF;">function</span> SelectionCb<span style="color: #080;">&#40;</span>hSrc,hEvent,hButtonGroup<span style="color: #080;">&#41;</span>
   otherButtons = <span style="color: #0000FF;">setdiff</span><span style="color: #080;">&#40;</span>hButtonGroup,hSrc<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>otherButtons,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSrc,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% needed to prevent de-selection</span>
<span style="color: #0000FF;">end</span></pre></div></div><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/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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/panel-level-uicontrols/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>The javacomponent function</title><link>http://undocumentedmatlab.com/blog/javacomponent/</link> <comments>http://undocumentedmatlab.com/blog/javacomponent/#comments</comments> <pubDate>Wed, 04 Aug 2010 18:00:59 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <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[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1793</guid> <description><![CDATA[Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/hgfeval/' rel='bookmark' title='The hgfeval function'>The hgfeval function</a> <small>The semi-documented hgfeval function can be useful for callback chaining - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In this blog I have often showed how using Java components can significantly improve Matlab GUI. Here is a simple reminder:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
target="_blank" href="http://UndocumentedMatlab.com/images/uicomponent.png"><img
alt="sample Java components integrated in Matlab figure window (click for details)" src="http://UndocumentedMatlab.com/images/uicomponent.png" title="sample Java components integrated in Matlab figure window (click for details)" width="450" height="354" /></a><p
class="wp-caption-text">sample Java components integrated in Matlab figure window (click for details)</p></div></center></p><p>Matlab is highly integrated with Java, and Java classes can seamlessly be accessed from Matlab. However, displaying Java GUI objects, as opposed to using computational (non-displayable) Java classes, requires using Matlab&#8217;s built-in <i><b>javacomponent</b></i> function. I have often used this function in past articles here, and today I would like to describe it in more detail.</p><h3 id="javacomponent">javacomponent</h3><p><i><b>javacomponent</b></i>, available since R14 (Matlab 7.0), is yet another <a
target="_blank" href="http://UndocumentedMatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> built-in function. This means that the function is explained in a comment within the function (which can be seen via the <i><b>edit</b>(&#8216;javacomponent&#8217;)</i> command), but nonetheless does not have official help or doc pages. It is an unsupported function originally intended only for internal Matlab use (which of course doesn&#8217;t mean we can&#8217;t use it).</p><p><i><b>javacomponent</b></i> accepts a component class name (a string) or a reference to a previously-created component object, an optional pixel position parameter (default=[20,20,60,20], just like <i><b>uicontrol</b></i>; may also contain the strings &#8216;North&#8217;, &#8216;South&#8217;, &#8216;East&#8217; or &#8216;West&#8217;), and an optional parent container handle  (defaults to the current figure). <i><b>javacomponent</b></i> then adds the requested component as a child of the requested parent container and wraps it in a Matlab Handle-Graphics (HG) container. <i><b>javacomponent</b></i> returns two handles: the Matlab HG container handle and a reference (handle) to the Java component. Here are some sample invocation formats:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #080;">&#91;</span>jButton, hButton<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>
hButton =
	javahandle_withcallbacks.<span style="">javax</span>.<span style="">swing</span>.<span style="">JButton</span>
jButton =
          <span style="color: #33f;">158.002197265625</span>
&nbsp;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span>,<span style="color:#A020F0;">'North'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click me!'</span><span style="color: #080;">&#41;</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">50</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click me!'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'East'</span>,hFig<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note the difference between Java object creation and <i><b>javacomponent</b></i>: A pre-created Java object only resides in JVM (Java Virtual Machine) memory, not onscreen, until <i><b>javacomponent</b></i> is called to display it. <i><b>javacomponent</b></i> only creates objects when a class name (string) parameter is passed to it, as a convenience service to programmers. In practice, it is better to separate these two actions: create the Java object separately, and then pass the object&#8217;s reference handle to <i><b>javacomponent</b></i> for display. This enables easier error-trapping if the Java object cannot be created or fails to initialize, before attempting to display the object:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create and initialize a JScrollBar object</span>
<span style="color: #0000FF;">try</span>
   jScrollbar = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JScrollBar'</span><span style="color: #080;">&#41;</span>;
   jScrollbar.<span style="">setOrientation</span><span style="color: #080;">&#40;</span>jScrollbar.<span style="">HORIZONTAL</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Cannot create Java-based scroll-bar!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #228B22;">% Display the object onscreen</span>
<span style="color: #0000FF;">try</span>
   javacomponent<span style="color: #080;">&#40;</span>jScrollbar,<span style="color:#A020F0;">'South'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Cannot display Java-base scroll-bar!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that Java GUI object should always use the EDT (Event Dispatch Thread). The reasons for this were outlined in the recent <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">Matlab-EDT article</a>. For this reason, the JScrollBar is created using the built-in <i><b>javaObjectEDT</b></i> function, which exists since R2008a and became documented/supported in R2009a.</p><p><i><b>javacomponent</b></i> accepts parent handles that are figures, toolbars, <i><b>uipanel</b></i>s or <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/"><i><b>uicontainer</b></i>s</a>.  Unfortunately, frames are not <i><b>uicontainer</b></i>s and therefore cannot be used as <i><b>javacomponent</b></i> parents. <b><u>Addendum Aug 6 2010</u></b>: I made an incorrect statement in the original post here regarding <i><b>uipanel</b></i>s, which has now been removed. I thank the reader who pointed this out to me.</p><p>Once the component has been created, even before it has been placed onscreen, it can be manipulated just like any other Java object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click again!'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: set(jButton,'text','…')</span></pre></div></div><p>The component can also be manipulated to some extent via its HG container, which is of a special Matlab type (class) called hgjavacomponent. This includes getting/setting the component position, position units, visibility, resizing callback, tag, <b>UserData</b> etc:</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>hButton,<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.2</span>,<span style="color: #33f;">0.3</span>,<span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButton,<span style="color:#A020F0;">'visible'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%note: on/off, not true/false as in Java</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButton,<span style="color:#A020F0;">'ResizeFcn'</span>,<span style="color: #080;">&#123;</span>@resizeCallbackFunc,param1,param2<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>When adding Java components which are container classes (descendants of <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html">java.awt.Container</a>), it is important to remember that only other Java components can be added to these containers. Matlab objects such as axes (for plots or images) and <i><b>uicontrol</b></i>s cannot be added since they do not have a Container wrapper.  Therefore, feel free to use these Java containers as long as their contained GUI is limited to Java components (JButton, JComboBox etc.). This limitation is very annoying – it would be very useful to be able to place Matlab axes or <i><b>uicontrol</b></i>s within a JTabbedPane or JSplitPane. Instead, we need to rely on Matlab-based workarounds (<a
target="_blank" href="http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/"><i><b>uitab</b></i></a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/23073-uisplitpane"><i><b>uisplitpane</b></i></a>) which are cumbersome compared to their Java counterparts.</p><p><i><b>javacomponent</b></i> can be used to place not only Swing components but also Swing-extended components onscreen. Matlab itself almost never uses Swing components as-is, instead preferring to use MathWorks-derived extensions of these components, generally in the com.mathworks.mwswing or com.mathworks.widgets packages. These packages and their classes are all in the static Java classpath and are therefore automatically available for use by Matlab programmers.</p><p>Just like Matlab components, <i><b>javacomponent</b></i> can also display third-party or your own Swing-derived components. There are quite a few online sources  for Swing components that can easily be incorporated in your Matlab application. Simply download the relevant class files, add them to your static (via classpath.txt) or dynamic (via <i><b>javaaddpath</b></i>) Java classpath, use <i><b>javacomponent</b></i> to display them, then use their reference handle to manipulate their appearance and behavior.</p><p><i><b>javacomponent</b></i>, useful as it is, has several limitations. In its string variant (classname) it requires a fully-qualified classname that is not inferred automatically. It also has a different parameters format than <i><b>uicontrol</b></i>, which may confuse users. <i><b>javacomponent</b></i> also cannot display <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html">java.awt.Window</a> components. Finally, it returns two handles – one is a <i><b>handle</b>()</i> reference of the Java object; the second an HG handle (a double numeric value) of the automatically-created HG container – <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/172873">users are often confused</a> as to which property should be set on which of these handles.</p><h3 id="utility">javacomponent-based utility functions</h3><p>To overcome these limitations, I created <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14583-UIComponent"><i><b>UIComponent</b></i></a> – a utility that merges <i><b>uicontrol</b></i> and <i><b>javacomponent</b></i>, available for download on the File Exchange.  It accepts all <i><b>uicontrol</b></i> parameters and styles, as well as any other displayable Java (Swing/AWT) class. <i><b>uicontrol</b></i>&#8216;s calling syntax was preserved for full backwards compatibility. <i><b>uicomponent</b></i> uses the built-in <i><b>uicontrol</b></i> whenever possible (for standard Matlab styles), and <i><b>javacomponent</b></i> for all other Java classes.</p><p><i><b>uicomponent</b></i> returns the same two handles that <i><b>javacomponent</b></i> returns (namely, a Java reference handle and a numeric HG handle), modified to include each other&#8217;s properties and handles (yet another undocumented trick that merits a dedicated article). Here are some examples (more can be found in <i><b>uicomponent</b></i>&#8216;s help comment):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'hello'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a regular uicontrol</span>
uicomponent<span style="color: #080;">&#40;</span>hFig, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'hello'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% specify parent</span>
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'jspinner'</span>,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">7</span><span style="color: #080;">&#41;</span>;
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'javax.swing.jslider'</span>,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'myObj'</span><span style="color: #080;">&#41;</span>; 
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'JComboBox'</span>,<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span>,<span style="color: #0000FF;">pi</span>,<span style="color:#A020F0;">'text'</span><span style="color: #080;">&#125;</span>,<span style="color:#A020F0;">'editable'</span>,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Another File Exchange submission which aims to tackle some of <i><b>javacomponent</b></i>&#8216;s limitations is Malcolm Lidierth&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15580"><i><b>JCONTROL</b></i></a>. <i><b>jcontrol</b></i> uses Matlab&#8217;s new object-oriented class approach and has the benefit of returning just a single handle object, which aggregates the handles for both HG container and the contained Java object.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/hgfeval/' rel='bookmark' title='The hgfeval function'>The hgfeval function</a> <small>The semi-documented hgfeval function can be useful for callback chaining - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/javacomponent/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Matlab and the Event Dispatch Thread (EDT)</title><link>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/</link> <comments>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/#comments</comments> <pubDate>Wed, 10 Mar 2010 07:18:58 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></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[Semi-documented function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Matt Whitaker]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1198</guid> <description><![CDATA[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.<pre> </pre>Related posts:<ol><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/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</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><li><a
href='http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome guest blogger Matt Whitaker, with the long awaited EDT article.</i></p><h3 id="EDT">Java Swing&#8217;s Event Dispatch Thread (EDT)<br
/><i>or: why does my GUI foul up?</i></h3><p>Matlab for the most part is a single threaded environment. That is, all commands are executed sequentially along a single execution thread. The main exception to this are the Handle Graphics (GUI) components whose operations execute on the <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html">Java Event Dispatch Thread (EDT)</a>. EDT effects are reflected even in mundane Matlab GUI operations.</p><p>If we execute the code below we will probably see nothing until the loop completes and the figure appears with the text label showing &#8217;10000&#8242;:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">h = <span style="color: #0000FF;">figure</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">for</span> n = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">10000</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>txt,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">int2str</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%for</span></pre></div></div><p>By adding a couple of <b><i>drawnow</i></b> commands we get the figure and text label to render and then we see the count progress to 10000.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">h = <span style="color: #0000FF;">figure</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
<span style="color: #0000FF;">for</span> n = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">10000</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>txt,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">int2str</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%for</span></pre></div></div><p>The <b><i>drawnow</i></b> function allows the EDT queue to be flushed and the pending graphics operations to be evaluated. This will also happen with <b><i>pause</i></b> and several other commands.</p><p>If we want to use Swing (or AWT) components in our user interfaces we need to take this multi-threaded environment into account. The Swing toolkit designers decided to make all the Swing components thread <b><u>un</u></b>-safe in order to decrease their complexity. As a consequence, all access to Swing components should be done from the event dispatch thread (EDT), to ensure the operations are executed sequentially, at the exact order in which they were dispatched. Any action on a Swing component done on another thread (Matlab&#8217;s main processing thread in our case) risks a race-condition or deadlock with the EDT, which could (and often does) result in weird, non-deterministic and non-repetitive behavior – all of which should be avoided in any application which should behave in a precisely deterministic manner.</p><p>In Java, the usual pattern to accomplish EDT dispatching is to create a <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/lang/Runnable.html">Runnable</a> object, encapsulate the GUI code in the <i>run</i> method of the Runnable object, then pass the Runnable object to the static <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/awt/EventQueue.html#invokeLater(java.lang.Runnable)">EventQueue.invokeLater</a> (or <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/awt/EventQueue.html#invokeAndWait(java.lang.Runnable)">EventQueue.invokeAndWait</a> if we need to block operations to get a return value) method.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Runnable</span> runnable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//GUI Code here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003399;">EventQueue</span>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span>runnable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>There are several functions in Matlab that implement this programming pattern for us: <b><i>javaObjectEDT</i></b>, <b><i>javaMethodEDT</i></b>, <b><i>awtinvoke</i></b>, <b><i>awtcreate</i></b> and <b><i>javacomponent</i></b>. <b><i>JavaMethodEDT</i></b> and <b><i>javaObjectEDT</i></b> were introduced in version R2008b (7.7) and are <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/javaobjectedt.html">minimally and only partially documented</a> although they have reasonably complete help comments. The other three are semi-documented (meaning they are unsupported but if you edit or type their m-file you&#8217;ll see a fairly detailed help section), and although there is some overlap in their functionality they are still available.</p><h3 id="javaObjectEDT">javaObjectEDT and javaMethodEDT</h3><p><b><i>javaObjectEDT</i></b> is the the preferred method since R2008b of creating swing components to be used on the EDT. An object created with <b><i>javaObjectEDT</i></b> will have all of its subsequent method calls run on the EDT. This is termed <i>Auto Delegation</i>. Auto-delegation greatly simplifies and increases the readability of code. Note that objects created as a result of method calls may not be implemented on the EDT.</p><p>If you have an existing Java object, you can pass it to <b><i>javaObjectEDT</i></b> at any time &#8211; all its subsequent calls will then onward run on the EDT. Note that this useful functionality is an under-documented <b><i>javaObjectEDT</i></b> feature: it is not mentioned in the main help section but only implied from the example.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a button on the EDT</span>
btn = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% this will run on EDT since btn was javaObjectEDT-created</span>
btn.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Create a button NOT on the EDT</span>
btn2 = javax.<span style="">swing</span>.<span style="">JButton</span>;
<span style="color: #228B22;">% Dangerous! call will run on main Matlab thread</span>
btn2.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button2'</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% modify btn2 so its methods will start running on the EDT</span>
javaObjectEDT<span style="color: #080;">&#40;</span>btn2<span style="color: #080;">&#41;</span>;
btn2.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button2'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The following example shows the use of <b><i>javaObjectEDT</i></b> and <b><i>javaMethodEDT</i></b> in a more complex situation using a JTable:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> tableExample
hFig = <span style="color: #0000FF;">figure</span>;
<span style="color: #0000FF;">drawnow</span>; <span style="color: #228B22;">%need to get figure rendered</span>
&nbsp;
<span style="color: #228B22;">%use Yair's createTable to add a javax.swing.JTable</span>
<span style="color: #228B22;">%http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table</span>
<span style="color: #228B22;">%wrap ceateTable in javaObjectEDT to put the ensuing method calls on the EDT</span>
f = java.<span style="">awt</span>.<span style="">Font</span><span style="color: #080;">&#40;</span>java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Dialog'</span><span style="color: #080;">&#41;</span>,java.<span style="">awt</span>.<span style="">Font</span>.<span style="">PLAIN</span>,<span style="color: #33f;">14</span><span style="color: #080;">&#41;</span>;
headers = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Selected'</span>,<span style="color:#A020F0;">'File'</span>,<span style="color:#A020F0;">'Analysis Routine'</span>,<span style="color:#A020F0;">'Task Status'</span><span style="color: #080;">&#125;</span>;
tbl = javaObjectEDT<span style="color: #080;">&#40;</span>createTable<span style="color: #080;">&#40;</span>hFig,headers,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF;">false</span>,<span style="color:#A020F0;">'Font'</span>,f<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set column 1 to use check boxes and set up a change callback</span>
tbl.<span style="">setCheckBoxEditor</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
jtable = javaObjectEDT<span style="color: #080;">&#40;</span>tbl.<span style="">getTable</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the underlying Java Table. IMPORTANT: we need to put jtable on the EDT</span>
columnModel = javaObjectEDT<span style="color: #080;">&#40;</span>jtable.<span style="">getColumnModel</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%now we can now do direct calls safely on jtable</span>
selectColumn = javaObjectEDT<span style="color: #080;">&#40;</span>columnModel.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
selectColumnCellEditor = selectColumn.<span style="">getCellEditor</span>;
chk = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getComponent'</span>,selectColumnCellEditor<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>chk,<span style="color:#A020F0;">'ItemStateChangedCallback'</span>,@chkChange_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%make column three a combo drop down</span>
analysisTable = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Analysis1'</span>;<span style="color:#A020F0;">'Analysis2'</span>;<span style="color:#A020F0;">'Analysis3'</span><span style="color: #080;">&#125;</span>;
cb = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'com.mathworks.mwswing.MJComboBox'</span>,analysisTable<span style="color: #080;">&#41;</span>;
cb.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
cb.<span style="">setFont</span><span style="color: #080;">&#40;</span>f<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>cb,<span style="color:#A020F0;">'ItemStateChangedCallback'</span>,@cbChange_Callback<span style="color: #080;">&#41;</span>;
editor = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.DefaultCellEditor'</span>,cb<span style="color: #080;">&#41;</span>;
analysisColumn = javaObjectEDT<span style="color: #080;">&#40;</span>columnModel.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
analysisColumn.<span style="">setCellEditor</span><span style="color: #080;">&#40;</span>editor<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set some column with restrictions</span>
selectColumn.<span style="">setMaxWidth</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>;
analysisColumn.<span style="">setPreferredWidth</span><span style="color: #080;">&#40;</span><span style="color: #33f;">300</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set the data</span>
SELECTED = java.<span style="">awt</span>.<span style="">event</span>.<span style="">ItemEvent</span>.<span style="">SELECTED</span>;
tbl.<span style="">setData</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color: #0000FF;">false</span>,<span style="color:#A020F0;">'file1'</span>,<span style="color:#A020F0;">'Analysis2'</span>,<span style="color:#A020F0;">'Analysis2'</span>;<span style="color: #F0F;">...</span>
             <span style="color: #0000FF;">true</span>,<span style="color:#A020F0;">'file2'</span>,<span style="color:#A020F0;">'Analysis3'</span>,<span style="color:#A020F0;">'Analysis3'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
&nbsp;
    <span style="color: #0000FF;">function</span> cbChange_Callback<span style="color: #080;">&#40;</span>src,ev<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        jRow = jtable.<span style="">getSelectedRow</span>;
        stateChange = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getStateChange'</span>,ev<span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">if</span> stateChange == SELECTED
            newData = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getItem'</span>,ev<span style="color: #080;">&#41;</span>;
            model = jtable.<span style="">getModel</span>;
            javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setValueAt'</span>,model,newData,jRow,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%cbChange</span>
&nbsp;
    <span style="color: #0000FF;">function</span> chkChange_Callback<span style="color: #080;">&#40;</span>src,ev<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        chkBox = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getItem'</span>,ev<span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">logical</span><span style="color: #080;">&#40;</span>javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'isSelected'</span>,chkBox<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
            beep; <span style="color: #228B22;">%put useful code here</span>
        <span style="color: #0000FF;">else</span>
            beep;
            <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.1</span><span style="color: #080;">&#41;</span>
            beep; <span style="color: #228B22;">%put useful code here</span>
        <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkChange_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%tableExample</span></pre></div></div><p>If you are running Matlab R2008a or later, <b><i>javacomponent</i></b> uses the <b><i>javaObjectEDT</i></b> function to create the returned objects so you do not have to do anything further to these objects to have their calls dispatched on the EDT. Users need to take care that objects added directly to the components created by <b><i>javacomponent</i></b> are on the EDT as well as specialized sub-components (e.g. CellRenderers and CellEditors). The overhead of calling <b><i>javaMethodEDT</i></b> is fairly small so if in doubt, use it.</p><p><b><i>javaObjectEDT</i></b> and its kin first appeared in R2008a, although they only became supported in R2008b. Unfortunately, using them on R2008a sometimes causes hangs and all sorts of other mis-behaviors. This problem was fixed in the R2008b release, when <b><i>javaObjectEDT</i></b> became a fully-supported function. The problem with using <b><i>javaObjectEDT</i></b> in our application is that if it ever runs on an R2008a platform it might hang! (on Matlab release R2007b and earlier we will get an informative message saying that the <b><i>javaObjectEDT</i></b> function does not exist)</p><p>For this reason, I am using the following method in my projects:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> result = javaObjEDT<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%Placeholder of Matlab's buggy javaObjectEDT function on R2008a</span>
&nbsp;
<span style="color: #228B22;">% Programmed by Yair M. Altman: altmany(at)gmail.com</span>
<span style="color: #228B22;">% $Revision: 1.2 $  $Date: 2009/01/25 11:31:08 $</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
      <span style="color: #0000FF;">try</span>
          result = <span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
      <span style="color: #0000FF;">catch</span>
          result = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
      <span style="color: #0000FF;">end</span>
      v = <span style="color: #0000FF;">version</span>;
      <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">str2double</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt; <span style="color: #33f;">7.6</span>
          result = <span style="color: #0000FF;">builtin</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javaObjectEDT'</span>,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
      <span style="color: #228B22;">% never mind</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that <b><i>javaMethodEDT</i></b> has the method name as its first input argument, and the object name or reference as its second arg. This is inconsistent with many other Matlab/Java functions, which normally accept the target object as the first argument (compare: <b><i>invoke</i></b>, <b><i>awtinvoke</i></b>, <b><i>notify</i></b> etc.). It also means that we cannot use the familiar <i>obj.javaMethodEDT(methodName)</i> format.</p><p>One final note: when <b><i>javaObjectEDT</i></b> and <b><i>javaMethodEDT</i></b> first appeared in R2008a, they were complemented by the <b><i>javaObjectMT</i></b> and <b><i>javaMethodMT</i></b> functions, which create and delegate Java objects on the main Matlab computational thread. Their internal documentation says that there are cases when execution must occur on the MT rather than EDT, although I am personally not aware of any such case.</p><h3 id="awtcreate">awtcreate and awtinvoke</h3><p>For users with versions prior to R2008b the user must use the <b><i>awtcreate</i></b> function to create objects on the EDT. One huge disadvantage of this older function is that if you have to pass java objects in the parameter list you must use the very cumbersome <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html#wp276">JNI style notation</a>. For example, for the simple task of setting a button label, one has to use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">btn = awtcreate<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>btn,<span style="color:#A020F0;">'setText(Ljava/lang/String;)'</span>,<span style="color:#A020F0;">'click me'</span><span style="color: #080;">&#41;</span></pre></div></div><p>The other disadvantage is that creating the object using <b><i>awtcreate</i></b> does not ensure that its subsequent method calls will be executed on the EDT. The <b><i>awtinvoke</i></b> function must be used for each call.</p><p>Also, both <b><i>awtcreate</i></b> and <b><i>awtinvoke</i></b> have some limitations due to bugs in the private <b><i>parseJavaSignature</i></b> function (for example, invoking methods which accept a java.lang.Object) which forces one to use the direct call to the method, using the main Matlab thread. This can result in the undesired effects described above. In this situation the best workaround is to call <i><b>pause</b>(0.01)</i> to allow the event queue to clear.</p><p>Versions of <b><i>javacomponent</i></b> earlier than R2008a use <b><i>awtcreate</i></b> and objects created by these versions must have their subsequent methods called by <b><i>awtinvoke</i></b> to be used on the EDT.</p><p>A very rare <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388">CSSM thread</a> discusses the usage of <b><i>awtcreate</i></b> and <b><i>awtinvoke</i></b> with some very interesting remarks by MathWorks personnel.</p><p>There is an interesting option in <b><i>awtinvoke</i></b> that was not carried over into the newer <b><i>javaMethodEDT</i></b>. This option allows the user to pass a function handle in the argument list along with its parameters. This option creates an undocumented com.mathworks.jmi.Callback object that has a delayed callback. The delayed callback is dispatched on the EDT so that it will be called once the java method used in <b><i>awtinvoke</i></b> is finished. Note that the actual function will still execute on the main Matlab thread the delayed callback will just control when it is called. However this may be useful at times. It is possible to put this functionality into a separate function we can call to delay execution until the event queue is cleared.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">%CALLBACKONEDTQUEUE will place a callback on the EDT to asynchronously</span>
<span style="color: #228B22;">%run a function.</span>
<span style="color: #228B22;">%CALLBBACKONEDTQUEUE(FCN) will run function handle FCN once all previous</span>
<span style="color: #228B22;">%methods dispatched to the EDT have completed.</span>
<span style="color: #228B22;">%CALLBBACKONEDTQUEUE(FCN,ARG1,ARG2,...) ill run function handle FCN with</span>
<span style="color: #228B22;">%arguments ARG1,ARG2...once all previous methods dispatched to the EDT</span>
<span style="color: #228B22;">%have completed.</span>
<span style="color: #228B22;">%Note that the function is still executing on the main Matlab thread. This</span>
<span style="color: #228B22;">%function just delays when it will be called.</span>
<span style="color: #0000FF;">function</span> callbackOnEDTQueue<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    validateattributes<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'function_handle'</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    callbackObj = handle<span style="color: #080;">&#40;</span>com.<span style="">mathworks</span>.<span style="">jmi</span>.<span style="">Callback</span>,<span style="color:#A020F0;">'callbackProperties'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>callbackObj,<span style="color:#A020F0;">'delayedCallback'</span>,<span style="color: #080;">&#123;</span>@cbEval,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    callbackObj.<span style="">postCallback</span>;
&nbsp;
    <span style="color: #0000FF;">function</span> cbEval<span style="color: #080;">&#40;</span>src,evt,args<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%cbEval</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%callbackOnEDTQueue</span></pre></div></div><p><pre> </pre>Related posts:<ol><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/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</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><li><a
href='http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/feed/</wfw:commentRss> <slash:comments>21</slash:comments> </item> <item><title>Inactive Control Tooltips &amp; Event Chaining</title><link>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/</link> <comments>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/#comments</comments> <pubDate>Wed, 24 Feb 2010 22:34:46 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1153</guid> <description><![CDATA[Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks.<pre> </pre>Related posts:<ol><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/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/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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again, I welcome guest blogger Matt Whitaker, who continues his series of articles.</i></p><p>In my last post I explored some <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">tips on tooltips</a>. One of these tips involved displaying tooltips on disabled uicontrols. I explained that displaying tooltips on inactive controls is problematic since Matlab appears to intercept mouse events to these inactive controls, so even setting the tooltip on the underlying Java object will not work: The java object appears not to receive the mouse-hover event and therefore does not &#8220;know&#8221; that it&#8217;s time to display the tooltip.</p><p>When Yair and I deliberated this issue, he pointed me to his <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/#comment-1173">comment on a previous article</a> showing an undocumented Java technique (Java also has some&#8230;) for forcing a tooltip to appear using the <b>ActionMap</b> of the uicontrol&#8217;s underlying Java object to get at a <i>postTip</i> action. We discussed using a <b>WindowButtonMotionFcn</b> callback to see if the mouse was above the inactive control, then triggering the forced tooltip display. Yair then went on to remind me and I quote: <i>&#8220;you&#8217;ll need to chain existing <b>WindowButtonMotionFcn</b> callbacks and take into account ModeManagers that override them.&#8221;</i></p><p>Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!</p><p>The Image Processing Toolbox has the nice pair of <b><i>iptaddcallback</i></b> and <b><i>iptremovecallback</i></b> functions that largely handle these issues. But  for general Matlab, there seemed to be no alternative until I remembered that events trigger callbacks. I decided to use a listener for the <i>WindowButtonMotion</i> event to detect the mouse motion. <a
target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Event_Listener">Event listeners were briefly explained two weeks ago</a> and deserve a dedicated future article. The advantage of using an event listener is that we don&#8217;t disturb any existing <b>WindowButtonMotionFcn</b> callback. We still need to be somewhat careful that our listeners don&#8217;t do conflicting things, but it&#8217;s a lot easier than trying to manage everything through the single <b>WindowButtonMotionFcn</b>.</p><p>A demonstration of this appears below with some comments following (note that this code uses the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> inactiveBtnToolTip
  <span style="color: #228B22;">%Illustrates how to make a tooltip appear on an inactive control</span>
  h = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'WindowButtonMotionFcn'</span>,@windowMotion,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">400</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  col = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'color'</span><span style="color: #080;">&#41;</span>;
  lbl = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">160</span>,<span style="color: #33f;">120</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Background'</span>,col, <span style="color:#A020F0;">'HorizontalAlignment'</span>,<span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
  btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'inactive'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'check'</span>, <span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Enable button tooltip'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Callback'</span>,@chkTooltipEnable, <span style="color:#A020F0;">'Value'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">180</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Background'</span>,col<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">drawnow</span>;
&nbsp;
  <span style="color: #228B22;">%create the tooltip and postTip action</span>
  jBtn = findjobj<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>;
  import java.<span style="">awt</span>.<span style="">event</span>.<span style="">ActionEvent</span>;
  javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jBtn,<span style="color:#A020F0;">'This button is inactive'</span><span style="color: #080;">&#41;</span>;
  actionMap = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getActionMap'</span>,jBtn<span style="color: #080;">&#41;</span>;
  action = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'get'</span>,actionMap,<span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
  actionEvent = ActionEvent<span style="color: #080;">&#40;</span>jBtn, ActionEvent.<span style="">ACTION_PERFORMED</span>, <span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%get the extents plus 2 pixels of the control to compare to the mouse position</span>
  btnPos = getpixelposition<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>+<span style="color: #080;">&#91;</span>-<span style="color: #33f;">2</span>,-<span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span>; <span style="color: #228B22;">%give a little band around the control</span>
  left = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  right = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  btm = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
  top =  <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% add a listener on mouse movement events</span>
  tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%tooltip manager</span>
  pointListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'WindowButtonMotionEvent'</span>,@figMouseMove<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%inControl is a flag to prevent multiple triggers of the postTip action</span>
  <span style="color: #228B22;">%while mouse remains in the button</span>
  inControl = <span style="color: #0000FF;">false</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> figMouseMove<span style="color: #080;">&#40;</span>src,evtData<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
    <span style="color: #228B22;">%get the current point</span>
    cPoint = evtData.<span style="">CurrentPoint</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &gt;= left <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &lt;= right <span style="color: #F0F;">&amp;&amp;...</span>
       <span style="">cPoint</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &gt;= btm  <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &lt;= top
&nbsp;
      <span style="color: #0000FF;">if</span> ~inControl <span style="color: #228B22;">%we just entered</span>
        inControl = <span style="color: #0000FF;">true</span>;
        action.<span style="">actionPerformed</span><span style="color: #080;">&#40;</span>actionEvent<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%show the tooltip</span>
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">if</span> inControl <span style="color: #228B22;">%we just existed</span>
        inControl = <span style="color: #0000FF;">false</span>;
        <span style="color: #228B22;">%toggle to make it disappear when leaving button</span>
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%gpMouseMove</span>
&nbsp;
  <span style="color: #0000FF;">function</span> windowMotion<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">%illustrate that we can still do a regular window button motion callback</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>lbl,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Mouse position: %d, %d'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'CurrentPoint'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%windowMotion</span>
&nbsp;
  <span style="color: #0000FF;">function</span> chkTooltipEnable<span style="color: #080;">&#40;</span>src,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>src,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkTooltipEnable</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%inactiveBtnToolTip</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 218px"><img
alt="Tooltip on an inactive button" src="http://undocumentedmatlab.com/images/tooltip4.png" title="Tooltip on an inactive button" width="208" height="255" /><p
class="wp-caption-text">Tooltip on an inactive button</p></div></center></p><p>Comments on the code:</p><ol><li>The code illustrates that we can successfully add an additional listener to listen for mouse motion events while still carrying out the original <b>WindowButtonMotionFcn</b> callback. This makes chaining callbacks much easier.</li><li>The <b><i>handle.listener</i></b> object has an <b>Enable</b> property that we can use to temporarily turn the listener on and off. This can be seen in the <i>chkTooltipEnable()</i> callback for the check box in the code above. If we wanted to permanently remove the listener we would simply use <i><b>delete</b>(pointListener)</i>. Note that <b><i>addlistener</i></b> adds a hidden property to the object being listened to, so that the listener is tied to the object&#8217;s lifecycle. If you create a listener directly using <b><i>handle.listener</i></b> you are responsible for it&#8217;s disposition. Unfortunately, <b><i>addlistener</i></b> fails for HG handles on pre-R2009 Matlab releases, so we use <b><i>handle.listener</i></b> directly.</li><li>The code illustrates a good practice when tracking rapidly firing events like mouse movement of handling reentry into the callback while it is still processing a previous callback. Here we use a flag called inControl to prevent the <i>postTip</i> action being continuously fired while the mouse remains in the control.</li><li>I was unable to determine if there is any corresponding action for the <i>postTip</i> to dismiss tips so I resorted to using the ToolTipManager to toggle its own <b>Enable</b> property to cleanly hide the tooltip as the mouse leaves the control.</li></ol><p>Each Matlab callback has an associated event with it. Some of the ones that might be immediately useful at the figure-level are <b>WindowButtonDown</b>, <b>WindowButtonUp</b>, <b>WindowKeyPress</b>, and <b>WindowKeyRelease</b>. They can all be accessed through <b><i>handle.listener</i></b> or <b><i>addlistener</i></b> as in the code above.</p><p>Unfortunately, events do not always have names that directly correspond to the callback names. In order to see the list of available events for a particular Matlab object, use the following code, which relies on another undocumented function &#8211; <b><i>classhandle</i></b>. Here we list the events for <b><i>gcf</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Events'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'SerializeEvent'</span>
    <span style="color:#A020F0;">'FigureUpdateEvent'</span>
    <span style="color:#A020F0;">'ResizeEvent'</span>
    <span style="color:#A020F0;">'WindowKeyReleaseEvent'</span>
    <span style="color:#A020F0;">'WindowKeyPressEvent'</span>
    <span style="color:#A020F0;">'WindowButtonUpEvent'</span>
    <span style="color:#A020F0;">'WindowButtonDownEvent'</span>
    <span style="color:#A020F0;">'WindowButtonMotionEvent'</span>
    <span style="color:#A020F0;">'WindowPostChangeEvent'</span>
    <span style="color:#A020F0;">'WindowPreChangeEvent'</span></pre></div></div><p>Note that I have made extensive use of the <b><i>javaMethodEDT</b></i> function to execute Java methods that affect swing components on Swing&#8217;s Event Dispatch Thread. I plan to write about this and related functions in my next article.</p><p><pre> </pre>Related posts:<ol><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/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/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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Additional uicontrol tooltip hacks</title><link>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/</link> <comments>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/#comments</comments> <pubDate>Wed, 17 Feb 2010 17:25:52 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1114</guid> <description><![CDATA[Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I&#8217;d like to welcome guest blogger Matthew Whitaker. Today Matt will discuss uicontrol tooltips hacks as the first of several posts that follow a logical thread culminating in the long promised article on the EDT.</i><br
/> <br/></p><p>A while back Yair wrote a cool article, <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">Spicing up Matlab uicontrol tooltips</a>, describing use of HTML formatting and images in <b><i>uicontrol</i></b> tooltips. I want to share some limitations I&#8217;ve seen with tooltips and their solution using the Matlab control&#8217;s underlying Java object.</p><h3 id="disabled-controls">Situation 1: Displaying a tooltip on disabled controls</h3><p>One issue with the stock Matlab <b><i>uicontrol</i></b> tooltips is that if you turn the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol_props.html#bqxoiky">uicontrol&#8217;s <b>Enable</b> property</a> to &#8216;inactive&#8217; or &#8216;off&#8217;, its tooltip no longer displays. This is the behavior that we normally want, but occasionally we wish to display a tooltip on a disabled control, for example, to explain why the control is disabled.</p><p>You can use the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>findjobj</i></b> utility</a> to find the Java handle for the <b><i>uicontrol</i></b>. This handle can then be used to set the tooltip text. The tooltip will display if you disable the control using its Java handle&#8217;s <b>Enabled</b> property rather than the Matlab handle&#8217;s <b>Enable</b> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hButton = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jButton= findjobj<span style="color: #080;">&#40;</span>hButton<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'Enabled'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'This is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>As customary for Java objects, its properties can also be set using their corresponding accessor methods:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,jButton,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jButton,<span style="color:#A020F0;">'Button is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 262px"><img
alt="tooltip on a disabled uicontrol" src="http://UndocumentedMatlab.com/images/tooltip1.png" title="tooltip on a disabled uicontrol" width="252" height="46" /><p
class="wp-caption-text">tooltip on a disabled uicontrol</p></div></center></p><p>Unfortunately, this hack does not work for &#8216;inactive&#8217; controls. There is no direct Java analogy for inactive controls &#8211; it&#8217;s a Matlab extension. It appears that Matlab somehow intercepts the mouse events associated with inactive controls. My next post will show how event callback can be used to display tooltips for inactive controls.</p><p>As an alternative for inactive edit-box controls, we can simulate the inactive behavior by setting the Java object&#8217;s <b>Editable</b> property (or by using its <i>setEditable()</i> accessor method), then setting the tooltip. Note that the extremely-useful Java <b>Editable</b> property is unavailable in the Matlab handle, for some inexplicable reason:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'Editable'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'Text is inactive for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 242px"><img
alt="tooltip on a non-editable editbox" src="http://UndocumentedMatlab.com/images/tooltip2.png" title="tooltip on a non-editable editbox" width="232" height="50" /><p
class="wp-caption-text">tooltip on a non-editable editbox</p></div></center></p><h3 id="truncated_text">Situation 2: Displaying a tooltip on truncated text</h3><p>If we want to conditionally display a tooltip for an editbox <b><i>uicontrol</i></b> when the text exceeds the control&#8217;s width, we can use the <b>TipWhenTruncatedEnabled</b> property (or its corresponding <i>setTipWhenTruncatedEnabled()</i> method). This will display a tooltip with the editbox contents if the string is shown truncated. This saves the user having to scroll through the control to see its contents. I often use this for edit controls that may contain long path names:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</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.1</span>,<span style="color: #33f;">0.8</span>,<span style="color: #33f;">0.4</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Too Short'</span><span style="color: #080;">&#41;</span>;
hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</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.1</span>,<span style="color: #33f;">0.7</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Long Enough to Display a Tool Tip'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox1 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jEditbox2 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</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>jEditbox1,<span style="color:#A020F0;">'TipWhenTruncatedEnabled'</span>,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% property-based alternative</span>
javaMethod<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setTipWhenTruncatedEnabled'</span>,jEditbox2,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% method-based alternative</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 260px"><img
alt="TipWhenTruncatedEnabled tooltip" src="http://UndocumentedMatlab.com/images/tooltip3.png" title="TipWhenTruncatedEnabled tooltip" width="250" height="100" /><p
class="wp-caption-text">TipWhenTruncatedEnabled tooltip</p></div></center></p><p>The <b>TipWhenTruncatedEnabled</b> property property is also available for multi-line editboxes, but has (obviously) no effect when scrollbars are present. Also note that setting the <b>TipWhenTruncatedEnabled</b> property to true overrides any previous tooltip that might have been set for the editbox.</p><p>Finally, note that the <b>TipWhenTruncatedEnabled</b> property can also be set for the editbox component of popup-menu (aka drop-down) controls, <u>after</u> they have been set to be editable using their Java <b>Editable</b> property (note that both properties are false by default for Matlab uicontrols). In the following screenshot, the drop-down&#8217;s editbox component contained an HTML snippet, that is shown unformatted within the edit-box and HTML-formatted in the de-truncated tooltip:</p><p><center><div
class="wp-caption aligncenter" style="width: 125px"><img
alt="de-truncated HTML-format tooltip" src="http://UndocumentedMatlab.com/images/popup_TipWhenTruncatedEnabled2.png" title="de-truncated HTML-format tooltip" width="115" height="72" /><p
class="wp-caption-text">de-truncated HTML-format tooltip</p></div></center></p><h3 id="tooltip_timing">Situation 3: Controlling tooltip timing</h3><p>As you have probably noticed, there is a slight delay between the time your mouse enters the control and when the tooltip actually appears. If you display a tooltip over a control for sufficiently long the tooltip will then disappear. Sometimes the default delays are too slow or fast for your application. These times can be controlled through the <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/ToolTipManager.html">javax.swing.ToolTipManager</a>. The ToolTipManager sets these parameters globally (including for your Matlab desktop components), but they are not persistent between sessions.</p><p>Some examples using the ToolTipManager:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is a button.'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is editable text'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%static method to get ToolTipManager object</span>
initialDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getInitialDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before display in milliseconds (=750 on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set tooltips to appear immediately</span>
&nbsp;
dismissDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getDismissDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before the tooltip disappears (=10000 (10 sec) on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,<span style="color: #33f;">2000</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set the dismiss delay to 2 seconds</span>
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%turns off all tooltips in system (including the Matlab desktop)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,initialDelay<span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,dismissDelay<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that I have made extensive use of the undocumented built-in <b><i>javaMethodEDT</i></b> function to execute Java methods that affect Swing components on the Swing Event Dispatch Thread (EDT). I plan to write about EDT following my next post on event callback chaining.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Continuous slider callback</title><link>http://undocumentedmatlab.com/blog/continuous-slider-callback/</link> <comments>http://undocumentedmatlab.com/blog/continuous-slider-callback/#comments</comments> <pubDate>Mon, 08 Feb 2010 09:13:39 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1052</guid> <description><![CDATA[Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab&#8217;s <a
target="_blank" rel="nofollow" href="http://blinkdagger.com/matlab/matlab-gui-tutorial-slider/">slider uicontrol</a> invokes the user callback only when the mouse button is released, and not continuously while the slider&#8217;s thumb is dragged. This functionality was again referred-to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/272224">yesterday</a>, and I decided it merits a dedicated post.</p><p>There are three distinct simple ways to achieve continuous callbacks:</p><h3 id="Java_Callbacks">Using Java callbacks</h3><p>As explained in an earlier <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">article</a>, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders&#8217; underlying Java objects, which are really not <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">JSliders</a> but <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#operation">JScrollBars</a>, have an <b>AdjustmentValueChangedCallback</b> property that is useful for our purposes and is accessible using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj utility</i></b></a>. Simply download <b><i>FindJObj</i></b> from the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">File Exchange</a>, and then:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSlider = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'slider'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jScrollBar = findjobj<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
jScrollBar.<span style="">AdjustmentValueChangedCallback</span> = @myCbFcn;
<span style="color: #228B22;">% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)</span></pre></div></div><p>Where <i>myCbFcn</i> is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider&#8217;s thumb is dragged.</p><h3 id="Event_Listener">Using an event listener</h3><p>An alternative to the Java route is to use Matlab&#8217;s undocumented <b><i>handle.listener</i></b> function to listen to the slider&#8217;s <b>Action</b> event, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSlider,<span style="color:#A020F0;">'ActionEvent'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>This alternative is used by Matlab&#8217;s own <b><i>imscrollpanel</i></b> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isJavaFigure
   <span style="color: #228B22;">% Must use these ActionEvents to get continuous events fired as slider</span>
   <span style="color: #228B22;">% thumb is dragged. Regular callbacks on sliders give only one event</span>
   <span style="color: #228B22;">% when the thumb is released.</span>
   hSliderHorListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>;
   hSliderVerListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollVertical<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hScrollpanel,<span style="color:#A020F0;">'sliderListeners'</span>,<span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span>hSliderHorListener hSliderVerListener<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Unfortunately, the event route is only available with Java Figures,</span>
   <span style="color: #228B22;">% so platforms without Java Figure support get discrete events only</span>
   <span style="color: #228B22;">% when the mouse is released from dragging the slider thumb.</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color:#A020F0;">'callback'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color:#A020F0;">'callback'</span>,@scrollVertical<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="Property_Listener">Using a property listener</h3><p>The <b><i>handle.listener</i></b> function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following <b>Value</b> property updates, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hhSlider = handle<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhSlider,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhSlider,hProp,<span style="color:#A020F0;">'PropertyPostSet'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>In addition to &#8216;PropertyPostSet&#8217;, we could also listen on &#8216;PropertyPreSet&#8217;, which is triggered immediately <u>before</u> the property is modified. There are also corresponding &#8216;*Get&#8217; options. In relatively old Matlab releases (I believe R2007b and earlier, but I&#8217;m not certain), the option names were simply &#8216;PostSet&#8217;, &#8216;PreSet&#8217; etc., without the &#8216;Property&#8217; prefix.</p><p>Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/continuous-slider-callback/feed/</wfw:commentRss> <slash:comments>28</slash:comments> </item> <item><title>Customizing listbox &amp; editbox scrollbars</title><link>http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/</link> <comments>http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/#comments</comments> <pubDate>Mon, 01 Feb 2010 17:40:03 +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[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=994</guid> <description><![CDATA[Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized.<pre> </pre>Related posts:<ol><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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='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>A few days ago, a CSSM forum reader asked how to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/271023">modify Matlab&#8217;s listbox scrollbars</a>. Another user asked how to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/271303">configure line-wrapping</a>. I thought this is a good opportunity to describe how listbox and editbox scrollbars can be customized. The timing is particularly opportune, after I have recently described how the Matlab <a
target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/">Editbox can be customized</a> by accessing its underlying Java object using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>.</p><p>Both the listbox and the multi-line editbox uicontrols share a similar design: a multi-line Java control embedded within a <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JViewport.html">JViewport</a> within a <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JScrollPane.html">JScrollPane</a> (note that for historical reasons, the Java view-port class is called JView<b>p</b>ort rather than the more standard <a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/CamelCase">camel-cased</a> JView<b>P</b>ort). In addition to the view-port, the containing scroll-pane also contains two scrollbars (horizontal and vertical), as expected from <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html">standard Java scroll-panes</a>.</p><p><center><div
class="wp-caption aligncenter" style="width: 266px"><img
alt="JScrollPane components" src="http://undocumentedmatlab.com/images/JScrollPane.gif" title="JScrollPane components" width="256" height="248" /><p
class="wp-caption-text">JScrollPane components</p></div></center></p><h3 id="scrollbarPolicies">Scrollbar policies</h3><p>Control of the scroll-pane&#8217;s scrollbar behavior is done via the JScrollPane&#8217;s <b>VerticalScrollBarPolicy</b> and <b>HorizontalScrollBarPolicy</b> properties.</p><p><b>VerticalScrollBarPolicy</b> accepts the self-explanatory values of:<ul><li>VERTICAL_SCROLLBAR_ALWAYS (=22)</li><li>VERTICAL_SCROLLBAR_NEVER (=21)</li><li>and VERTICAL_SCROLLBAR_AS_NEEDED (=20)</li></ul><p><b>HorizontalScrollBarPolicy</b> accepts:<ul><li>HORIZONTAL_SCROLLBAR_ALWAYS (=32)</li><li>HORIZONTAL_SCROLLBAR_NEVER (=31)</li><li>and HORIZONTAL_SCROLLBAR_AS_NEEDED (=30)</li></ul><p>All these properties are static enumerated constants that can be referred using either their Java notation (e.g., JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) or their equivalent numeric values. Using the non-numeric format is better, since it is more readable and the numeric values may change, but the choice is yours.</p><p>By default, Matlab implements a <b>VerticalScrollBarPolicy</b> of VERTICAL_SCROLLBAR_ALWAYS for sufficiently tall uicontrols (&gt;20-25 pixels, which practically means always) and VERTICAL_SCROLLBAR_NEVER for shorter uicontrols.</p><p>For the horizontal scrollbar, Matlab implements a <b>HorizontalScrollBarPolicy</b> of HORIZONTAL_SCROLLBAR_NEVER for all editboxes and for narrow listboxes (&lt;35 pixels), and HORIZONTAL_SCROLLBAR_AS_NEEDED for wide listboxes.</p><p>These settings are generally satisfactory. However, in some cases users may wish to modify the settings. For example, the default <b>VerticalScrollBarPolicy</b> setting of VERTICAL_SCROLLBAR_ALWAYS causes the vertical scrollbar to appear even when unneeded (the entire editbox content is visible). Also, we may wish to have a horizontal scrollbar on narrow listboxes and editboxes, something that the standard HORIZONTAL_SCROLLBAR_NEVER prevents. In both cases, a *_SCROLLBAR_AS_NEEDED policy might be more appropriate.</p><p>To modify these settings, we simply need to get the uicontrol&#8217;s underlying Java reference handle (using the <b><i>FindJObj</i></b> utility), and modify the appropriate property. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a multi-line (Max&gt;1) editbox uicontrol</span>
hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'max'</span>,<span style="color: #33f;">5</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the Java scroll-pane container reference</span>
jScrollPane = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Modify the scroll-pane's scrollbar policies</span>
<span style="color: #228B22;">% (note the equivalent alternative methods used below)</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'VerticalScrollBarPolicy'</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED</span>
jScrollPane.<span style="">setHorizontalScrollBarPolicy</span><span style="color: #080;">&#40;</span><span style="color: #33f;">30</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)" src="http://UndocumentedMatlab.com/images/editbox7a.png" title="default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)" width="75" height="60" /><p
class="wp-caption-text">default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)</p></div><div
class="wp-caption aligncenter" style="width: 230px"><img
alt="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" src="http://UndocumentedMatlab.com/images/editbox7b.png" title="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" width="75" height="60" /> &nbsp;&nbsp;&nbsp; <img
alt="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" src="http://UndocumentedMatlab.com/images/editbox7c.png" title="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" width="75" height="60" /><p
class="wp-caption-text">non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)</p></div></center></p><p>Note that updating the uicontrol handle (hEditbox)&#8217;s <b>Position</b> property has the side-effect of automatically reverting the scrollbar policies to their default values (HORIZONTAL_SCROLLBAR_NEVER and VERTICAL_SCROLLBAR_ALWAYS/NEVER). This also happens whenever the uicontrol is resized interactively (by resizing its container figure window, for example). It is therefore advisable to set jScrollPane&#8217;s <b>ComponentResizedCallback</b> property to &#8220;unrevert&#8221; the policies:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">cbStr = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'set(gcbo,'</span><span style="color:#A020F0;">'VerticalScrollBarPolicy'</span><span style="color:#A020F0;">',%d)'</span>, <span style="color: #F0F;">...</span>
                <span style="">jScrollPane</span>.<span style="">VERTICAL_SCROLLBAR_AS_NEEDED</span><span style="color: #080;">&#41;</span>;
hjScrollPane = handle<span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hjScrollPane,<span style="color:#A020F0;">'ComponentResizedCallback'</span>,cbStr<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="lineWrap">Line-wrapping</h3><p>By default, line-wrapping is turned on, effectively disabling horizontal scrolling (which is why Matlab set the <b>HorizontalScrollBarPolicy</b> to HORIZONTAL_SCROLLBAR_NEVER. However, in some cases it may be more useful to turn line-wrapping off and horizontal scrolling on using the TextArea&#8217;s <i>setWrapping()</i> method. Here&#8217;s a usage example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jViewPort = jScrollPane.<span style="">getViewport</span>;
jEditbox = jViewPort.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setWrapping</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% do *NOT* use set(...)!!!</span>
newPolicy = jScrollPane.<span style="">HORIZONTAL_SCROLLBAR_AS_NEEDED</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'HorizontalScrollBarPolicy'</span>,newPolicy<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 150px"><img
alt="multi-line editbox with wrapping on" src="http://UndocumentedMatlab.com/images/editbox11a.png" title="multi-line editbox with wrapping on" width="140" height="60" /><p
class="wp-caption-text">multi-line editbox with wrapping on</p></div><div
class="wp-caption aligncenter" style="width: 150px"><img
alt="multi-line editbox with wrapping off" src="http://UndocumentedMatlab.com/images/editbox11b.png" title="multi-line editbox with wrapping off" width="140" height="60" /><p
class="wp-caption-text">multi-line editbox with wrapping off</p></div></center></p><p>Notes:</p><ol><li><i>setWrapping()</i> only works for the default EditorKit, and fails for <a
target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#EditorKit">HTMLEditorKit</a> – This is due to HTML&#8217;s inherent wrapping behavior, as can easily be seen in any browser webpage.</li><li>while <i>setWrapping()</i> may seem like a regular setter method for a <b>Wrapping</b> property, in reality it is not. Actually, <b><i>set</i></b>(jEditbox,&#8217;wrapping&#8217;,flag) may crash Matlab. So, always use the <i>setWrapping(flag)</i> method variant, which is entirely safe.</li></ol><p><pre> </pre>Related posts:<ol><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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='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/customizing-listbox-editbox-scrollbars/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-09 04:31:27 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/ui-controls/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.647s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328787085; expires=Fri, 08-Feb-2013 11:31:25 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Thu, 09 Feb 2012 11:31:27 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Thu, 09 Feb 2012 12:31:27 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               3d44cf5ae046ad050a36d2ac0843b205
Content-Encoding:   gzip
-->
