<?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; uicontrol</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/uicontrol/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +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>Panel-level uicontrols</title><link>http://undocumentedmatlab.com/blog/panel-level-uicontrols/</link> <comments>http://undocumentedmatlab.com/blog/panel-level-uicontrols/#comments</comments> <pubDate>Wed, 24 Nov 2010 18:00:53 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[TitleHandle]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1976</guid> <description><![CDATA[Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-html-panel/' rel='bookmark' title='GUI integrated HTML panel'>GUI integrated HTML panel</a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In one of my larger Matlab applications &#8211; <em>Integration-lab Debriefing System</em> (IDS, which shall be described in a future dedicated article) &#8211; I wanted to present a panel-level checkbox that applies to the entire panel contents. In my particular case, the IDS <i><b>uipanel</b></i> contained a Java table (another future topic) with multiple selectable rows, and I needed a global checkbox that selects all (or none) of them at once:</p><p><center><div
class="wp-caption aligncenter" style="width: 138px"><img
alt='Panel-level ("Objects") checkbox' src="http://UndocumentedMatlab.com/images/IDS_Main_Tabs.png" title='Panel-level ("Objects") checkbox' width="128" height="107" /><p
class="wp-caption-text">Panel-level ("Objects") checkbox</p></div></center></p><p>One way to do this is to calculate the checkbox&#8217;s desired position relative to the <i><b>uipanel</b></i> and place a regular checkbox-style <i><b>uicontrol</b></i> there. The checkbox can even be made a child of the <i><b>uipanel</b></i> with &#8216;normalized&#8217; units, thereby moving and resizing it together with its <i><b>uipanel</b></i> parent when the later is moved or resized.</p><p>But there&#8217;s a much simpler method that want to share. It relies on the undocumented fact that the <i><b>uipanel</b></i>&#8216;s title label is a simple hidden <i><b>uicontrol</b></i> child of the <i><b>uipanel</b></i> handle. This <i><b>uicontrol</b></i> handle can be found and simply transformed from a &#8216;style&#8217;='text&#8217; control into a &#8216;style&#8217;='checkbox&#8217; control, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Prepare the panel</span>
hPanel = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.4</span>,<span style="color: #33f;">0.4</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Objects'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the title label's handle</span>
<span style="color: #0000FF;">warning</span> off MATLAB<span style="color: #F0F;">:</span>Uipanel<span style="color: #F0F;">:</span>HiddenImplementation  <span style="color: #228B22;">% turn off warning</span>
hTitle = <span style="color: #0000FF;">setdiff</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hPanel<span style="color: #080;">&#41;</span>,hPanel<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% retrieve title handle</span>
hTitle = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hPanel,<span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative, uses hidden prop</span>
&nbsp;
<span style="color: #228B22;">% Modify the uicontrol style; add 20 pixel space for the checkbox</span>
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hTitle,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTitle, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">1</span>, <span style="color:#A020F0;">'pos'</span>,newPos<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 130px"><img
alt="Panel-level checkbox" src="http://UndocumentedMatlab.com/images/IDS_Main_Checkbox.png" title="Panel-level checkbox" width="120" height="70" /><p
class="wp-caption-text">Panel-level checkbox</p></div></center></p><p>Note that we can retrieve the title handle using either the <i><b>uipanel</b></i>&#8216;s hidden property <b>TitleHandle</b>, or by scanning the panel&#8217;s children using <i><b>findall</b></i>. I prefer the <b>TitleHandle</b> approach because it does not require modification (minor as it might be) when the panel already contains other children.</p><p>The down-side is that since <b>TitleHandle</b> is an undocumented hidden property, it may change its name, its behavior or even be removed in some future Matlab release. In fact, there&#8217;s even a Matlab warning about this, unless you turn it off using:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">warning</span> off MATLAB<span style="color: #F0F;">:</span>Uipanel<span style="color: #F0F;">:</span>HiddenImplementation</pre></div></div><p>We can use this approach for more complex options panels, as the following example illustrates. Here, we set radio-button controls rather than a checkbox control, and also modify the title color to blue:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Main panel and basic alternative control</span>
hPanelMain = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.1,.1,.8,.8<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Title'</span>,<span style="color:#A020F0;">'Main'</span>, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;
hAlt1 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.1,.8,.5,.1<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Alternative #1'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative options panel #2</span>
hAlt2 = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.07,.4,.5,.35<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Alternative #2'</span><span style="color: #080;">&#41;</span>;
hAlt2Title = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt2, <span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt2Title,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAlt2Title, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'pos'</span>,newPos<span style="color: #080;">&#41;</span>;
hAlt2a = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt2, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.6,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Option 1'</span><span style="color: #080;">&#41;</span>;
hAlt2b = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt2, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.2,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Option 2'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative options panel #3</span>
hAlt3 = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hPanelMain, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.07,.05,.5,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Alternative #3'</span><span style="color: #080;">&#41;</span>;
hAlt3Title = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt3, <span style="color:#A020F0;">'TitleHandle'</span><span style="color: #080;">&#41;</span>;
newPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAlt3Title,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% in pixels</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAlt3Title, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'radio'</span>, <span style="color:#A020F0;">'pos'</span>,newPos, <span style="color:#A020F0;">'ForegroundColor'</span>,<span style="color:#A020F0;">'blue'</span><span style="color: #080;">&#41;</span>;
hAlt3a = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hAlt3, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.5,.7,.3<span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'popup'</span>, <span style="color:#A020F0;">'string'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Option 3a'</span>,<span style="color:#A020F0;">'Option 3b'</span>,<span style="color:#A020F0;">'Option 3c'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 431px"><img
alt="Advanced panel-level controls" src="http://UndocumentedMatlab.com/images/Panel_Controls.png" title="Advanced panel-level controls" width="421" height="373" /><p
class="wp-caption-text">Advanced panel-level controls</p></div></center></p><p>Note that since the hAlt2Title and hAlt3Title radio-buttons are children of their respective <i><b>uipanel</b></i> parents, we cannot use a simple <i><b>uibuttongroup</b></i> to group them in a mutual-exclusion group. Instead, we must use a dedicated callback function. This is actually quite easy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Set the callback for all relevant radio-buttons</span>
hButtonGroup = <span style="color: #080;">&#91;</span>hAlt1, hAlt2Title, hAlt3Title<span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButtonGroup, <span style="color:#A020F0;">'Callback'</span>, <span style="color: #080;">&#123;</span>@SelectionCb, hButtonGroup<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% This is the callback function that manages mutual exclusion</span>
<span style="color: #0000FF;">function</span> SelectionCb<span style="color: #080;">&#40;</span>hSrc,hEvent,hButtonGroup<span style="color: #080;">&#41;</span>
   otherButtons = <span style="color: #0000FF;">setdiff</span><span style="color: #080;">&#40;</span>hButtonGroup,hSrc<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>otherButtons,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSrc,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% needed to prevent de-selection</span>
<span style="color: #0000FF;">end</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-html-panel/' rel='bookmark' title='GUI integrated HTML panel'>GUI integrated HTML panel</a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/panel-level-uicontrols/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>The javacomponent function</title><link>http://undocumentedmatlab.com/blog/javacomponent/</link> <comments>http://undocumentedmatlab.com/blog/javacomponent/#comments</comments> <pubDate>Wed, 04 Aug 2010 18:00:59 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1793</guid> <description><![CDATA[Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/hgfeval/' rel='bookmark' title='The hgfeval function'>The hgfeval function</a> <small>The semi-documented hgfeval function can be useful for callback chaining - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In this blog I have often showed how using Java components can significantly improve Matlab GUI. Here is a simple reminder:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
target="_blank" href="http://UndocumentedMatlab.com/images/uicomponent.png"><img
alt="sample Java components integrated in Matlab figure window (click for details)" src="http://UndocumentedMatlab.com/images/uicomponent.png" title="sample Java components integrated in Matlab figure window (click for details)" width="450" height="354" /></a><p
class="wp-caption-text">sample Java components integrated in Matlab figure window (click for details)</p></div></center></p><p>Matlab is highly integrated with Java, and Java classes can seamlessly be accessed from Matlab. However, displaying Java GUI objects, as opposed to using computational (non-displayable) Java classes, requires using Matlab&#8217;s built-in <i><b>javacomponent</b></i> function. I have often used this function in past articles here, and today I would like to describe it in more detail.</p><h3 id="javacomponent">javacomponent</h3><p><i><b>javacomponent</b></i>, available since R14 (Matlab 7.0), is yet another <a
target="_blank" href="http://UndocumentedMatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> built-in function. This means that the function is explained in a comment within the function (which can be seen via the <i><b>edit</b>(&#8216;javacomponent&#8217;)</i> command), but nonetheless does not have official help or doc pages. It is an unsupported function originally intended only for internal Matlab use (which of course doesn&#8217;t mean we can&#8217;t use it).</p><p><i><b>javacomponent</b></i> accepts a component class name (a string) or a reference to a previously-created component object, an optional pixel position parameter (default=[20,20,60,20], just like <i><b>uicontrol</b></i>; may also contain the strings &#8216;North&#8217;, &#8216;South&#8217;, &#8216;East&#8217; or &#8216;West&#8217;), and an optional parent container handle  (defaults to the current figure). <i><b>javacomponent</b></i> then adds the requested component as a child of the requested parent container and wraps it in a Matlab Handle-Graphics (HG) container. <i><b>javacomponent</b></i> returns two handles: the Matlab HG container handle and a reference (handle) to the Java component. Here are some sample invocation formats:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #080;">&#91;</span>jButton, hButton<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>
hButton =
	javahandle_withcallbacks.<span style="">javax</span>.<span style="">swing</span>.<span style="">JButton</span>
jButton =
          <span style="color: #33f;">158.002197265625</span>
&nbsp;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span>,<span style="color:#A020F0;">'North'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click me!'</span><span style="color: #080;">&#41;</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">50</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; javacomponent<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click me!'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'East'</span>,hFig<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note the difference between Java object creation and <i><b>javacomponent</b></i>: A pre-created Java object only resides in JVM (Java Virtual Machine) memory, not onscreen, until <i><b>javacomponent</b></i> is called to display it. <i><b>javacomponent</b></i> only creates objects when a class name (string) parameter is passed to it, as a convenience service to programmers. In practice, it is better to separate these two actions: create the Java object separately, and then pass the object&#8217;s reference handle to <i><b>javacomponent</b></i> for display. This enables easier error-trapping if the Java object cannot be created or fails to initialize, before attempting to display the object:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create and initialize a JScrollBar object</span>
<span style="color: #0000FF;">try</span>
   jScrollbar = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JScrollBar'</span><span style="color: #080;">&#41;</span>;
   jScrollbar.<span style="">setOrientation</span><span style="color: #080;">&#40;</span>jScrollbar.<span style="">HORIZONTAL</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Cannot create Java-based scroll-bar!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #228B22;">% Display the object onscreen</span>
<span style="color: #0000FF;">try</span>
   javacomponent<span style="color: #080;">&#40;</span>jScrollbar,<span style="color:#A020F0;">'South'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Cannot display Java-base scroll-bar!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that Java GUI object should always use the EDT (Event Dispatch Thread). The reasons for this were outlined in the recent <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">Matlab-EDT article</a>. For this reason, the JScrollBar is created using the built-in <i><b>javaObjectEDT</b></i> function, which exists since R2008a and became documented/supported in R2009a.</p><p><i><b>javacomponent</b></i> accepts parent handles that are figures, toolbars, <i><b>uipanel</b></i>s or <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/"><i><b>uicontainer</b></i>s</a>.  Unfortunately, frames are not <i><b>uicontainer</b></i>s and therefore cannot be used as <i><b>javacomponent</b></i> parents. <b><u>Addendum Aug 6 2010</u></b>: I made an incorrect statement in the original post here regarding <i><b>uipanel</b></i>s, which has now been removed. I thank the reader who pointed this out to me.</p><p>Once the component has been created, even before it has been placed onscreen, it can be manipulated just like any other Java object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Click again!'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: set(jButton,'text','…')</span></pre></div></div><p>The component can also be manipulated to some extent via its HG container, which is of a special Matlab type (class) called hgjavacomponent. This includes getting/setting the component position, position units, visibility, resizing callback, tag, <b>UserData</b> etc:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButton,<span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.3</span>,<span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButton,<span style="color:#A020F0;">'visible'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%note: on/off, not true/false as in Java</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hButton,<span style="color:#A020F0;">'ResizeFcn'</span>,<span style="color: #080;">&#123;</span>@resizeCallbackFunc,param1,param2<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>When adding Java components which are container classes (descendants of <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html">java.awt.Container</a>), it is important to remember that only other Java components can be added to these containers. Matlab objects such as axes (for plots or images) and <i><b>uicontrol</b></i>s cannot be added since they do not have a Container wrapper.  Therefore, feel free to use these Java containers as long as their contained GUI is limited to Java components (JButton, JComboBox etc.). This limitation is very annoying – it would be very useful to be able to place Matlab axes or <i><b>uicontrol</b></i>s within a JTabbedPane or JSplitPane. Instead, we need to rely on Matlab-based workarounds (<a
target="_blank" href="http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/"><i><b>uitab</b></i></a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/23073-uisplitpane"><i><b>uisplitpane</b></i></a>) which are cumbersome compared to their Java counterparts.</p><p><i><b>javacomponent</b></i> can be used to place not only Swing components but also Swing-extended components onscreen. Matlab itself almost never uses Swing components as-is, instead preferring to use MathWorks-derived extensions of these components, generally in the com.mathworks.mwswing or com.mathworks.widgets packages. These packages and their classes are all in the static Java classpath and are therefore automatically available for use by Matlab programmers.</p><p>Just like Matlab components, <i><b>javacomponent</b></i> can also display third-party or your own Swing-derived components. There are quite a few online sources  for Swing components that can easily be incorporated in your Matlab application. Simply download the relevant class files, add them to your static (via classpath.txt) or dynamic (via <i><b>javaaddpath</b></i>) Java classpath, use <i><b>javacomponent</b></i> to display them, then use their reference handle to manipulate their appearance and behavior.</p><p><i><b>javacomponent</b></i>, useful as it is, has several limitations. In its string variant (classname) it requires a fully-qualified classname that is not inferred automatically. It also has a different parameters format than <i><b>uicontrol</b></i>, which may confuse users. <i><b>javacomponent</b></i> also cannot display <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html">java.awt.Window</a> components. Finally, it returns two handles – one is a <i><b>handle</b>()</i> reference of the Java object; the second an HG handle (a double numeric value) of the automatically-created HG container – <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/172873">users are often confused</a> as to which property should be set on which of these handles.</p><h3 id="utility">javacomponent-based utility functions</h3><p>To overcome these limitations, I created <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14583-UIComponent"><i><b>UIComponent</b></i></a> – a utility that merges <i><b>uicontrol</b></i> and <i><b>javacomponent</b></i>, available for download on the File Exchange.  It accepts all <i><b>uicontrol</b></i> parameters and styles, as well as any other displayable Java (Swing/AWT) class. <i><b>uicontrol</b></i>&#8216;s calling syntax was preserved for full backwards compatibility. <i><b>uicomponent</b></i> uses the built-in <i><b>uicontrol</b></i> whenever possible (for standard Matlab styles), and <i><b>javacomponent</b></i> for all other Java classes.</p><p><i><b>uicomponent</b></i> returns the same two handles that <i><b>javacomponent</b></i> returns (namely, a Java reference handle and a numeric HG handle), modified to include each other&#8217;s properties and handles (yet another undocumented trick that merits a dedicated article). Here are some examples (more can be found in <i><b>uicomponent</b></i>&#8216;s help comment):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'hello'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a regular uicontrol</span>
uicomponent<span style="color: #080;">&#40;</span>hFig, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'hello'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% specify parent</span>
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'jspinner'</span>,<span style="color:#A020F0;">'value'</span>,<span style="color: #33f;">7</span><span style="color: #080;">&#41;</span>;
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'javax.swing.jslider'</span>,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'myObj'</span><span style="color: #080;">&#41;</span>; 
uicomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'JComboBox'</span>,<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span>,<span style="color: #0000FF;">pi</span>,<span style="color:#A020F0;">'text'</span><span style="color: #080;">&#125;</span>,<span style="color:#A020F0;">'editable'</span>,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Another File Exchange submission which aims to tackle some of <i><b>javacomponent</b></i>&#8216;s limitations is Malcolm Lidierth&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15580"><i><b>JCONTROL</b></i></a>. <i><b>jcontrol</b></i> uses Matlab&#8217;s new object-oriented class approach and has the benefit of returning just a single handle object, which aggregates the handles for both HG container and the contained Java object.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/hgfeval/' rel='bookmark' title='The hgfeval function'>The hgfeval function</a> <small>The semi-documented hgfeval function can be useful for callback chaining - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/javacomponent/feed/</wfw:commentRss> <slash:comments>18</slash:comments> </item> <item><title>Inactive Control Tooltips &amp; Event Chaining</title><link>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/</link> <comments>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/#comments</comments> <pubDate>Wed, 24 Feb 2010 22:34:46 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1153</guid> <description><![CDATA[Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again, I welcome guest blogger Matt Whitaker, who continues his series of articles.</i></p><p>In my last post I explored some <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">tips on tooltips</a>. One of these tips involved displaying tooltips on disabled uicontrols. I explained that displaying tooltips on inactive controls is problematic since Matlab appears to intercept mouse events to these inactive controls, so even setting the tooltip on the underlying Java object will not work: The java object appears not to receive the mouse-hover event and therefore does not &#8220;know&#8221; that it&#8217;s time to display the tooltip.</p><p>When Yair and I deliberated this issue, he pointed me to his <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/#comment-1173">comment on a previous article</a> showing an undocumented Java technique (Java also has some&#8230;) for forcing a tooltip to appear using the <b>ActionMap</b> of the uicontrol&#8217;s underlying Java object to get at a <i>postTip</i> action. We discussed using a <b>WindowButtonMotionFcn</b> callback to see if the mouse was above the inactive control, then triggering the forced tooltip display. Yair then went on to remind me and I quote: <i>&#8220;you&#8217;ll need to chain existing <b>WindowButtonMotionFcn</b> callbacks and take into account ModeManagers that override them.&#8221;</i></p><p>Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!</p><p>The Image Processing Toolbox has the nice pair of <b><i>iptaddcallback</i></b> and <b><i>iptremovecallback</i></b> functions that largely handle these issues. But  for general Matlab, there seemed to be no alternative until I remembered that events trigger callbacks. I decided to use a listener for the <i>WindowButtonMotion</i> event to detect the mouse motion. <a
target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Event_Listener">Event listeners were briefly explained two weeks ago</a> and deserve a dedicated future article. The advantage of using an event listener is that we don&#8217;t disturb any existing <b>WindowButtonMotionFcn</b> callback. We still need to be somewhat careful that our listeners don&#8217;t do conflicting things, but it&#8217;s a lot easier than trying to manage everything through the single <b>WindowButtonMotionFcn</b>.</p><p>A demonstration of this appears below with some comments following (note that this code uses the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> inactiveBtnToolTip
  <span style="color: #228B22;">%Illustrates how to make a tooltip appear on an inactive control</span>
  h = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'WindowButtonMotionFcn'</span>,@windowMotion,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">400</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  col = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'color'</span><span style="color: #080;">&#41;</span>;
  lbl = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">160</span>,<span style="color: #33f;">120</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Background'</span>,col, <span style="color:#A020F0;">'HorizontalAlignment'</span>,<span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
  btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'inactive'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'check'</span>, <span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Enable button tooltip'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Callback'</span>,@chkTooltipEnable, <span style="color:#A020F0;">'Value'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">180</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Background'</span>,col<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">drawnow</span>;
&nbsp;
  <span style="color: #228B22;">%create the tooltip and postTip action</span>
  jBtn = findjobj<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>;
  import java.<span style="">awt</span>.<span style="">event</span>.<span style="">ActionEvent</span>;
  javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jBtn,<span style="color:#A020F0;">'This button is inactive'</span><span style="color: #080;">&#41;</span>;
  actionMap = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getActionMap'</span>,jBtn<span style="color: #080;">&#41;</span>;
  action = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'get'</span>,actionMap,<span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
  actionEvent = ActionEvent<span style="color: #080;">&#40;</span>jBtn, ActionEvent.<span style="">ACTION_PERFORMED</span>, <span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%get the extents plus 2 pixels of the control to compare to the mouse position</span>
  btnPos = getpixelposition<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>+<span style="color: #080;">&#91;</span>-<span style="color: #33f;">2</span>,-<span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span>; <span style="color: #228B22;">%give a little band around the control</span>
  left = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  right = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  btm = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
  top =  <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% add a listener on mouse movement events</span>
  tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%tooltip manager</span>
  pointListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'WindowButtonMotionEvent'</span>,@figMouseMove<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%inControl is a flag to prevent multiple triggers of the postTip action</span>
  <span style="color: #228B22;">%while mouse remains in the button</span>
  inControl = <span style="color: #0000FF;">false</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> figMouseMove<span style="color: #080;">&#40;</span>src,evtData<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
    <span style="color: #228B22;">%get the current point</span>
    cPoint = evtData.<span style="">CurrentPoint</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &gt;= left <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &lt;= right <span style="color: #F0F;">&amp;&amp;...</span>
       <span style="">cPoint</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &gt;= btm  <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &lt;= top
&nbsp;
      <span style="color: #0000FF;">if</span> ~inControl <span style="color: #228B22;">%we just entered</span>
        inControl = <span style="color: #0000FF;">true</span>;
        action.<span style="">actionPerformed</span><span style="color: #080;">&#40;</span>actionEvent<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%show the tooltip</span>
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">if</span> inControl <span style="color: #228B22;">%we just existed</span>
        inControl = <span style="color: #0000FF;">false</span>;
        <span style="color: #228B22;">%toggle to make it disappear when leaving button</span>
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%gpMouseMove</span>
&nbsp;
  <span style="color: #0000FF;">function</span> windowMotion<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">%illustrate that we can still do a regular window button motion callback</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>lbl,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Mouse position: %d, %d'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'CurrentPoint'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%windowMotion</span>
&nbsp;
  <span style="color: #0000FF;">function</span> chkTooltipEnable<span style="color: #080;">&#40;</span>src,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>src,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkTooltipEnable</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%inactiveBtnToolTip</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 218px"><img
alt="Tooltip on an inactive button" src="http://undocumentedmatlab.com/images/tooltip4.png" title="Tooltip on an inactive button" width="208" height="255" /><p
class="wp-caption-text">Tooltip on an inactive button</p></div></center></p><p>Comments on the code:</p><ol><li>The code illustrates that we can successfully add an additional listener to listen for mouse motion events while still carrying out the original <b>WindowButtonMotionFcn</b> callback. This makes chaining callbacks much easier.</li><li>The <b><i>handle.listener</i></b> object has an <b>Enable</b> property that we can use to temporarily turn the listener on and off. This can be seen in the <i>chkTooltipEnable()</i> callback for the check box in the code above. If we wanted to permanently remove the listener we would simply use <i><b>delete</b>(pointListener)</i>. Note that <b><i>addlistener</i></b> adds a hidden property to the object being listened to, so that the listener is tied to the object&#8217;s lifecycle. If you create a listener directly using <b><i>handle.listener</i></b> you are responsible for it&#8217;s disposition. Unfortunately, <b><i>addlistener</i></b> fails for HG handles on pre-R2009 Matlab releases, so we use <b><i>handle.listener</i></b> directly.</li><li>The code illustrates a good practice when tracking rapidly firing events like mouse movement of handling reentry into the callback while it is still processing a previous callback. Here we use a flag called inControl to prevent the <i>postTip</i> action being continuously fired while the mouse remains in the control.</li><li>I was unable to determine if there is any corresponding action for the <i>postTip</i> to dismiss tips so I resorted to using the ToolTipManager to toggle its own <b>Enable</b> property to cleanly hide the tooltip as the mouse leaves the control.</li></ol><p>Each Matlab callback has an associated event with it. Some of the ones that might be immediately useful at the figure-level are <b>WindowButtonDown</b>, <b>WindowButtonUp</b>, <b>WindowKeyPress</b>, and <b>WindowKeyRelease</b>. They can all be accessed through <b><i>handle.listener</i></b> or <b><i>addlistener</i></b> as in the code above.</p><p>Unfortunately, events do not always have names that directly correspond to the callback names. In order to see the list of available events for a particular Matlab object, use the following code, which relies on another undocumented function &#8211; <b><i>classhandle</i></b>. Here we list the events for <b><i>gcf</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Events'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'SerializeEvent'</span>
    <span style="color:#A020F0;">'FigureUpdateEvent'</span>
    <span style="color:#A020F0;">'ResizeEvent'</span>
    <span style="color:#A020F0;">'WindowKeyReleaseEvent'</span>
    <span style="color:#A020F0;">'WindowKeyPressEvent'</span>
    <span style="color:#A020F0;">'WindowButtonUpEvent'</span>
    <span style="color:#A020F0;">'WindowButtonDownEvent'</span>
    <span style="color:#A020F0;">'WindowButtonMotionEvent'</span>
    <span style="color:#A020F0;">'WindowPostChangeEvent'</span>
    <span style="color:#A020F0;">'WindowPreChangeEvent'</span></pre></div></div><p>Note that I have made extensive use of the <b><i>javaMethodEDT</b></i> function to execute Java methods that affect swing components on Swing&#8217;s Event Dispatch Thread. I plan to write about this and related functions in my next article.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Additional uicontrol tooltip hacks</title><link>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/</link> <comments>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/#comments</comments> <pubDate>Wed, 17 Feb 2010 17:25:52 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1114</guid> <description><![CDATA[Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I&#8217;d like to welcome guest blogger Matthew Whitaker. Today Matt will discuss uicontrol tooltips hacks as the first of several posts that follow a logical thread culminating in the long promised article on the EDT.</i><br
/> <br/></p><p>A while back Yair wrote a cool article, <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">Spicing up Matlab uicontrol tooltips</a>, describing use of HTML formatting and images in <b><i>uicontrol</i></b> tooltips. I want to share some limitations I&#8217;ve seen with tooltips and their solution using the Matlab control&#8217;s underlying Java object.</p><h3 id="disabled-controls">Situation 1: Displaying a tooltip on disabled controls</h3><p>One issue with the stock Matlab <b><i>uicontrol</i></b> tooltips is that if you turn the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol_props.html#bqxoiky">uicontrol&#8217;s <b>Enable</b> property</a> to &#8216;inactive&#8217; or &#8216;off&#8217;, its tooltip no longer displays. This is the behavior that we normally want, but occasionally we wish to display a tooltip on a disabled control, for example, to explain why the control is disabled.</p><p>You can use the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>findjobj</i></b> utility</a> to find the Java handle for the <b><i>uicontrol</i></b>. This handle can then be used to set the tooltip text. The tooltip will display if you disable the control using its Java handle&#8217;s <b>Enabled</b> property rather than the Matlab handle&#8217;s <b>Enable</b> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hButton = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jButton= findjobj<span style="color: #080;">&#40;</span>hButton<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'Enabled'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'This is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>As customary for Java objects, its properties can also be set using their corresponding accessor methods:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,jButton,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jButton,<span style="color:#A020F0;">'Button is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 262px"><img
alt="tooltip on a disabled uicontrol" src="http://UndocumentedMatlab.com/images/tooltip1.png" title="tooltip on a disabled uicontrol" width="252" height="46" /><p
class="wp-caption-text">tooltip on a disabled uicontrol</p></div></center></p><p>Unfortunately, this hack does not work for &#8216;inactive&#8217; controls. There is no direct Java analogy for inactive controls &#8211; it&#8217;s a Matlab extension. It appears that Matlab somehow intercepts the mouse events associated with inactive controls. My next post will show how event callback can be used to display tooltips for inactive controls.</p><p>As an alternative for inactive edit-box controls, we can simulate the inactive behavior by setting the Java object&#8217;s <b>Editable</b> property (or by using its <i>setEditable()</i> accessor method), then setting the tooltip. Note that the extremely-useful Java <b>Editable</b> property is unavailable in the Matlab handle, for some inexplicable reason:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'Editable'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'Text is inactive for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 242px"><img
alt="tooltip on a non-editable editbox" src="http://UndocumentedMatlab.com/images/tooltip2.png" title="tooltip on a non-editable editbox" width="232" height="50" /><p
class="wp-caption-text">tooltip on a non-editable editbox</p></div></center></p><h3 id="truncated_text">Situation 2: Displaying a tooltip on truncated text</h3><p>If we want to conditionally display a tooltip for an editbox <b><i>uicontrol</i></b> when the text exceeds the control&#8217;s width, we can use the <b>TipWhenTruncatedEnabled</b> property (or its corresponding <i>setTipWhenTruncatedEnabled()</i> method). This will display a tooltip with the editbox contents if the string is shown truncated. This saves the user having to scroll through the control to see its contents. I often use this for edit controls that may contain long path names:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.8</span>,<span style="color: #33f;">0.4</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Too Short'</span><span style="color: #080;">&#41;</span>;
hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.7</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Long Enough to Display a Tool Tip'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox1 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jEditbox2 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox1,<span style="color:#A020F0;">'TipWhenTruncatedEnabled'</span>,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% property-based alternative</span>
javaMethod<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setTipWhenTruncatedEnabled'</span>,jEditbox2,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% method-based alternative</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 260px"><img
alt="TipWhenTruncatedEnabled tooltip" src="http://UndocumentedMatlab.com/images/tooltip3.png" title="TipWhenTruncatedEnabled tooltip" width="250" height="100" /><p
class="wp-caption-text">TipWhenTruncatedEnabled tooltip</p></div></center></p><p>The <b>TipWhenTruncatedEnabled</b> property property is also available for multi-line editboxes, but has (obviously) no effect when scrollbars are present. Also note that setting the <b>TipWhenTruncatedEnabled</b> property to true overrides any previous tooltip that might have been set for the editbox.</p><p>Finally, note that the <b>TipWhenTruncatedEnabled</b> property can also be set for the editbox component of popup-menu (aka drop-down) controls, <u>after</u> they have been set to be editable using their Java <b>Editable</b> property (note that both properties are false by default for Matlab uicontrols). In the following screenshot, the drop-down&#8217;s editbox component contained an HTML snippet, that is shown unformatted within the edit-box and HTML-formatted in the de-truncated tooltip:</p><p><center><div
class="wp-caption aligncenter" style="width: 125px"><img
alt="de-truncated HTML-format tooltip" src="http://UndocumentedMatlab.com/images/popup_TipWhenTruncatedEnabled2.png" title="de-truncated HTML-format tooltip" width="115" height="72" /><p
class="wp-caption-text">de-truncated HTML-format tooltip</p></div></center></p><h3 id="tooltip_timing">Situation 3: Controlling tooltip timing</h3><p>As you have probably noticed, there is a slight delay between the time your mouse enters the control and when the tooltip actually appears. If you display a tooltip over a control for sufficiently long the tooltip will then disappear. Sometimes the default delays are too slow or fast for your application. These times can be controlled through the <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/ToolTipManager.html">javax.swing.ToolTipManager</a>. The ToolTipManager sets these parameters globally (including for your Matlab desktop components), but they are not persistent between sessions.</p><p>Some examples using the ToolTipManager:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is a button.'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is editable text'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%static method to get ToolTipManager object</span>
initialDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getInitialDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before display in milliseconds (=750 on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set tooltips to appear immediately</span>
&nbsp;
dismissDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getDismissDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before the tooltip disappears (=10000 (10 sec) on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,<span style="color: #33f;">2000</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set the dismiss delay to 2 seconds</span>
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%turns off all tooltips in system (including the Matlab desktop)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,initialDelay<span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,dismissDelay<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that I have made extensive use of the undocumented built-in <b><i>javaMethodEDT</i></b> function to execute Java methods that affect Swing components on the Swing Event Dispatch Thread (EDT). I plan to write about EDT following my next post on event callback chaining.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uicontrol-border/' rel='bookmark' title='Customizing uicontrol border'>Customizing uicontrol border</a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Continuous slider callback</title><link>http://undocumentedmatlab.com/blog/continuous-slider-callback/</link> <comments>http://undocumentedmatlab.com/blog/continuous-slider-callback/#comments</comments> <pubDate>Mon, 08 Feb 2010 09:13:39 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1052</guid> <description><![CDATA[Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab&#8217;s <a
target="_blank" rel="nofollow" href="http://blinkdagger.com/matlab/matlab-gui-tutorial-slider/">slider uicontrol</a> invokes the user callback only when the mouse button is released, and not continuously while the slider&#8217;s thumb is dragged. This functionality was again referred-to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/272224">yesterday</a>, and I decided it merits a dedicated post.</p><p>There are three distinct simple ways to achieve continuous callbacks:</p><h3 id="Java_Callbacks">Using Java callbacks</h3><p>As explained in an earlier <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">article</a>, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders&#8217; underlying Java objects, which are really not <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">JSliders</a> but <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#operation">JScrollBars</a>, have an <b>AdjustmentValueChangedCallback</b> property that is useful for our purposes and is accessible using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj utility</i></b></a>. Simply download <b><i>FindJObj</i></b> from the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">File Exchange</a>, and then:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSlider = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'slider'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jScrollBar = findjobj<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
jScrollBar.<span style="">AdjustmentValueChangedCallback</span> = @myCbFcn;
<span style="color: #228B22;">% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)</span></pre></div></div><p>Where <i>myCbFcn</i> is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider&#8217;s thumb is dragged.</p><h3 id="Event_Listener">Using an event listener</h3><p>An alternative to the Java route is to use Matlab&#8217;s undocumented <b><i>handle.listener</i></b> function to listen to the slider&#8217;s <b>Action</b> event, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSlider,<span style="color:#A020F0;">'ActionEvent'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>This alternative is used by Matlab&#8217;s own <b><i>imscrollpanel</i></b> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isJavaFigure
   <span style="color: #228B22;">% Must use these ActionEvents to get continuous events fired as slider</span>
   <span style="color: #228B22;">% thumb is dragged. Regular callbacks on sliders give only one event</span>
   <span style="color: #228B22;">% when the thumb is released.</span>
   hSliderHorListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>;
   hSliderVerListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollVertical<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hScrollpanel,<span style="color:#A020F0;">'sliderListeners'</span>,<span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span>hSliderHorListener hSliderVerListener<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Unfortunately, the event route is only available with Java Figures,</span>
   <span style="color: #228B22;">% so platforms without Java Figure support get discrete events only</span>
   <span style="color: #228B22;">% when the mouse is released from dragging the slider thumb.</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color:#A020F0;">'callback'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color:#A020F0;">'callback'</span>,@scrollVertical<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="Property_Listener">Using a property listener</h3><p>The <b><i>handle.listener</i></b> function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following <b>Value</b> property updates, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hhSlider = handle<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhSlider,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhSlider,hProp,<span style="color:#A020F0;">'PropertyPostSet'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>In addition to &#8216;PropertyPostSet&#8217;, we could also listen on &#8216;PropertyPreSet&#8217;, which is triggered immediately <u>before</u> the property is modified. There are also corresponding &#8216;*Get&#8217; options. In relatively old Matlab releases (I believe R2007b and earlier, but I&#8217;m not certain), the option names were simply &#8216;PostSet&#8217;, &#8216;PreSet&#8217; etc., without the &#8216;Property&#8217; prefix.</p><p>Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/continuous-slider-callback/feed/</wfw:commentRss> <slash:comments>30</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>14</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>18</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>GUI integrated HTML panel</title><link>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/</link> <comments>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/#comments</comments> <pubDate>Tue, 15 Dec 2009 12:30:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=865</guid> <description><![CDATA[Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/' rel='bookmark' title='HTML support in Matlab uicomponents'>HTML support in Matlab uicomponents</a> <small>Matlab uicomponents support HTML and CSS, enabling colored items, superscript/subscript, fonts, bold/italic/underline and many other modifications...</small></li><li><a
href='http://undocumentedmatlab.com/blog/panel-level-uicontrols/' rel='bookmark' title='Panel-level uicontrols'>Panel-level uicontrols</a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, I explained how a <a
target="_blank" href="http://undocumentedmatlab.com/blog/gui-integrated-browser-control/">browser control can be integrated in Matlab GUI applications</a>. Sometimes we only need to display simple HTML, for which a full browser seems like overkill. Moreover, we may wish to edit the displayed contents, which cannot be done using the browser control. The solution is to use a standard Java Swing <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html">JEditorPane</a> control, which is an editable HTML-aware control.</p><p>Oddly enough, it was only yesterday that Mikhail, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/author/99871">a known Matlab Java specialist on the CSSM newsgroup</a>, posted an example for this as answer to <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1903516/matlab-displaying-markup-html-or-other-format/1903990#1903990">a question on the StackOverflow</a> forum (slightly edited for clarity):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">mytext = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;body&gt;&lt;table border=&quot;1&quot;&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;tr&gt;&lt;th&gt;Month&lt;/th&gt;&lt;th&gt;Savings&lt;/th&gt;&lt;/tr&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;tr&gt;&lt;td&gt;January&lt;/td&gt;&lt;td&gt;$100&lt;/td&gt;&lt;/tr&gt;'</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #228B22;">% Create a figure with a scrollable JEditorPane</span>
hfig = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
je = javax.<span style="">swing</span>.<span style="">JEditorPane</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span>, mytext<span style="color: #080;">&#41;</span>;
jp = javax.<span style="">swing</span>.<span style="">JScrollPane</span><span style="color: #080;">&#40;</span>je<span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>hcomponent, hcontainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jp, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, hfig<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hcontainer, <span style="color:#A020F0;">'units'</span>, <span style="color:#A020F0;">'normalized'</span>, <span style="color:#A020F0;">'position'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Turn anti-aliasing on (R2006a, Java 5.0)</span>
java.<span style="">lang</span>.<span style="">System</span>.<span style="">setProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'awt.useSystemAAFontSettings'</span>, <span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
je.<span style="">setFont</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Font</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Arial'</span>, java.<span style="">awt</span>.<span style="">Font</span>.<span style="">PLAIN</span>, <span style="color: #33f;">13</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JEditorPane</span>.<span style="">HONOR_DISPLAY_PROPERTIES</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% This only works on Java 1.5 (Matlab R14SP2 to R2007a):</span>
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">SwingUtilities2</span>.<span style="">AA_TEXT_PROPERTY_KEY</span>, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 443px"><img
alt="Editable HTML-aware JEditorPane" src="http://UndocumentedMatlab.com/images/JEditorPane.png" title="Editable HTML-aware JEditorPane" width="433" height="184" /><p
class="wp-caption-text">Editable HTML-aware JEditorPane</p></div></center></p><p>Mikhail&#8217;s code included setting SwingUtilities2&#8242;s AA_TEXT_PROPERTY_KEY property for anti-aliased fonts. Unfortunately, <a
target="_blank" rel="nofollow" href="http://www.jroller.com/gfx/entry/be_ready_for_java_se">SwingUtilities2 was an unsupported and undocumented internal class in Java 1.5</a> (undocumented/unsupported by Sun, not MathWorks for a change&#8230;) and completely disappeared in Java 1.6 (which is bundled with Matlab R2007b onward). Therefore, SwingUtilities2 and its antialias property can only be used on Matlab releases R14SP2 (7.0.4) through R2007a (7.4) &#8211; other Matlab versions will throw an error.</p><p>Alternately, use JIDE&#8217;s AA_TEXT_PROPERTY_KEY (JIDE is bundled with Matlab and this is supported even on new Matlab releases &#8211; I will present JIDE in future articles).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">property = com.<span style="">jidesoft</span>.<span style="">swing</span>.<span style="">JideSwingUtilities</span>.<span style="">AA_TEXT_PROPERTY_KEY</span>;
je.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span>property, <span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Or, simply add the following switch to your <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C">java.opt file</a>:</p><pre>-Dswing.aatext=true</pre><p>With this switch, you no longer need to set anti-aliasing separately for each component. It is entirely harmless to set this switch even on Matlab/Java versions that do not support it (the switch is simply ignored in these cases).</p><p>Note that while JEditorPane&#8217;s support for HTML is extensive, it is incomplete. It also does not contain a JavaScript engine or other web-related features we have come to expect in a browser. For the more complex stuff we can use the browser control as explained in last week&#8217;s article.</p><p>Matlab&#8217;s own multi-line editbox <b><i>uicontrol</i></b> uses JEditorPane (or actually its derived-class <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html">JTextPane</a>) as an underlying component. This means that the simple-looking Matlab editbox is actually a powerful HTML-aware component. In order to use these hidden undocumented features we need the editbox&#8217;s underlying JTextPane handle. This is done using <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">the <b><i>FindJObj</i></b> utility</a>, which will be described in my next article. Following that, I will show how to <a
target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/">customize Matlab&#8217;s dull-looking editbox</a> into something much more powerful. Here&#8217;s a sample, to help you stay tuned:</p><p><center><div
class="wp-caption aligncenter" style="width: 260px"><img
alt="HTML contents in a regular Matlab editbox" src="http://UndocumentedMatlab.com/images/editbox10.png" title="HTML contents in a regular Matlab editbox" width="250" height="75" /><p
class="wp-caption-text">HTML contents in a regular Matlab editbox</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/' rel='bookmark' title='HTML support in Matlab uicomponents'>HTML support in Matlab uicomponents</a> <small>Matlab uicomponents support HTML and CSS, enabling colored items, superscript/subscript, fonts, bold/italic/underline and many other modifications...</small></li><li><a
href='http://undocumentedmatlab.com/blog/panel-level-uicontrols/' rel='bookmark' title='Panel-level uicontrols'>Panel-level uicontrols</a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/gui-integrated-html-panel/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>GUI integrated browser control</title><link>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/</link> <comments>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/#comments</comments> <pubDate>Wed, 09 Dec 2009 15:35:21 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=847</guid> <description><![CDATA[A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-html-panel/' rel='bookmark' title='GUI integrated HTML panel'>GUI integrated HTML panel</a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, I <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-help-popup-contents/">described the built-in PopupPanel object</a>, and showed how it can be used to present popup messages with HTML content and even entire webpages. I explained that PopupPanel uses an internal browser object to achieve this. In fact, Matlab&#8217;s browser object predates PopupPanel by many years and quite a few releases. This browser object can be used as a stand-alone component that we can easily embed in our Matlab GUI applications.</p><p>Here is a simple example in which a Matlab Listbox uicontrol is used to select the contents of an adjacent browser component:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a blank figure window</span>
f=<span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'Browser GUI demo'</span>,<span style="color:#A020F0;">'Num'</span>,<span style="color:#A020F0;">'off'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the browser object on the right</span>
jObject = com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">html</span>.<span style="">HTMLBrowserPanel</span>;
<span style="color: #080;">&#91;</span>browser,container<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jObject, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, f<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>container, <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.3</span>,<span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.65</span>,<span style="color: #33f;">0.9</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the URLs listbox on the left</span>
urls = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'www.cnn.com'</span>,<span style="color:#A020F0;">'www.bbc.co.uk'</span>,<span style="color:#A020F0;">'myLocalwebpage.html'</span>,<span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'www.Mathworks.com'</span>, <span style="color:#A020F0;">'UndocumentedMatlab.com'</span><span style="color: #080;">&#125;</span>;
hListbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'listbox'</span>, <span style="color:#A020F0;">'string'</span>,urls, <span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.05</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.9</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
        <span style="color:#A020F0;">'userdata'</span>,browser<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Set the listbox's callback to update the browser contents</span>
cbStr=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'strs = get(gcbo,'</span><span style="color:#A020F0;">'string'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'url = strs{get(gcbo,'</span><span style="color:#A020F0;">'value'</span><span style="color:#A020F0;">')};'</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser = get(gcbo,'</span><span style="color:#A020F0;">'userdata'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'msg=['</span><span style="color:#A020F0;">'&lt;html&gt;&lt;h2&gt;Loading '</span><span style="color:#A020F0;">' url '</span><span style="color:#A020F0;">' - please wait'</span><span style="color:#A020F0;">'];'</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser.setHtmlText(msg); pause(0.1); drawnow;'</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'browser.setCurrentLocation(url);'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hListbox,<span style="color:#A020F0;">'Callback'</span>,cbStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/Browser.png" target="_blank"><img
alt="Browser object integrated in Matlab GUI (click for large image)" src="http://UndocumentedMatlab.com/images/Browser.png" title="Browser object integrated in Matlab GUI (click for large image)" width="450" height="329" /></a><p
class="wp-caption-text">Browser object integrated in Matlab GUI</p></div></center></p><p>In this simple example, we can see how the Java browser object can easily be controlled by Matlab. Specifically, we use two modes of the browser: first we present an HTML message (&#8216;<b>Loading www.cnn.com &#8211; please wait</b>&#8216;) and then replacing this content with the actual webpage, if accessible. If the webpage is not accessible, an error message is displayed:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/Browser_404.png" target="_blank"><img
alt="Browser message when webpage is missing (click for large image)" src="http://UndocumentedMatlab.com/images/Browser_404.png" title="Browser message when webpage is missing (click for large image)" width="450" height="165" /></a><p
class="wp-caption-text">Browser message when webpage is missing</p></div></center></p><p>We can easily expand this simple example to display any HTML message or webpage, in a seamless integration within our GUI.</p><p>Now, who ever said that Matlab GUI looks static or boring???</p><p>In an unrelated note, I would like to extend good wishes to Ken Orr, who has left the Mathworks Desktop development team to join Apple <a
target="_blank" rel="nofollow" href="http://explodingpixels.wordpress.com/2009/12/04/heading-to-apple/">a few days ago</a>. You probably know Ken from his good work on the Desktop and the official <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/">Matlab Desktop blog</a>. Hopefully, in his new position Ken will be able to influence Mac Java in a way that will reduce the <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2009/08/31/pouncing-on-snow-leopard/">numerous recurring issues that afflict Matlab Mac releases</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-html-panel/' rel='bookmark' title='GUI integrated HTML panel'>GUI integrated HTML panel</a> <small>Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/gui-integrated-browser-control/feed/</wfw:commentRss> <slash:comments>8</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-05-21 20:33:26 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/uicontrol/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      1.858s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337657605; expires=Wed, 22-May-2013 03:33:25 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 22 May 2012 03:33:26 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 22 May 2012 04:33:26 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               43cac9fa369d9e368a154c255b6bf86c
Content-Encoding:   gzip
-->
