<?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; FindJObj</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/findjobj/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 02 Feb 2012 00:24:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>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>Customizing uicontrol border</title><link>http://undocumentedmatlab.com/blog/customizing-uicontrol-border/</link> <comments>http://undocumentedmatlab.com/blog/customizing-uicontrol-border/#comments</comments> <pubDate>Sun, 31 Oct 2010 19:05:17 +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[FindJObj]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1947</guid> <description><![CDATA[Matlab uicontrol borders can easily be modified - this article shows how<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/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li><li><a
href='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/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>Today, a CSSM user <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/295214">posted</a> the question how to modify the border of Matlab editboxes. There is of course no documented way to do this in plain Matlab. However, this is so easy to do using the edit-box&#8217;s undocumented underlying Java, that I decided to skip my regular weekly article schedule and post an out-of-band explanation.</p><p>As the CSSM poster correctly figured out, we first need to get the uicontrol&#8217;s underlying Java reference. This is done using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>. The next step is to get the Java object&#8217;s <b>Border</b> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; hEdit = <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: #080;">&#41;</span>;  <span style="color: #228B22;">% create Matlab uicontrol</span>
&nbsp;
&gt;&gt; jEdit = findjobj<span style="color: #080;">&#40;</span>hEdit<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% get underlying Java reference</span>
jEdit =
	javahandle_withcallbacks.<span style="">com</span>.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">EditTextPeer</span>$hgTextField
&nbsp;
&gt;&gt; jEdit.<span style="">Border</span>  <span style="color: #228B22;">% check the control's border</span>
<span style="color: #0000FF;">ans</span> =
com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">XPStyle</span>$XPFillBorder@e5a137
&nbsp;
&gt;&gt; jEdit.<span style="">Border</span>.<span style="color: #0000FF;">get</span>
	BorderOpaque = on
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	LineColor = <span style="color: #080;">&#91;</span><span style="color: #33f;">0.498039</span> <span style="color: #33f;">0.615686</span> <span style="color: #33f;">0.72549</span><span style="color: #080;">&#93;</span>
	RoundedCorners = off
	Thickness = <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
	<span style="color: #F0F;">...</span></pre></div></div><p>Unfortunately, as the CSSM poster has discovered, this <b>Border</b> property cannot be modified. Such an object is called <i>immutable</i> in Java; another common example is the <b>Font</b> property.</p><p>However, we can easily replace the object&#8217;s <b>Border</b> with a custom-made border, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">lineColor = 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: #228B22;">% =red</span>
thickness = <span style="color: #33f;">3</span>;  <span style="color: #228B22;">% pixels</span>
roundedCorners = <span style="color: #0000FF;">true</span>;
newBorder = javax.<span style="">swing</span>.<span style="">border</span>.<span style="">LineBorder</span><span style="color: #080;">&#40;</span>lineColor,thickness,roundedCorners<span style="color: #080;">&#41;</span>;
jEdit.<span style="">Border</span> = newBorder;
jEdit.<span style="">repaint</span>;  <span style="color: #228B22;">% redraw the modified control</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 100px"><img
alt="editbox with regular border" src="http://UndocumentedMatlab.com/images/editbox1.png" title="editbox with regular border" width="90" height="30" /><p
class="wp-caption-text">editbox with regular border</p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 100px"><img
alt="editbox with custom border" src="http://UndocumentedMatlab.com/images/editbox4a.png" title="editbox with custom border" width="90" height="30" /><p
class="wp-caption-text">editbox with custom border</p></div></center></p><p>Much more complex and interesting borders can be created in much the same way. Interested readers are referred to the <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/border.html">official documentation of Java Borders</a> or any decent Swing textbook.</p><p>We can use the same technique to remove the uicontrol&#8217;s borders altogether:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jEdit.<span style="">Border</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 100px"><img
alt="editbox with no border" src="http://UndocumentedMatlab.com/images/editbox3.png" title="editbox with no border" width="90" height="30" /><p
class="wp-caption-text">editbox with no border</p></div></center></p><p>Setting the border as shown above works in much the same manner for all Java uicontrols. Simply use <i><b>findjobj</b></i> to get the Java reference and then update its <b>Border</b> property.</p><p>This article has described the <b>Border</b> property. Matlab uicontrols&#8217; underlying Java objects contain numerous other useful properties and methods that can greatly extend our Matlab GUI, and I intend to describe some of them in the upcoming months/years.</p><p>Do you have any particular uicontrol customization that you would like me to describe? If so, please do <a
href="http://undocumentedmatlab.com/blog/customizing-uicontrol-border/#respond">post a comment</a>.</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/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li><li><a
href='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/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-uicontrol-border/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>JIDE Property Grids</title><link>http://undocumentedmatlab.com/blog/jide-property-grids/</link> <comments>http://undocumentedmatlab.com/blog/jide-property-grids/#comments</comments> <pubDate>Thu, 22 Apr 2010 00:37: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[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[Levente Hunyadi]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[UIInspect]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1287</guid> <description><![CDATA[The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-linesmoothing-property/' rel='bookmark' title='Plot LineSmoothing property'>Plot LineSmoothing property</a> <small>LineSmoothing is a hidden and undocumented plot line property that creates anti-aliased (smooth unpixelized) lines in Matlab plots...</small></li><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/60898">Levente Hunyadi</a>.</i></p><h3>Matlab&#8217;s property inspector</h3><p>We often wish to edit properties of heterogeneous objects using a common interface. Matlab&#8217;s property inspector, invoked with the built-in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inspect.html"><i><b>inspect</b></i></a> function, answers this need. The inspector is based on a two-column table of property names and values. Properties and their values are populated automatically, and the user can edit values in-place. The inspector enables property categorization, sub-categorization and sorting, which help users find and modify properties easily. For each property, the inspector displays a matching edit control: editbox/combobox/checkbox etc. This simplifies property value editing and prevents illegal value entry. Matlab&#8217;s GUI builder, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f7-998368.html">GUIDE</a>, uses the inspector to let users edit GUI properties such as position, color etc. It is also used by other tools such as the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f9-47085.html#f9-43456">Plot Editor</a>.</p><p><center><div
class="wp-caption aligncenter" style="width: 365px"><img
alt="Matlab's built-in property inspector" src="http://UndocumentedMatlab.com/images/Inspector_mde_cropped.gif" title="Matlab's built-in property inspector" width="355" height="271" /><p
class="wp-caption-text">Matlab's built-in property inspector</p></div></center></p><p>The Matlab inspector can be embedded, with not-too-much effort, within Matlab GUI applications. Examples of this can be found in the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/">FindJObj</a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935">UIInspect</a> utilities.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://undocumentedmatlab.com/images/findjobj.png" target="_blank"><img
alt="FindJObj - embedded property inspector" src="http://undocumentedmatlab.com/images/findjobj.png" title="FindJObj - embedded property inspector" width="450" height="405" /></a><p
class="wp-caption-text">FindJObj - embedded property inspector</p></div></center></p><p>Unfortunately, Matlab&#8217;s property inspector is limited to Handle Graphics, Java and COM objects, and cannot be used for structures or user-defined Matlab classes. We shall see below how to set up our own property grid, populate it with data, and subscribe to property change events. This is a rather procedural approach. It is usually more convenient to use a declarative approach in which a structure or Matlab class is passed to a function that automatically discovers its properties and their meta-information. The <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28732">Property Grid</a> utility at Matlab File Exchange provides these services.</p><h3 id="simple">A simple property grid</h3><p>Matlab&#8217;s property inspector is based on a property grid control by <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com">JIDE Software</a>. JIDE Grids is a collection of components that extend the standard Java Swing <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html">JTable</a> component, and is included in each Matlab installation (/java/jarext/jide/jide-grids.jar under the Matlab root). In particular, JIDE Grids includes the <em>PropertyTable</em> class, which is a fully customizable property grid component. You can find further details on JIDE Grids in the <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf">Developer Guide</a> and the <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/">Javadoc documentation</a>.</p><p>There are several related classes associated with the <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyTable.html"><em>PropertyTable</em></a> class. First, a <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyTableModel.html"><em>PropertyTableModel</em></a> encapsulates all properties that are visualized in the property grid. Each property derives from the <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/Property.html"><em>Property</em></a> abstract class, which features some common actions to properties, most notably to get and set property value. <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/DefaultProperty.html"><em>DefaultProperty</em></a> is a default concrete subclass of <em>Property</em>. Finally, <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/grid/PropertyPane.html"><em>PropertyPane</em></a> decorates a property grid with icons for changing category view to alphabetically sorted view as well as expanding and collapsing categories, and a description text box at the bottom that can be shown or hidden.</p><p>Here are the <i>DefaultProperty</i> fields and their respective roles:</p><table><tbody><tr><th>Field</th><th>Role</th></tr><tr><td>Name</td><td>Interal property name, not necessarily displayed, used as a key to identify the property.</td></tr><tr><td>DisplayName</td><td>A short property name shown in the left column of the property grid.</td></tr><tr><td>Description</td><td>A concise description of the property, shown at the bottom of the property pane, below the grid.</td></tr><tr><td>Type</td><td>The Java type associated with the property, used to invoke the appropriate renderer or editor.</td></tr><tr><td>EditorContext</td><td>An editor context object. If set, both the type and the context are used to look up the renderer or editor to use. This lets, for instance, one flag value to display as a true/false label, while another as a checkbox.</td></tr><tr><td>Category</td><td>A string specifying the property&#8217;s category, for grouping purposes.</td></tr><tr><td>Editable</td><td>Specifies whether the property value is modifiable or read-only.</td></tr><tr><td>Value</td><td>The current property value, as a Java object.</td></tr></tbody></table><p>Just as with any Java object, these fields may either be accessed with the Java get/set semantics (e.g. <i>getName()</i> or <i>setName(name)</i>), or the Matlab <i><b>get/set</b></i> semantics (e.g. <i><b>get</b>(prop,&#8217;Name&#8217;)</i> or <i><b>set</b>(prop,&#8217;Name&#8217;,name)</i>). When using the Matlab syntax, remember to wrap the Java object in a <i><b>handle()</b></i> call, to prevent a <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/#memory_leak">memory leak</a>.</p><p>To use a property grid in Matlab, first construct a set of <i>DefaultProperty</i> objects. For each object, set at least the name, type and initial value. Next, add the properties to a table model. Finally, construct a property grid with the given table model and encapsulate in a property pane:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Initialize JIDE's usage within Matlab</span>
com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJUtilities</span>.<span style="">initJIDE</span>;
&nbsp;
<span style="color: #228B22;">% Prepare the properties list</span>
list = java.<span style="">util</span>.<span style="">ArrayList</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
prop1 = com.<span style="">jidesoft</span>.<span style="color: #0000FF;">grid</span>.<span style="">DefaultProperty</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setName</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'stringProp'</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setType</span><span style="color: #080;">&#40;</span>javaclass<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'char'</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'initial value'</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setCategory</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'My Category'</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setDisplayName</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Editable string property'</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setDescription</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'A concise description for my property.'</span><span style="color: #080;">&#41;</span>;
prop1.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
list.<span style="">add</span><span style="color: #080;">&#40;</span>prop1<span style="color: #080;">&#41;</span>;
&nbsp;
prop2 = com.<span style="">jidesoft</span>.<span style="color: #0000FF;">grid</span>.<span style="">DefaultProperty</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setName</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'flagProp'</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setType</span><span style="color: #080;">&#40;</span>javaclass<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'logical'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setValue</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setCategory</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'My Category'</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setDisplayName</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Read-only flag property'</span><span style="color: #080;">&#41;</span>;
prop2.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
list.<span style="">add</span><span style="color: #080;">&#40;</span>prop2<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Prepare a properties table containing the list</span>
model = com.<span style="">jidesoft</span>.<span style="color: #0000FF;">grid</span>.<span style="">PropertyTableModel</span><span style="color: #080;">&#40;</span>list<span style="color: #080;">&#41;</span>;
model.<span style="">expandAll</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">grid</span> = com.<span style="">jidesoft</span>.<span style="color: #0000FF;">grid</span>.<span style="">PropertyTable</span><span style="color: #080;">&#40;</span>model<span style="color: #080;">&#41;</span>;
pane = com.<span style="">jidesoft</span>.<span style="color: #0000FF;">grid</span>.<span style="">PropertyPane</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">grid</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Display the properties pane onscreen</span>
hFig = <span style="color: #0000FF;">figure</span>;
panel = uipanel<span style="color: #080;">&#40;</span>hFig<span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>pane, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">200</span> <span style="color: #33f;">200</span><span style="color: #080;">&#93;</span>, panel<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Wait for figure window to close &amp; display the prop value</span>
uiwait<span style="color: #080;">&#40;</span>hFig<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>prop1.<span style="">getValue</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Here, <em>com.mathworks.mwswing.MJUtilities.initJIDE</em> is called to initialize JIDE&#8217;s usage within Matlab. Without this call, we may see a JIDE warning message in some Matlab releases. We only need to <em>initJIDE</em> once per Matlab session, although there is no harm in repeated calls.</p><p><i><b>javaclass</b></i> is a function (included in the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28732">Property Grid</a> utility, or directly downloadable from <a
target="_blank" rel="nofollow" href="http://undocumentedmatlab.com/files/javaclass.m">here</a>) that returns a Java class for the corresponding Matlab type with the given dimension: <em><b>javaclass</b>(&#8216;logical&#8217;)</em> or <em><b>javaclass</b>(&#8216;logical&#8217;,0)</em> (a single logical flag value) returns a <em>java.lang.Boolean</em> class; <em><b>javaclass</b>(&#8216;char&#8217;,1)</em> (a character array) returns a <em>java.lang.String</em> class; <em><b>javaclass</b>(&#8216;double&#8217;,2)</em> (a matrix of double-precision floating point values) returns <em>double[][]</em>.</p><p><i><b>javacomponent</b></i> is the undocumented built-in Matlab function that adds Java Swing components to a Matlab figure, using the given dimensions and parent handle. When the user closes the figure, prop.<em>getValue()</em> fetches and displays the new property value.</p><p><center><div
class="wp-caption aligncenter" style="width: 312px"><img
alt="A simple user-defined property grid" src="http://UndocumentedMatlab.com/images/PropertyGrid_simple2.png" title="A simple user-defined property grid" width="302" height="294" /><p
class="wp-caption-text">A simple user-defined property grid</p></div></center></p><p>Next week&#8217;s article will show how to add more complex renderers and editors (display the flag value as a checkbox for example), define nested properties, and subscribe to property value change events. So stay tuned&#8230;</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-linesmoothing-property/' rel='bookmark' title='Plot LineSmoothing property'>Plot LineSmoothing property</a> <small>LineSmoothing is a hidden and undocumented plot line property that creates anti-aliased (smooth unpixelized) lines in Matlab plots...</small></li><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/jide-property-grids/feed/</wfw:commentRss> <slash:comments>12</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> <item><title>Rich Matlab editbox contents</title><link>http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/</link> <comments>http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#comments</comments> <pubDate>Wed, 20 Jan 2010 21:04:11 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=944</guid> <description><![CDATA[The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/' rel='bookmark' title='Customizing listbox &amp; editbox scrollbars'>Customizing listbox &#038; editbox scrollbars</a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='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-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>In an earlier post, I mentioned that <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">most Matlab uicontrols support HTML strings</a>. Unfortunately, HTML is not supported in multi-line editbox contents. Today I will show how this limitation can be removed for a multi-line editbox, thereby enabling rich contents (enabling HTML for a single-line editbox needs a different solution).</p><p>We first need to get the editbox&#8217;s underlying Java object, as explained in my previous article about <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">the <b><i>findjobj</i></b> utility</a>. Since a multi-line editbox is contained within a scroll-pane, we need to dig within the scrollpane container to find the actual editable area object:</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;">% List the scroll-pane's contents:</span>
&gt;&gt; jScrollPane.<span style="">list</span>
com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,100x50,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 javax.<span style="">swing</span>.<span style="">JViewport</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,81x48,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">EditTextPeer</span>$hgTextEditMultiline<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,81x48,<span style="color: #F0F;">...</span>,kit=javax.<span style="">swing</span>.<span style="">text</span>.<span style="">StyledEditorKit</span>@ce05fc,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span>$1<span style="color: #080;">&#91;</span>,<span style="color: #33f;">82</span>,<span style="color: #33f;">1</span>,17x48,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">31</span>,17x17,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,17x17,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span>$2<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span></pre></div></div><p>In this listing, we see that jScrollPane contains a JViewport and 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>. We need the internal hgTextEditMultiline object:</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>;</pre></div></div><p>The retrieved jEditbox reference, is an object of class com.mathworks.hg.peer.EditTextPeer$hgTextEditMultiline, which indirectly extends the standard <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html">javax.swing.JTextPane</a>. The default Matlab implementation of the editbox uicontrol simply enables a multi-line vertical-scrollable text area using the system font. However, the underlying JTextPane object enables many important customizations, including the ability to specify different font attributes (size/color/bold/italic etc.) and paragraph attributes (alignment etc.) for text segments (called style runs) and the ability to embed images, HTML and other controls.</p><p>Setting rich contents can be done in several alternative ways. From easiest to hardest:</p><h3 id="pageURL">Setting page URL</h3><p>Use the <i>setPage(url)</i> method to load a text page from the specified URL (any pre-existing editbox content will be erased). The page contents may be plain text, HTML  or RTF. The content type will automatically be determined and the relevant <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.html">StyledEditorKit</a> and <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledDocument.html">StyledDocument</a> will be chosen for that content. Additional StyledEditorKit content parsers can be registered to handle additional content types.  Here&#8217;s an example loading an HTML page:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setPage</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'http://tinyurl.com/c27zpt'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>where the URL&#8217;s contents are:</p><div
class="wp_syntax"><div
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/dukeWaveRed.gif&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;64&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;64&quot;</span>&gt;</span>
This is an uneditable <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">code</span>&gt;</span>JEditorPane<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">code</span>&gt;</span>, which was
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">em</span>&gt;</span>initialized<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">em</span>&gt;</span> with <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>HTML<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> text 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">font</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span>-<span style="color: #cc66cc;">2</span>&gt;</span>from<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">font</span>&gt;</span> a <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">font</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span>+<span style="color: #cc66cc;">2</span><span style="color: #ff0000;">&quot;&gt;</span></span>URL<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">font</span>&gt;</span>.
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>An editor pane uses specialized editor kits to read, write,
display, and edit text of different formats. The Swing text 
package includes editor kits for plain text, HTML, and RTF. 
You can also develop custom editor kits for other formats. 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;JavaScript&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/js/omi/jsc/s_code_remote.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Matlab editbox initialized from an HTML webpage URL" src="http://UndocumentedMatlab.com/images/editbox6a.png" title="Matlab editbox initialized from an HTML webpage URL" width="450" height="180" /><p
class="wp-caption-text">Matlab editbox initialized from an HTML webpage URL</p></div></center></p><h3 id="EditorKit">Setting the EditorKit and ContentType</h3><p>Set the requested StyledEditorKit (via <i>setEditorKit()</i>) or ContentType properties and then use <i>setText()</i> to set the text, which should be of the appropriate content type. Note that setting EditorKit or ContentType clears any existing text and left-aligns the contents (hgTextEditMultiline is center aligned by default). Also note that HTML &lt;div&gt;s get their own separate lines and that &lt;html&gt; and &lt;body&gt; opening and closing tags are accepted but unnecessary. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setEditorKit</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">text</span>.<span style="">html</span>.<span style="">HTMLEditorKit</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% alternative: jEditbox.setContentType('text/html');</span>
htmlStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;b&gt;&lt;div style=&quot;font-family:impact;color:green&quot;&gt;'</span><span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Matlab&lt;/div&gt;&lt;/b&gt; GUI is &lt;i&gt;'</span> <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;font color=&quot;red&quot;&gt;highly&lt;/font&gt;&lt;/i&gt; customizable'</span><span style="color: #080;">&#93;</span>;
jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span>htmlStr<span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="HTML contents in a Matlab editbox" src="http://UndocumentedMatlab.com/images/editbox6b.png" title="HTML contents in a Matlab editbox" width="200" height="50" /><p
class="wp-caption-text">HTML contents in a Matlab editbox</p></div></center></p><p>Let&#8217;s show another usage example, of an event log file, spiced with icons and colored text based on event severity. First, define the logging utility function (the icon filenames may need to be changed based on your Matlab release):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> logMessage<span style="color: #080;">&#40;</span>jEditbox,text,severity<span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Ensure we have an HTML-ready editbox</span>
   HTMLclassname = <span style="color:#A020F0;">'javax.swing.text.html.HTMLEditorKit'</span>;
   <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isa</span><span style="color: #080;">&#40;</span>jEditbox.<span style="">getEditorKit</span>,HTMLclassname<span style="color: #080;">&#41;</span>
      jEditbox.<span style="">setContentType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% Parse the severity and prepare the HTML message segment</span>
   <span style="color: #0000FF;">if</span> nargin&lt;<span style="color: #33f;">3</span>,  severity=<span style="color:#A020F0;">'info'</span>;  <span style="color: #0000FF;">end</span>
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">lower</span><span style="color: #080;">&#40;</span>severity<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'i'</span>,  icon = <span style="color:#A020F0;">'greenarrowicon.gif'</span>; color=<span style="color:#A020F0;">'gray'</span>;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'w'</span>,  icon = <span style="color:#A020F0;">'demoicon.gif'</span>;       color=<span style="color:#A020F0;">'black'</span>;
      <span style="color: #0000FF;">otherwise</span>, icon = <span style="color:#A020F0;">'warning.gif'</span>;        color=<span style="color:#A020F0;">'red'</span>;
   <span style="color: #0000FF;">end</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'</span>,icon<span style="color: #080;">&#41;</span>;
   iconTxt =<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;img src=&quot;file:///'</span>,icon,<span style="color:#A020F0;">'&quot; height=16 width=16&gt;'</span><span style="color: #080;">&#93;</span>;
   msgTxt = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&amp;nbsp;&lt;font color='</span>,color,<span style="color:#A020F0;">'&gt;'</span>,text,<span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span>;
   newText = <span style="color: #080;">&#91;</span>iconTxt,msgTxt<span style="color: #080;">&#93;</span>;
   endPosition = jEditbox.<span style="">getDocument</span>.<span style="">getLength</span>;
   <span style="color: #0000FF;">if</span> endPosition&gt;<span style="color: #33f;">0</span>, newText=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;br/&gt;'</span> newText<span style="color: #080;">&#93;</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% Place the HTML message segment at the bottom of the editbox</span>
   currentHTML = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>jEditbox.<span style="">getText</span><span style="color: #080;">&#41;</span>;
   jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>currentHTML,<span style="color:#A020F0;">'&lt;/body&gt;'</span>,newText<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   endPosition = jEditbox.<span style="">getDocument</span>.<span style="">getLength</span>;
   jEditbox.<span style="">setCaretPosition</span><span style="color: #080;">&#40;</span>endPosition<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% end of content</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Now, let&#8217;s use this logging utility function to log some messages:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a regular info message...'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a warning message...'</span>, <span style="color:#A020F0;">'warn'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'an error message!!!'</span>, <span style="color:#A020F0;">'error'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a regular message again...'</span>, <span style="color:#A020F0;">'info'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Rich editbox contents (a log file)" src="http://UndocumentedMatlab.com/images/editbox10.png" title="Rich editbox contents (a log file)" width="200" height="60" /><p
class="wp-caption-text">Rich editbox contents (a log file)</p></div></center></p><p>HTML editboxes are normally editable, images included. In actual applications, we may wish to prevent editing the display log. To do this, simply call jEditbox.<i>setEditable(false)</i>.</p><p>Setting a hyperlink handler is easy: first we need to ensure that we&#8217;re using an HTML content-type document. Next, set the editbox to be uneditable (hyperlinks display correctly when the editbox is editable, but are unclickable), using jEditbox.<i>setEditable(false)</i>. Finally, set the callback function in the editbox&#8217;s HyperlinkUpdateCallback property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setContentType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link: &lt;a href= &quot;http://UndocumentedMatlab.com&quot;&gt;UndocumentedMatlab.com&lt;/a&gt;'</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
hjEditbox = handle<span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hjEditbox,<span style="color:#A020F0;">'HyperlinkUpdateCallback'</span>,@linkCallbackFcn<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">function</span> linkCallbackFcn<span style="color: #080;">&#40;</span>src,eventData<span style="color: #080;">&#41;</span>
   url = eventData.<span style="">getURL</span>;      <span style="color: #228B22;">% a java.net.URL object</span>
   description = eventData.<span style="">getDescription</span>; <span style="color: #228B22;">% URL string</span>
   jEditbox = eventData.<span style="">getSource</span>;
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">ENTERED</span><span style="color: #080;">&#41;</span>
               <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link hover enter'</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">EXITED</span><span style="color: #080;">&#41;</span>
               <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link hover exit'</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">ACTIVATED</span><span style="color: #080;">&#41;</span>
               jEditbox.<span style="">setPage</span><span style="color: #080;">&#40;</span>url<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 230px"><img
alt="Hyperlink in editbox" src="http://UndocumentedMatlab.com/images/editbox12.png" title="Hyperlink in editbox" width="220" height="60" /><p
class="wp-caption-text">Hyperlink in editbox</p></div></center></p><h3 id="styles">Setting the style runs programmatically</h3><p>Setting the styles programmatically, one style run after another, can be done via the text-pane&#8217;s Document property object. Individual character ranges can be set using the Document&#8217;s <i>setCharacterAttributes</i> method,  or entire style runs can be inserted via <i>insertString</i>. Attributes are updated using the static methods available in javax.swing.text.StyleConstants. These methods include setting character attributes (font/size/bold/italic/strike-through/underline/subscript/superscript and foreground/background colors), paragraph attributes (indentation/spacing/tab-stops/bidi), image icons and any Swing Component (buttons etc.). Here is the end result:<br
/><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Rich editbox contents: images, controls &#038; font styles" src="http://UndocumentedMatlab.com/images/editbox6c.png" title="Rich editbox contents: images, controls &#038; font styles" width="450" height="250" /><p
class="wp-caption-text">Rich editbox contents: images, controls &#038; font styles</p></div></center></p><p>Note that if a styled multi-line editbox is converted to a single-line editbox (by setting hEditbox&#8217;s Max property to 1), it loses all style information, embedded images and components. Returning to multi-line mode will therefore show only the plain-text.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/' rel='bookmark' title='Customizing listbox &amp; editbox scrollbars'>Customizing listbox &#038; editbox scrollbars</a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='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-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/rich-matlab-editbox-contents/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>FindJObj GUI &#8211; display container hierarchy</title><link>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/</link> <comments>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/#comments</comments> <pubDate>Tue, 12 Jan 2010 13:47:18 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=908</guid> <description><![CDATA[The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In my previous post, I explained how the <b><i>findjobj</i></b> utility can be used to access a Matlab component&#8217;s underlying Java component. <b><i>Findjobj</i></b> has another role: displaying the component hierarchy of complex Matlab containers such as the <a
target="_blank" href="http://undocumentedmatlab.com/blog/figure-toolbar-customizations/">figure window</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/">GUIDE</a> or the <a
target="_blank" href="http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/">Editor</a>.</p><p>When <b><i>findjobj</i></b> is called with no output arguments, the function infers that the user requests to see the GUI version, rather than to get the control&#8217;s Java handle:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; findjobj<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: findjobj(gcf)</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/findjobj9.png" target="_blank"><img
alt="FindJObj GUI (click to zoom)" src="http://UndocumentedMatlab.com/images/findjobj9.png" title="FindJObj GUI (click to zoom)" width="450" height="296" /></a><p
class="wp-caption-text">FindJObj GUI (click to zoom)</p></div></center></p><p>There are several note-worthy aspects in this graphical hierarchy presentation:</p><p>The hierarchy tree itself is displayed using the internal com.mathworks.hg.peer.UITreePeer Java object. This is the object that underlies the semi-documented <b><i>uitree</i></b> function. The hierarchy sub-components are presented as tree nodes, each having a separate icon based on the component type. In some cases (toolbar buttons for example), the component&#8217;s icon image is used for its corresponding tree node. A javax.swing.JProgressBar is presented while the tree is being populated, an action that can take a few seconds depending on the target figure&#8217;s complexity. Some tree branches which are normally uninteresting are automatically collapsed: hidden containers (these are also grayed-out), menubars, toolbars and scrollbars. In parallel to the Java container hierarchy, a separate tree branch is presented with the corresponding Matlab (Handle-Graphics, or HG) hierarchy.</p><p><center><div
class="wp-caption aligncenter" style="width: 430px"><img
alt="Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container" src="http://undocumentedmatlab.com/images/statusbar6d2.png" title="Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container" width="420" height="509" /><p
class="wp-caption-text">Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container</p></div></center></p><p>Each node item gets a unique tooltip (see top screenshot above). Similarly, a unique context-menu (right-click menu) is attached to each node item with actions that are relevant for that node:</p><p><center><div
class="wp-caption aligncenter" style="width: 321px"><img
alt="Item-specific context-menu" src="http://UndocumentedMatlab.com/images/findjobj_contextmenu.png" title="Item-specific context-menu" width="311" height="171" /><p
class="wp-caption-text">Item-specific context-menu</p></div></center></p><p>Finally, a node-selection callback is attached to the tree, that will flash a red border around the GUI control when its corresponding Java node-item is clicked/selected:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://undocumentedmatlab.com/images/findjobj.png" target="_blank"><img
alt="FindJObj - flashing red border around a toolbar icon" src="http://undocumentedmatlab.com/images/findjobj.png" title="FindJObj - flashing red border around a toolbar icon" width="450" height="405" /></a><p
class="wp-caption-text">FindJObj - flashing red border around a toolbar icon</p></div></center></p><p>Once the tree was done, I set out to display and enable modifications of component properties and callbacks in separate adjacent panels. I used the internal com.mathworks.mlwidgets.inspector.PropertyView component to display the properties (this is the JIDE component that underlies the built-in <b><i>inspect</i></b> function). To prevent a JIDE run-time alert, I called com.mathworks.mwswing.MJUtilities.initJIDE. A label is added to the table&#8217;s header, displaying the currently selected sub-component&#8217;s class (e.g., &#8220;javax.swing.JButton&#8221;), and a tooltip with a color-coded list of all the control&#8217;s properties.</p><p>The callbacks table was implemented using com.jidesoft.grid.TreeTable to enable easy column resizing, but this is otherwise used as a simple data table. A checkbox was added to filter out the 30-odd standard <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">Swing callbacks</a>, which are non-unique to the selected sub-component (tree node). All the panels &#8211; tree, properties and callbacks &#8211; are then placed in resizable javax.swing.JSplitPanes and presented to the user.</p><p>I have omitted mention of some other undocumented features in <b><i>findjobj</i></b>. After all, space here is limited and the function is over 2500 lines long. I encourage you to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">download the utility</a> and explore the code, and I gladly welcome your feedback.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</title><link>http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/</link> <comments>http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/#comments</comments> <pubDate>Wed, 06 Jan 2010 21:06:33 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[JavaFrame]]></category> <category><![CDATA[PixelBounds]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=892</guid> <description><![CDATA[The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism.<pre> </pre>Related posts:<ol><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><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-components/' rel='bookmark' title='Setting status-bar components'>Setting status-bar components</a> <small>Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...</small></li><li><a
href='http://undocumentedmatlab.com/blog/date-selection-components/' rel='bookmark' title='Date selection components'>Date selection components</a> <small>The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In a previous post, I explained that all Matlab GUI (except the axes plotting engine) is based on Java components, and showed how we can use this information to <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">display HTML contents in Matlab <b><i>uicontrol</i></b>s</a>. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/findjobj/">other posts</a>, I have shown how a utility called <b><i>findjobj</i></b> can be used to access the underlying Java components to enable customizations that are unavailable in standard Matlab: <a
target="_blank" href="http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/">setting the line location in an edit-box</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/button-customization/">customizing button appearance</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">setting uicontrol callbacks</a>, or <a
target="_blank" href="http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/">setting list-box mouse actions</a>. I have also shown how <b><i>findjobj</i></b> can be used to display the component hierarchy of complex Matlab containers such as the <a
target="_blank" href="http://undocumentedmatlab.com/blog/figure-toolbar-customizations/">figure window</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/">GUIDE</a> or the <a
target="_blank" href="http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/">Editor</a>.</p><p>The time is therefore well overdue for a formal introduction of <b><i>findjobj</i></b>, explaining its uses and internal mechanism. Of course, readers are welcome to continue using <b><i>findjobj</i></b> as a black-box utility, but I think important insight can be gained from understanding its inner details. <b><i>Findjobj</i></b>&#8216;s code is available for free download on the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">MathWorks File Exchange</a>. It is one of my favorite submissions and is apparently well-liked by users, being highly reviewed and highly downloaded.</p><p><b><i>Findjobj</i></b> has two main purposes:</p><ol><li>Find the underlying Java object reference of a given Matlab handle &#8211; Historically this was the original purpose, hence the utility&#8217;s name. <b><i>Findjobj</i></b> was meant to extend Matlab&#8217;s standard <b><i>findobj</i></b> function, which does not expose Java components.</li><li>Display a container&#8217;s internal components hierarchy in a graphical user interface, to facilitate visualization of complex containers. This was later extended to also display and allow modification of the sub-components&#8217; properties and callbacks.</li></ol><p>Today I will focus on the first (programmatic) aspect;  next week I will describe the second (GUI) aspect.</p><p><b><i>Findjobj</i></b>&#8216;s heart is finding a control&#8217;s underlying Java handle. Unfortunately, this is not exposed by Matlab except in very rare cases. As hard as I tried, I could not find a way to directly access the underlying Java-peer handle. I therefore resorted to getting the control&#8217;s enclosing Java frame (window) reference, and then working down its sub-components hierarchy until finding the Java object(s) which satisfy the position and/or class criteria. To get the enclosing Java frame (aka <i>TopLevelAncestor</i>), I use the Matlab figure&#8217;s undocumented <b>JavaFrame</b> property. Using this property issues a standard warning (since Matlab release R2008a) of becoming obsolete in some future Matlab release. Since it worked so far, I have turned off this warning in <b><i>findjobj</i></b>&#8216;s code, but note that this code may well fail in some future Matlab version. If and when <b>JavaFrame</b> does become obsolete, be sure to look in this blog for workarounds…</p><p>Traversing the frame&#8217;s hierarchy presents several challenges: Main-menu items are accessed using different functions than other Swing components or sub-containers, and are not automatically accessible until first displayed. I have overcome this latter challenge by simulating a menu-open action in case menus should be searched (this is off by default since it takes several seconds and also changes the GUI focus). For &#8220;regular&#8221; sub-containers, sometimes we need to loop over <i>getComponent(&#8230;)</i> and in some other cases over <i>getChildAt(&#8230;)</i>.</p><p>Another challenge was presented by the fact that Java positions start at (0,0) in the <u>top</u> left corner increasing rightward and <u>downward</u>, rather than starting at (1,1) in the bottom left and increasing upward as in Matlab. Moreover, Java positions are always pixel-based and relative to their parent container, which is different from Matlab (if the Matlab units is &#8216;pixels&#8217; then the value is absolute; if &#8216;normalized&#8217; then it returns a non-pixel value). To further complicate matters, some Matlab controls have a different size than their Java counterparts: some controls have a 5-pixel margins while others not, some controls are shifted by a pixel or two from their container&#8217;s border (for a total offset of up to 7 pixels), while some controls (such as popup-menus) have an entirely different reported size. In theory, we could use the Matlab component&#8217;s undocumented <b>PixelBounds</b> property (much faster than <b><i>getpixelposition</i></b>), but unfortunately <b>PixelBounds</b> turns out to be unreliable and returns erroneous values in many cases. Finally, different Java containers/components have different ways of returning their position: for some it is a <i>getLocation()</i> method, for others it is <i>getX()/getY()</i> and for others it is the <b>X</b> and <b>Y</b> properties (that sometimes have no corresponding <i>getX()/getY()</i> accessor methods!).</p><p>Having finally overcome all these challenges (and quite a few smaller ones, documented within the source code), I have wrapped the algorithm in a function interface that tries to emulate <b><i>findobj</i></b>&#8216;s. Using <b><i>findjobj</i></b> can now be as easy as:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Modify the mouse cursor when over the button</span>
hButton = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#41;</span>;
jButton = findjobj<span style="color: #080;">&#40;</span>hButton<span style="color: #080;">&#41;</span>;
jButton.<span style="">setCursor</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Cursor</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Cursor</span>.<span style="">HAND_CURSOR</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 260px"><img
alt="Modified uicontrol cursor - a Java property" src="http://UndocumentedMatlab.com/images/button_cursor_hand.png" title="Modified uicontrol cursor - a Java property" width="105" height="66" /><p
class="wp-caption-text">Modified uicontrol cursor - a Java property</p></div></center></p><p>&#8230;or as complex as:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Find all non-button controls with the specified label</span>
jControls = findjobj<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'property'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'text'</span>,<span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#125;</span>, <span style="color:#A020F0;">'not'</span>,<span style="color:#A020F0;">'class'</span>,<span style="color:#A020F0;">'button'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Space here is limited and <b><i>findjobj</i></b> is over 2500 lines long, so I have obviously not covered everything. I encourage you to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">download the utility</a> and explore the code, and I gladly welcome your feedback.</p><p><pre> </pre>Related posts:<ol><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><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-components/' rel='bookmark' title='Setting status-bar components'>Setting status-bar components</a> <small>Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...</small></li><li><a
href='http://undocumentedmatlab.com/blog/date-selection-components/' rel='bookmark' title='Date selection components'>Date selection components</a> <small>The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/feed/</wfw:commentRss> <slash:comments>19</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 03:08:48 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/findjobj/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.412s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328350126; expires=Sun, 03-Feb-2013 10:08:46 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 10:08:48 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 11:08:48 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               8c4f129f919180dee50e175da7de8f69
Content-Encoding:   gzip
-->
