<?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; GUI</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/gui/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>Customizing menu items part 3</title><link>http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/</link> <comments>http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/#comments</comments> <pubDate>Wed, 09 May 2012 18:00:05 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Icons]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[JavaFrame]]></category> <category><![CDATA[Menubar]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2909</guid> <description><![CDATA[Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/' rel='bookmark' title='Customizing menu items part 1'>Customizing menu items part 1</a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In the past weeks I&#8217;ve shown how Matlab menus can be customized in a variety of undocumented manners, using HTML, <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/">pure Matlab</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/">Java</a>. Today I conclude this mini-series with an article that explains how to use the underlying Java object to customize menu item icons. Menu customizations are explored in depth in section 4.6 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">book</a>.</p><h3 id="underlying">A reminder: accessing the underlying Java object</h3><p>Matlab menus (<i><b>uimenu</b></i>) are basically simple wrappers for the much more powerful and flexible Java Swing <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html"><code>JMenu</code> and <code>JMenuItem</code></a> on which they are based.  Many important functionalities that are available in Java menus are missing from the Matlab <i><b>uimenu</b></i>s.</p><p>Getting the Java reference for the figure window&#8217;s main menu is very easy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jFrame = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hFig<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'JavaFrame'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% R2008a and later</span>
    jMenuBar = jFrame.<span style="">fHG1Client</span>.<span style="">getMenuBar</span>;
<span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% R2007b and earlier</span>
    jMenuBar = jFrame.<span style="">fFigureClient</span>.<span style="">getMenuBar</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>There are many customizations that can only be done using the Java handle: setting icons, several dozen callback types, tooltips, background color, font, text alignment, and so on. etc. Interested readers may wish to <i><b>get</b></i>/<i><b>set</b></i>/<i><b>inspect</b></i>/<i><b>methodsview</b></i>/<i><b><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object">uiinspect</a></b></i> the <code>jSave</code> reference handle and/or to read the documentation for <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JMenuItem.html"><code>JMenuItem</code></a>. Today&#8217;s article will focus on icon customizations.</p><h3 id="simple">Setting simple menu item icons</h3><p>Many of Matlab&#8217;s icons reside in either the <i>[matlabroot '/toolbox/matlab/icons/']</i> folder or the <i>[matlabroot '/java/jar/mwt.jar']</i> file (a JAR file is simply a zip file that includes Java classes and resources such as icon images). Let us create icons from the latter, to keep a consistent look-and-feel with the rest of Matlab (we could just as easily use our own external icon files):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% External icon file example</span>
jSave.<span style="">setIcon</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'C:\Yair\save.gif'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% JAR resource example</span>
jarFile = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'/java/jar/mwt.jar'</span><span style="color: #080;">&#41;</span>;
iconsFolder = <span style="color:#A020F0;">'/com/mathworks/mwt/resources/'</span>;
iconURI = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'jar:file:/'</span> jarFile <span style="color:#A020F0;">'!'</span> iconsFolder <span style="color:#A020F0;">'save.gif'</span><span style="color: #080;">&#93;</span>;
iconURI = java.<span style="">net</span>.<span style="">URL</span><span style="color: #080;">&#40;</span>iconURI<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% not necessary for external files</span>
jSave.<span style="">setIcon</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span>iconURI<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that setting a menu item&#8217;s icon automatically re-aligns all other items in the menu, including those that do not have an icon (an internal bug that was introduced in R2010a causes a misalignment, as shown below):</p><p><center><div
class="wp-caption aligncenter" style="width: 240px"><img
alt="Menu item with a custom Icon (R2009b)" src="http://UndocumentedMatlab.com/images/uimenu3.png" title="Menu item with a custom Icon (R2009b)" width="230" height="120"/><p
class="wp-caption-text">Menu item with a custom <b>Icon</b> (R2009b)</p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 237px"><img
alt="...and the same in R2010a onward" src="http://UndocumentedMatlab.com/images/uimenu3b.png" title="...and the same in R2010a onward" width="227" height="120"/><p
class="wp-caption-text">...and the same in R2010a onward</p></div></center></p><h3 id="checkmark">Checkmark icon</h3><p>The empty space on the left of the menu is reserved for the check mark. Each Matlab menu item is check-able, since it is an object that extends the <code>com.mathworks.mwswing.MJCheckBoxMenuItem</code> class. I have not found a way to eliminate this empty space, which is really unnecessary in the File-menu case (it is only actually necessary in the View and Tools menus). Note that if an icon is set for the item, both the icon and the checkmark will be displayed, side by side.</p><p>The check mark is controlled by the <b>State</b> property of the Java object (which accepts logical true/false values), or the <b>Checked</b> property of the Matlab handle (which accepts the regular &#8216;on&#8217;/'off&#8217; string values):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Set the check mark at the Matlab level</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuFileSave'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'Checked'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Equivalent - set the checkmark at the Java level</span>
jSave.<span style="">setState</span><span style="color: #080;">&#40;</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: 195px"><img
alt="State = true, Icon = [ ]" src="http://UndocumentedMatlab.com/images/uimenu_save_checked1.png" title="State = true, Icon = [ ]" width="185" height="138"/><p
class="wp-caption-text"><b>State</b> = true, <b>Icon</b> = &#91; &#93;</p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 215px"><img
alt="State = true, Icon = custom" src="http://UndocumentedMatlab.com/images/uimenu_save_checked2.png" title="State = true, Icon = custom" width="205" height="137"/><p
class="wp-caption-text"><b>State</b> = true, <b>Icon</b> = custom</p></div></center></p><h3 id="custom">Customizing menu icons</h3><p>Icons can be customized: modify the gap between the icon and the label with the <b>IconTextGap</b> property (default = 4 [pixels]); place icons to the right of the label by setting <b>HorizontalTextPosition</b> to <code>jSave.LEFT</code> (=2), or centered using <code>jSave.CENTER</code> (=0). Note that the above-mentioned misalignment bug does not appear in these cases:</p><p><center><div
class="wp-caption aligncenter" style="width: 232px"><img
alt="jSave.setHorizontalTextPosition(jSave.LEFT)" src="http://UndocumentedMatlab.com/images/uimenu4.png" title="jSave.setHorizontalTextPosition(jSave.LEFT)" width="222" height="121"/><p
class="wp-caption-text"><code>jSave.setHorizontalTextPosition<br
/>(jSave.LEFT)</code></p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 233px"><img
alt="jSave.setHorizontalTextPosition(jSave.CENTER)" src="http://UndocumentedMatlab.com/images/uimenu5.png" title="jSave.setHorizontalTextPosition(jSave.CENTER)" width="223" height="120"/><p
class="wp-caption-text"><code>jSave.setHorizontalTextPosition<br
/>(jSave.CENTER)</code></p></div></center></p><p>Note how the label text can be seen through (or on top of) the icon when it is centered. This feature can be used to create stunning menu effects as shown below. Note how the width and height of the menu item automatically increased to accommodate my new 77&#215;31 icon size (icons are normally sized 16&#215;16 pixels):</p><p><center><div
class="wp-caption aligncenter" style="width: 295px"><img
alt="Overlaid icon (HorizontalTextPosition = CENTER)" src="http://UndocumentedMatlab.com/images/uimenu6.png" title="Overlaid icon (HorizontalTextPosition = CENTER)" width="285" height="136"/><p
class="wp-caption-text">Overlaid icon (<b>HorizontalTextPosition</b> = CENTER)</p></div></center></p><p>To resize an icon programmatically before setting it in a Java component, we can use the following example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">myIcon = <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/matlabicon.gif'</span><span style="color: #080;">&#41;</span>;
imageToolkit = java.<span style="">awt</span>.<span style="">Toolkit</span>.<span style="">getDefaultToolkit</span>;
iconImage = imageToolkit.<span style="">createImage</span><span style="color: #080;">&#40;</span>myIcon<span style="color: #080;">&#41;</span>;
iconImage = iconImage.<span style="">getScaledInstance</span><span style="color: #080;">&#40;</span><span style="color: #33f;">32</span>,<span style="color: #33f;">32</span>,iconImage.<span style="">SCALE_SMOOTH</span><span style="color: #080;">&#41;</span>;
jSave.<span style="">setIcon</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span>iconImage<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Remember when rescaling images, particularly small ones with few pixels, that it is always better to shrink than to enlarge images: enlarging a small icon image might introduce a significant pixelization effect:</p><p><center><div
class="wp-caption aligncenter" style="width: 100px"><img
alt="16x16 icon image resized to 32x32" src="http://UndocumentedMatlab.com/images/button_icon2.png" title="16x16 icon image resized to 32x32" width="90" height="90"/><p
class="wp-caption-text">16x16 icon image resized to 32x32</p></div></center></p><p>Separate icons can be specified for a different appearance during mouse hover (<b>RolloverIcon</b>; requires <b>RolloverEnabled</b>=1), item click/press (<b>PressedIcon</b>), item selection (<b>SelectedIcon</b>, <b>RolloverSelectedIcon</b>, <b>DisabledSelectedIcon</b>), and disabled menu item (<b>DisabledIcon</b>). All these properties are empty ([]) by default, which applies a predefined default variation (image color filter) to the main item&#8217;s Icon. For example, let us modify <b>DisabledIcon</b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">myIcon = <span style="color:#A020F0;">'C:\Yair\Undocumented Matlab\Images\save_disabled.gif'</span>;
jSaveAs.<span style="">setDisabledIcon</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">ImageIcon</span><span style="color: #080;">&#40;</span>myIcon<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jSaveAs.<span style="">setEnabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 130px"><img
alt="Enabled, main Icon" src="http://UndocumentedMatlab.com/images/uimenu7a.png" title="Enabled, main Icon" width="120" height="135"/><p
class="wp-caption-text">Enabled, main <b>Icon</b></p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 170px"><img
alt="Disabled, default Icon variation" src="http://UndocumentedMatlab.com/images/uimenu7b.png" title="Disabled, default Icon variation" width="120" height="135"/><p
class="wp-caption-text">Disabled, default <b>Icon</b> variation</p></div> &nbsp;&nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 180px"><img
alt="Disabled, custom DisabledIcon" src="http://UndocumentedMatlab.com/images/uimenu7c.png" title="Disabled, custom DisabledIcon" width="120" height="135"/><p
class="wp-caption-text">Disabled, custom <b>DisabledIcon</b></p></div></center></p><p>Note the automatic graying of disabled menu items, including their icon. This effect can also be achieved programmatically using the static methods in <code>com.mathworks.mwswing.IconUtils</code>: <i>changeIconColor(), createBadgedIcon(), createGhostedIcon()</i>, and <i>createSelectedIcon()</i>. When we use a non-default custom <b>DisabledIcon</b>, it is used instead of the gray icon variant.</p><p>This concludes my mini-series of customizing menus in Matlab. If you have used any nifty customization that I have not mentioned, please post a comment about it <a
href="http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/#respond">below</a>.</p><p><span
class="alignleft"><img
alt="Ken &#038; Mike" src="http://UndocumentedMatlab.com/images/ken_and_mike.png" title="Ken &#038; Mike" width="98" height="83"/></span>In an unrelated note, I would like to extend good wishes to Mike Katz, who has left the MathWorks mobile development team to join Kinvey a few days ago. Mike has been with MathWorks since 2005 and has been responsible for maintaining the official <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/">MATLAB Desktop blog</a>, together with <a
target="_blank" href="http://undocumentedmatlab.com/blog/gui-integrated-browser-control/">Ken Orr</a>. I&#8217;m not sure yet which direction the Desktop blog will take, and by whom, but in any case it won&#8217;t be the same. You&#8217;re both missed, Mike &#038; Ken!</p><p
/>&nbsp;</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/' rel='bookmark' title='Customizing menu items part 1'>Customizing menu items part 1</a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Customizing menu items part 2</title><link>http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/</link> <comments>http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/#comments</comments> <pubDate>Wed, 02 May 2012 11:57:38 +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[Undocumented feature]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[JavaFrame]]></category> <category><![CDATA[Menubar]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2902</guid> <description><![CDATA[Matlab menu items can be customized in a variety of useful ways using their underlying Java object.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/' rel='bookmark' title='Customizing menu items part 1'>Customizing menu items part 1</a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/">explained</a> how to customize Matlab&#8217;s menu items using some undocumented tricks that do not need Java. Today I will show how using just a tiny bit of Java magic powder we can add much more complex customizations to menu items. Menu customizations are explored in depth in section 4.6 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">book</a>.</p><h3 id="underlying">Accessing the underlying Java object</h3><p>Matlab menus (<i><b>uimenu</b></i>) are basically simple wrappers for the much more powerful and flexible Java Swing <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html"><code>JMenu</code> and <code>JMenuItem</code></a> on which they are based.  Many important functionalities that are available in Java menus are missing from the Matlab <i><b>uimenu</b></i>s.</p><p>Getting the Java reference for the figure window&#8217;s main menu is very easy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jFrame = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hFig<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'JavaFrame'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% R2008a and later</span>
    jMenuBar = jFrame.<span style="">fHG1Client</span>.<span style="">getMenuBar</span>;
<span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% R2007b and earlier</span>
    jMenuBar = jFrame.<span style="">fFigureClient</span>.<span style="">getMenuBar</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that we have used the figure handle&#8217;s hidden <a
target="_blank" href="http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/#JavaFrame"><b>JavaFrame</b> property</a>, accessed through the reference&#8217;s <i><b>handle</b></i>() wrapper, to prevent an annoying warning message.</p><p>There are many customizations that can only be done using the Java handle: setting icons, several dozen callback types, tooltips, background color, font, text alignment, and so on. etc. Interested readers may wish to <i><b>get</b></i>/<i><b>set</b></i>/<i><b>inspect</b></i>/<i><b>methodsview</b></i>/<i><b><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object">uiinspect</a></b></i> the <code>jSave</code> reference handle and/or to read the documentation for <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JMenuItem.html"><code>JMenuItem</code></a>. Some useful examples are provided below.</p><h3 id="dynamic">Dynamic menu behavior</h3><p>As a first example of Java-based customization, let us add DHTML-like behavior to the menu, such that the menu items will automatically be displayed when the mouse hovers over the item, without waiting for a user mouse click. First, get the <code>jMenuBar</code> reference as described above. Now, set the <b>MouseEnteredCallback</b> to automatically simulate a user mouse click on each menu item using its <i>doClick()</i> method. Setting the callback should be done separately to each of the top-level menu components:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">for</span> menuIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> jMenuBar.<span style="">getComponentCount</span>
    jMenu = jMenuBar.<span style="">getComponent</span><span style="color: #080;">&#40;</span>menuIdx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
    hjMenu = handle<span style="color: #080;">&#40;</span>jMenu,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hjMenu,<span style="color:#A020F0;">'MouseEnteredCallback'</span>,<span style="color:#A020F0;">'doClick(gcbo)'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that using this mechanism may be awkward if the top-level menu does not have a sub-menu but is rather a stand-alone menu item. For example, if the top-level menu-item &#8220;Help&#8221; is a stand-alone menu button (i.e., there are no help menu-items, just the single Help item), then moving the mouse over this item will trigger the help event, although the user did not actually click on the item. To prevent this behavior, we should modify the code snippet above to only work on those menu items that have sub-items.</p><h3 id="accelerator">Custom accelerator shortcut keys</h3><p>As another example, Matlab automatically assigns a non-modifiable keyboard accelerator key modifier of <ctrl>, while JMenus allow any combination of Alt/Ctrl/Shift/Meta (depending on the platform). Let us modify the default File/Save accelerator key from &#8216;Ctrl-S&#8217; to &#8216;Alt-Shift-S&#8217; as an example. We need a reference for the &#8220;Save&#8221; menu item. Note that unlike regular Java components, menu items are retrieved using the <i>getMenuComponent()</i> method and not <i>getComponent()</i>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% File main menu is the first main menu item =&gt; index=0</span>
jFileMenu = jMenuBar.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Save menu item is the 5th menu item (separators included)</span>
jSave = jFileMenu.<span style="">getMenuComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%Java indexes start with 0!</span>
inspect<span style="color: #080;">&#40;</span>jSave<span style="color: #080;">&#41;</span> 	<span style="color: #228B22;">% just to be sure: label='Save' =&gt; good!</span>
&nbsp;
<span style="color: #228B22;">% Finally, set a new accelerator key for this menu item:</span>
jAccelerator = javax.<span style="">swing</span>.<span style="">KeyStroke</span>.<span style="">getKeyStroke</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'alt shift S'</span><span style="color: #080;">&#41;</span>;
jSave.<span style="">setAccelerator</span><span style="color: #080;">&#40;</span>jAccelerator<span style="color: #080;">&#41;</span>;</pre></div></div><p>That is all there is to it – the label is modified automatically to reflect the new keyboard accelerator key. More info on setting different combinations of accelerator keys and modifiers can be found in the official Java documentation for <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/6/docs/api/javax/swing/KeyStroke.html#getKeyStroke%28java.lang.String%29"><code>KeyStroke</code></a>.</p><p><center><div
class="wp-caption aligncenter" style="width: 212px"><img
alt="Modification of menu item accelerator and tooltip" src="http://UndocumentedMatlab.com/images/uimenu1.png" title="Modification of menu item accelerator and tooltip" width="202" height="200"/><p
class="wp-caption-text">Modification of menu item accelerator and tooltip</p></div></center></p><p>Note that the Save menu-item reference can only be retrieved after opening the File menu at least once earlier; otherwise, an exception will be thrown when trying to access the menu item. The File menu does NOT need to remain open – it only needs to have been opened sometime earlier, for its menu items to be rendered. This can be done either interactively (by selecting the File menu) or programmatically:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Simulate mouse clicks to force the File main-menu to open &amp; close</span>
jFileMenu.<span style="">doClick</span>; <span style="color: #228B22;">% open the File menu</span>
jFileMenu.<span style="">doClick</span>; <span style="color: #228B22;">% close the menu</span>
&nbsp;
<span style="color: #228B22;">% Now the Save menu is accessible:</span>
jSave = jFileMenu.<span style="">getMenuComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="tooltip">Tooltip and highlight</h3><p>For some unknown reason, MathWorks did not include a tooltip property in its Matlab menu handle, contrary to all the other Matlab GUI components. So we must use the Java handle, specifically the <b>ToolTipText</b> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jSave.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'modified menu item with tooltip'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Java menu items also contain a property called <b>Armed</b>, which is a logical value (default=false). When turned on, the menu item is highlighted just as when it is selected (see the <i>Save As&#8230;</i> menu item in the screenshot above). On a Windows system, this means a blue background:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jSave.<span style="">setArmed</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>When the item is actually selected and then de-selected, <b>Armed</b> reverts to a false (off) value. Alternating the <b>Armed</b> property value can achieve an effect of flashing the menu item.</p><h3 id="callbacks">Callbacks</h3><p>In addition to the standard Swing control callbacks discussed in an <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">earlier article</a>, menu items possess several additional callbacks that are specific to menu items, including:</p><ul><li><b>ActionPerformedCallback</b> – fired when the menu item is invoked</li><li><b>StateChangedCallback</b> – fired when the menu item is selected or deselected</li><li><b>MenuDragMouseXXXCallback</b> (XXX=Dragged/Entered/Exited/Released) – fired when the menu item is dragged, for the corresponding event</li><li><b>MenuKeyXXXCallback</b> (XXX=Pressed/Released/Typed) – fired when a keyboard click event occurs (the menu item&#8217;s accelerator was typed)</li></ul><p>Using these callbacks, together with the 30-odd standard Swing callbacks, we can control our menu&#8217;s behavior to a much higher degree than possible using the standard Matlab handle.</p><p>Next week, I will conclude this mini-series with an article explaining how to customize menu item icons.</ctrl></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/' rel='bookmark' title='Customizing menu items part 1'>Customizing menu items part 1</a> <small>Matlab menus can be customized in a variety of undocumented manners - first article of a series. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Customizing menu items part 1</title><link>http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/</link> <comments>http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/#comments</comments> <pubDate>Wed, 25 Apr 2012 18:14:08 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Menubar]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2897</guid> <description><![CDATA[Matlab menus can be customized in a variety of undocumented manners - first article of a series.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Over the past years, I have not posted articles dealing with menu items. I have shown how to <a
target="_blank" href="http://undocumentedmatlab.com/blog/modifying-default-toolbar-menubar-actions/">directly access menu items&#8217; hidden handles</a>, but not much more than that. A year ago I <a
target="_blank" href="http://undocumentedmatlab.com/blog/2010-perspective/">promised</a> a mini-series on menu customizations, and it&#8217;s time to keep my promise. In today&#8217;s article, the first in the mini-series, I will present several undocumented menu customization topics that rely on pure-Matlab (i.e, no Java today). The next article in this series will focus on Java-based customizations.</p><h3 id="Invoking">Invoking menu item callbacks</h3><p>As noted above, a figure window&#8217;s menu items can be directly accessed. Once we access a menu item&#8217;s handle, we can extract its <b>Callback</b> property and directly invoke it (for example, using the semi-documented <a
target="_blank" href="http://undocumentedmatlab.com/blog/hgfeval/"><i><b>hgfeval</b></i> function</a>). Note that menu callbacks are kept in <b>Callback</b>, while toolbar callbacks are kept in <b>ClickedCallback</b>.</p><p>Menu callbacks generally use internal <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> functions (i.e., having a readable help section but no doc, online help, or official support), which are part of Matlab&#8217;s uitools folder. These functions are specific to each top-level menu tree: <i><b>filemenufcn, editmenufcn, viewmenufcn, insertmenufcn, toolsmenufcn, desktopmenufcn, winmenu</b></i>, and <i><b>helpmenufcn</b></i> implement the figure&#8217;s eight respective top-level menu trees&#8217; callbacks. These functions accept an optional figure handle (otherwise, <i><b>gcbf</b></i> is assumed), followed by a string specifying the specific menu item whose action needs to be run. <i><b>webmenufcn</b></i> implements the Help menu&#8217;s Web Resources sub-menu callbacks in a similar manner.</p><p>Use of these functions makes it easy to invoke a menu action directly from our Matlab code: instead of accessing the relevant menu item and invoking its <b>Callback</b>, we simply find out the menu item string in advance and use it directly. For example,</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">filemenufcn FileClose;
editmenufcn<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'EditPaste'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><i><b>uimenufcn</b></i> is a related fully-undocumented (built-in) function, available since Matlab R11 (late 1990s). It accepts a figure handle (or the zero [0] handle to indicate the desktop) and action name. For example, the fully-documented <i><b>commandwindow</b></i> function uses the following code to bring the Command Window into focus:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uimenufcn<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color:#A020F0;">'WindowCommandWindow'</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Uitools">Customizing menus via uitools</h3><p><i><b>makemenu</b></i> is another semi-documented uitool function that enables easy creation of hierarchical menu trees with separators and accelerators. It is a simple and effective wrapper for <i><b>uimenu</b></i>. <i><b>makemenu</b></i> is a useful function that has been made obsolete (grandfathered) without any known replacement.</p><p><i><b>makemenu</b></i> accepts four parameters: a figure handle, a char matrix of labels (&#8216;&gt;&#8217; indicating sub item, &#8216;&gt;&gt;&#8217; indicating sub-sub items etc.; &#8216;&amp;&#8217; indicating keyboard shortcut; &#8216;^x&#8217; indicating an accelerator key; &#8216;-&#8217; indicating a separator line), a char matrix of callbacks, and an optional char matrix of tags (empty by default). <i><b>makemenu</b></i> makes use of another semi-documented grandfathered function, <i><b>menulabel</b></i>, to parse the specified label components. <i><b>makemenu</b></i> returns an array of handles of the created <i><b>uimenu</b></i> items:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">labels = str2mat<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&amp;File'</span>, <span style="color: #F0F;">...</span>    <span style="color: #228B22;">% File top menu</span>
           <span style="color:#A020F0;">'&gt;&amp;New^n'</span>, <span style="color: #F0F;">...</span>           <span style="color: #228B22;">% File=&gt;New</span>
           <span style="color:#A020F0;">'&gt;&amp;Open'</span>, <span style="color: #F0F;">...</span>            <span style="color: #228B22;">% File=&gt;Open</span>
           <span style="color:#A020F0;">'&gt;&gt;Open &amp;document^d'</span>, <span style="color: #F0F;">...</span>    <span style="color: #228B22;">% File=&gt;Open=&gt;doc</span>
           <span style="color:#A020F0;">'&gt;&gt;Open &amp;graph^g'</span>, <span style="color: #F0F;">...</span>       <span style="color: #228B22;">% File=&gt;Open=&gt;graph</span>
           <span style="color:#A020F0;">'&gt;-------'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% File=&gt;separator line</span>
           <span style="color:#A020F0;">'&gt;&amp;Save^s'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% File=&gt;Save</span>
           <span style="color:#A020F0;">'&amp;Edit'</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% Edit top menu</span>
           <span style="color:#A020F0;">'&amp;View'</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% View top menu</span>
           <span style="color:#A020F0;">'&gt;&amp;Axis^a'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% View=&gt;Axis</span>
           <span style="color:#A020F0;">'&gt;&amp;Selection region^r'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% View=&gt;Selection</span>
calls = str2mat<span style="color: #080;">&#40;</span><span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% no action: File top menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'New'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Open sub-menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Open doc'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Open graph'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Separator</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Save'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Edit top menu</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: View top menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'View axis'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'View selection region'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;
handles = makemenu<span style="color: #080;">&#40;</span>hFig, labels, calls<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'menuBar'</span>,<span style="color:#A020F0;">'none'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 277px"><img
alt="A simple figure menu" src="http://UndocumentedMatlab.com/images/makemenu.png" title="A simple figure menu" width="267" height="148"/><p
class="wp-caption-text">A simple figure menu</p></div></center></p><h3 id="HTML">Customizing menus via HTML</h3><p>Since menu items share the same <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">HTML/CSS support feature</a> as all Java Swing labels, we can specify font size/face/color, bold, italic, underline, superscript/subscript, and practically any HTML formatting.</p><p>Note that some features, such as the font or foreground/background colors, have specific properties that we can set using the Java handle, instead of using HTML. The benefit of using HTML is that it enables setting all the formatting in a single property. HTML does not require using Java – just pure Matlab (see the following example).</p><p>Multi-line menu items can easily be done with HTML: simply include a <code>&lt;br&gt;</code> element in the label – the menu item will split into two lines and automatically resize vertically when displayed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">txt1 = <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;&lt;u&gt;&lt;i&gt;Save&lt;/i&gt;&lt;/u&gt;'</span>;
txt2 = <span style="color:#A020F0;">'&lt;font color=&quot;red&quot;&gt;&lt;sup&gt;this file&lt;/sup&gt;&lt;/font&gt;&lt;/b&gt;&lt;/html&gt;'</span>;
txt3 = <span style="color:#A020F0;">'&lt;br /&gt;this file as...'</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuFileSave'</span><span style="color: #080;">&#41;</span>,   <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span>txt1,txt2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuFileSaveAs'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span>txt1,txt3<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 195px"><img
alt="A multi-line HTML-rendered menu item" src="http://UndocumentedMatlab.com/images/uimenu2.png" title="A multi-line HTML-rendered menu item" width="185" height="184"/><p
class="wp-caption-text">A multi-line HTML-rendered menu item</p></div></center></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>hMenuItem, <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;2: C:\My Documents\doc.txt&lt;br /&gt;'</span>
   <span style="color:#A020F0;">'&lt;font size=&quot;-1&quot; face=&quot;Courier New&quot; color=&quot;red&quot;&gt;&amp;nbsp;&amp;nbsp; '</span>
   <span style="color:#A020F0;">'Date: 15-Jun-2011 13:23:45&lt;br /&gt;&amp;nbsp;&amp;nbsp; Size: 123 KB&lt;/font&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 421px"><img
alt="HTML-rendered menu items" src="http://UndocumentedMatlab.com/images/uimenu2b.png" title="HTML-rendered menu items" width="411" height="246"/><p
class="wp-caption-text">HTML-rendered menu items</p></div></center></p><p>Much more complex customizations can be achieved using Java. So stay tuned to part 2 of this mini-series&#8230;</p><p>Note: Menu customization is explored in depth in section 4.6 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">book</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Specialized Matlab plots</title><link>http://undocumentedmatlab.com/blog/specialized-matlab-plots/</link> <comments>http://undocumentedmatlab.com/blog/specialized-matlab-plots/#comments</comments> <pubDate>Wed, 11 Apr 2012 17:25:15 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[File Exchange]]></category> <category><![CDATA[JFreeChart]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2849</guid> <description><![CDATA[The new MathWorks Plot Gallery provides access to some plotting examples on the File Exchange. Numerous others are available, extending the customizability of Matlab graphics.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/' rel='bookmark' title='JFreeChart graphs and gauges'>JFreeChart graphs and gauges</a> <small>JFreeChart is an open-source charting library that can easily be integrated in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/' rel='bookmark' title='Matlab layout managers: uicontainer and relatives'>Matlab layout managers: uicontainer and relatives</a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</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/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>A few weeks ago, MathWorks has added a new section on its website called the &#8220;<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/discovery/gallery.html">Plot Gallery</a>&#8220;. This section highlights specialized Matlab plots that were posted on the Matlab File Exchange (FEX) and can be used by Matlab users in their GUIs. Unfortunately, for the moment this list only includes some 20 customized plots posted by MathWorks employees, in addition to some 50 examples of using the standard Matlab plots. I have a suspicion that one day, if this proves popular, we will find the gallery integrated in the main Matlab Desktop, possibly next to the current <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-type-selection-components/">plot selector component</a>.</p><p>Since the Plot Gallery section is a new effort at reducing plotting complexity to Matlab users, I think that it should be encouraged by user feedback. Please feel free to leave them a comment with your feedback, either <a
href="http://undocumentedmatlab.com/blog/specialized-matlab-plots/#respond">on this page</a>, or on their semi-official blog <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/pick/2012/03/23/matlab-plot-gallery/">announcement page</a>.</p><p>My personal beef with the new Plot Gallery is the fact that it does not currently include user submissions. FEX actually contains numerous user-submitted specialized plotting utilities, answering a wide variety of needs in multiple discipline fields. These can easily be found using FEX&#8217;s search functionality or tags. For example, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/?term=tag%3A%22plot%22">the FEX &#8220;plot&#8221; tag</a> currently has 440 separate submissions. Perhaps one day these user submissions will be added to the Plot Gallery in some searchable structured manner.</p><p>One set of specialized plots that I&#8217;d like to highlight today is based on the open-source JFreeChart, that I have <a
target="_blank" href="http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/">described here</a> last year. Sven Körner has used this to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A92825+JFreeChart">provide</a> a dozen different customized Matlab plots, a few of which can be seen below, showing the power of integrating external code in Matlab GUI. JFreeChart contains many more customizable chart, plot and gauge types so Sven&#8217;s examples should merely be considered as appetizers:</p><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
alt="MATLAB-integrated dial gauge chart" src="http://UndocumentedMatlab.com/images/dialdemo4_300.jpg" title="MATLAB-integrated dial gauge chart" width="300" height="407"/><p
class="wp-caption-text">MATLAB-integrated dial gauge chart</p></div><br
/><div
class="wp-caption aligncenter" style="width: 410px"><img
alt="MATLAB-integrated multi-axes chart" src="http://UndocumentedMatlab.com/images/multipleAxis_400.jpg" title="MATLAB-integrated multi-axes chart" width="400" height="406"/><p
class="wp-caption-text">MATLAB-integrated multi-axes chart</p></div><br
/></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/' rel='bookmark' title='JFreeChart graphs and gauges'>JFreeChart graphs and gauges</a> <small>JFreeChart is an open-source charting library that can easily be integrated in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/' rel='bookmark' title='Matlab layout managers: uicontainer and relatives'>Matlab layout managers: uicontainer and relatives</a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</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/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/specialized-matlab-plots/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Using spinners in Matlab GUI</title><link>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/</link> <comments>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/#comments</comments> <pubDate>Wed, 25 Jan 2012 20:00: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[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2679</guid> <description><![CDATA[Spinner controls can easily be added to Matlab GUI. This article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/' rel='bookmark' title='Blurred Matlab figure window'>Blurred Matlab figure window</a> <small>Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>One of the few standard Java Swing controls that does not have any Matlab uicontrol counterpart is <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html"><code>JSpinner</code></a>. <code>JSpinner</code> is basically an editbox with two tiny adjacent up/down buttons. Spinners are similar in functionality to a combo-box (a.k.a. drop-down or pop-up menu), where a user can switch between several pre-selected values. They are often used when the list of possible values is too large to display in a combo-box menu. Like combo-boxes, spinners too can be editable (meaning that the user can type a value in the editbox) or not (the user can only &#8220;spin&#8221; the value using the up/down buttons).</p><p><code>JSpinner</code> uses an internal data model, similarly to <code>JTree</code>, <code>JTable</code> and other complex controls. The default model is <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerNumberModel.html"><code>SpinnerNumberModel</code></a>, which defines a min/max value (unlimited=[] by default) and step-size (1 by default). Additional predefined models are <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerListModel.html"><code>SpinnerListModel</code></a> (which accepts a cell array of possible string values) and <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerDateModel.html"><code>SpinnerDateModel</code></a> (which defines a date range and step unit).</p><p>Here&#8217;s a basic code snippet showing how to display a simple numeric spinner for numbers between 20 and 35, with an initial value of 24 and increments of 1:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jModel = javax.<span style="">swing</span>.<span style="">SpinnerNumberModel</span><span style="color: #080;">&#40;</span><span style="color: #33f;">24</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">35</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
jSpinner = javax.<span style="">swing</span>.<span style="">JSpinner</span><span style="color: #080;">&#40;</span>jModel<span style="color: #080;">&#41;</span>;
jhSpinner = javacomponent<span style="color: #080;">&#40;</span>jSpinner, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The spinner value can be set using the edit-box or by clicking on one of the tiny arrow buttons, or programmatically by setting the <b>Value</b> property. The spinner object also has related read-only properties <b>NextValue</b> and <b>PreviousValue</b>. The spinner&#8217;s model object has the corresponding <b>Value</b> (settable), <b>NextValue</b> (read-only) and <b>PreviousValue</b> (read-only) properties. In addition, the model also has the settable <b>Maximum</b>, <b>Minimum</b> and <b>StepSize</b> properties.</p><p>To attach a data-change callback, set the spinner&#8217;s <b>StateChangedCallback</b> property.</p><p>I have created a small Matlab demo, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26970-spinnerdemo"><i><b>SpinnerDemo</b></i></a>,  which demonstrates usage of <code>JSpinner</code> in Matlab figures. Each of the three predefined models (number, list, and date) is presented, and the spinner values are inter-connected via their callbacks. The Matlab code is modeled after the <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SpinnerDemoProject/src/components/SpinnerDemo.java">Java code</a> that is used to document <code>JSpinner</code> in the official documentation. Readers are welcome to download this demo from the Matlab File Exchange and reuse its source code.</p><p><center><div
class="wp-caption aligncenter" style="width: 225px"><img
alt="Java's SpinnerDemo" src="http://UndocumentedMatlab.com/images/SpinnerDemoJava.png" title="Java's SpinnerDemo" width="215" height="134"/><p
class="wp-caption-text">Java's SpinnerDemo</p></div> &nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 198px"><img
alt="My Matlab SpinnerDemo" src="http://UndocumentedMatlab.com/images/SpinnerDemoMatlab.png" title="My Matlab SpinnerDemo" width="188" height="184"/><p
class="wp-caption-text">My Matlab SpinnerDemo</p></div></center></p><p>As can be seen from the screenshot, <i><b>SpinnerDemo</b></i> also demonstrates how to attach a label to a GUI control with an associated accelerator key (Alt-D in the screenshot example, which sets the focus to the Date control).</p><p>An internal component in Matlab, namely <code>com.mathworks.mwswing.MJSpinner</code>, extends <code>javax.swing.JSpinner</code>, but in this particular case I cannot see any big advantage of using the internal <code>MJSpinner</code> rather than the standard <code>JSpinner</code>. On the contrary, using <code>JSpinner</code> will likely improve forward compatibility &#8211; MathWorks may well change <code>MJSpinner</code> in the future, but it cannot do anything to the standard Swing <code>JSpinner</code>. In other cases, internal Matlab controls do offer significant advantages over the standard Swing controls, but not here it would seem. In any case, the <i><b>SpinnerDemo</b></i> utility uses <code>MJSpinner</code>, but you can safely use <code>JSpinner</code> instead (line #86).</p><p>The internal Matlab controls are discussed in detail in Chapter 5 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">Matlab-Java book</a>, and <code>MJSpinner</code> is specifically discussed in section 5.2.1.</p><p>Another <code>JSpinner</code> derivative is JIDE&#8217;s <code>com.jidesoft.grid.SpinnerCellEditor</code>, which can be used as the cell-editor component in tables. An example of this was shown in the article about <a
target="_blank" href="http://undocumentedmatlab.com/blog/advanced-jide-property-grids/">Advanced JIDE Property Grids</a> (and section 5.7.5 in the book). You may also be interested in the <code>com.jidesoft.combobox.DateSpinnerComboBox</code>, which presents a control that includes both a date-selection combo-box and a spinner (section 5.7.2):</p><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
alt="A property grid with spinner control" src="http://UndocumentedMatlab.com/images/PropertyGrid_types.png" title="A property grid with spinner control" width="200" height="165" /><p
class="wp-caption-text">A property grid with spinner control</p></div> &nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 289px"><img
alt="JIDE's DateSpinnerComboBox" src="http://UndocumentedMatlab.com/images/DateSpinnerComboBox.png" title="JIDE's DateSpinnerComboBox" width="279" height="228" /><p
class="wp-caption-text">JIDE's DateSpinnerComboBox</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/' rel='bookmark' title='Blurred Matlab figure window'>Blurred Matlab figure window</a> <small>Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Common javacomponent problems</title><link>http://undocumentedmatlab.com/blog/common-javacomponent-problems/</link> <comments>http://undocumentedmatlab.com/blog/common-javacomponent-problems/#comments</comments> <pubDate>Wed, 07 Dec 2011 18:00:38 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[GUIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2607</guid> <description><![CDATA[The javacomponent function is very useful for placing Java components on-screen, but has a few quirks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/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/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>The <i><b>javacomponent</b></i> function, which I described <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent/">here</a> last year, is a very important built-in Matlab function that enables placing Java components in Matlab figure GUI. Using <i><b>javacomponent</b></i> is pretty straight-forward. However, there are a few quirks that users should be aware of. In today&#8217;s article I&#8217;ll try to highlight some of them and discuss workarounds.</p><h3 id="figure-vis">Figure visibility</h3><p>Java components can only be placed onscreen when their containing Matlab figure has been made visible. This means that calls to <i><b>javacomponent</b></i> cannot be placed at the GUIDE-created *_OpeningFcn() function, because that function is invoked <i>before</i> the figure window is made visible.</p><p>Of course, we can always force the figure to become visible by setting the figure&#8217;s <b>Visible</b> property to <code>'on'</code> in this function, before calling our <i><b>javacomponent</b></i>s. But IMHO, a better option would be to simply place the <i><b>javacomponent</b></i>s in the corresponding GUIDE-created *_OutputFcn() function, which is invoked <i>after</i> the figure window is made visible.</p><h3 id="parent-vis">Auto-hiding with parent container</h3><p>Java components are not automatically hidden with their ancestor container panel. This is also the root cause of the failure of Java components to disappear when switching tabs in a <i><b>uitab</b></i>.</p><p>One simple workaround for this that I often use is to link the <b>Visible</b> properties of the <i><b>javacomponent</b></i> container and the parent container:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = 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>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, hParent<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hParent, <span style="color:#A020F0;">'linked_props__'</span>, linkprop<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>hParent,hContainer<span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'Visible'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This has indeed been fixed in R2010b. If you ask me, this should have been standard behavior of <i><b>javacomponent</b></i> since the very beginning&#8230;</p><p>Although there is no need for the workaround in R2010b onward, I usually keep the workaround because it doesn&#8217;t hurt and enables backward compatibility for users who may have an older Matlab release.</p><h3 id="parent-types">Possible parent container types</h3><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> (some of these are not documented as possible parents in some Matlab releases, but they are). Since R2008a, parents of type <a
target="_blank" href="http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/"><i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i></a> can also be used. Unfortunately, frames are not <i><b>uicontainer</b></i>s and, therefore, cannot be used as <i><b>javacomponent</b></i> parents.</p><p>Note: Due to a bug in R2007a, <i><b>javacomponent</b></i>s cannot be added to <i><b>uicontainer</b></i>s, since <i>javacomponent.m</i> checks if <code>isa(hParent,'uicontainer')</code> (and similarly for <code>'uiflowcontainer', 'uigridcontainer'</code>), instead of <code>isa(hParent,'hg.uicontainer')</code> (and similarly for the others). If we modify <i>javacomponent.m</i> accordingly (add &#8220;hg.&#8221; in lines 98-100), this bug will be fixed. Since R2007b, <code>isa(…,'hg.uicontainer')</code> is equivalent to <code>isa(…,'uicontainer')</code>, so this fix is unnecessary.</p><h3 id="inputs">Input parameters</h3><p>Unlike many other Matlab functions, <i><b>javacomponent</b></i> does not accept optional parameter-value (P-V) pairs. If we want to customize the appearance of the Java component, it is better to create it , customize it, and only then to present it onscreen using <i><b>javacomponent</b></i>. If we first present the component and then customize it, there might be all sorts of undesirable flicker effects while the component is changing its properties.</p><p>One of the parameters that unfortunately cannot be customized before calling <i><b>javacomponent</b></i> is the component&#8217;s position onscreen. <i><b>javacomponent</b></i> only accepts a position vector in pixel units. To modify the component to use normalized units, we need to modify the container&#8217;s properties:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = 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>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><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;">'norm'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Similarly, we can set the container&#8217;s <b>UserData</b> and <b>ApplicationData</b> only after the call to <i><b>javacomponent</b></i>.</p><h3 id="bg-color">Background color</h3><p>The default background color of <i><b>javacomponent</b></i>s is a slightly different shade of gray than the default <i><b>uicontrol</b></i> background color. Please refer to my recent <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent-background-color/">article</a> for a detailed discussion of this issue.</p><h3 id="alignment">Vertical alignment</h3><p>Java components are slightly mis-aligned vertically with combo-box (<b>Style</b>=&#8217;popup&#8217;) <i><b>uicontrol</b></i>s, even when positioned using the same Y position. This is actually due to an apparent bug in Matlab&#8217;s implementation of the combo-box <i><b>uicontrol</b></i>, and not in the Java component&#8217;s: Apparently, the Matlab control does not obey its specified height and uses some other default height.</p><p>If we place <i><b>javacomponent</b></i>s side-by-side with a regular Matlab popup <i><b>uicontrol</b></i>s and give them all the same Y position, we can see this mis-alignment. It is only a few pixels, but the effect is visible and disturbing. To fix it, we need to add a small offset to the <i><b>javacomponent</b></i>&#8216;s container&#8217;s <b>Position</b> property to make both the initial Y position slightly lower, and the height value slightly higher than the values for the corresponding Matlab combo-box control.</p><h3 id="callbacks">Callbacks</h3><p>Unlike Matlab <i><b>uicontrol</b></i> callbacks, Java callbacks are activated even when the affected value does not change. Therefore, setting a value in the component&#8217;s callback could well cause an infinite loop of invoked callbacks.</p><p>Matlab programmers often use the practice of modifying component value within the callback function, as a means of fixing user-entered values to be within a certain data range, or in order to format the displayed value. This is relatively harmless in Matlab (if done correctly) since updating a Matlab <i><b>uicontrol</b></i>&#8216;s value to the current value does not retrigger the callback. But since this is not the case with Java callbacks, users should beware not to use the same practice there. In cases where this cannot be avoided, users should at least implement some sort of <a
target="_blank" href="http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/">callback re-entrancy prevention logic</a>.</p><h3 id="EDT">EDT</h3><p>Java components typically need to use the independent Java Event processing Thread (EDT). EDT is very important for Matlab GUI, as explained in <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">this article</a>. Failure to use EDT properly in Matlab can lead to unexpected GUI behavior and even Matlab hangs or crashes.</p><p>If the <i><b>javacomponent</b></i> function is called in a very specific syntax format where the first input arg is a string (the name of the Java class to be created), then the newly-created component is placed on the EDT. However, this is not the normal manner in which <i><b>javacomponent</b></i> is used: A much more typical use-case is where <i><b>javacomponent</b></i> is passed a reference handle to a previously-created Java component. In such cases, it is the user&#8217;s responsibility to place the component on the EDT. Until R2008a this should be done using the <i><b>awtcreate</b></i> function; since R2008b, we can use the much simpler <i><b>javaObjectEDT</b></i> function (<i><b>javaObjectEDT</b></i> actually existed since R2008a, but was buggy on that release so I suggest using it only starting in R2008b; it became documented starting in R2009a). In fact, modern <i><b>javacomponent</b></i> saves us the trouble, by automatically using <i><b>javaObjectEDT</b></i> to auto-delegate the component onto the EDT, even if we happen to have created it on the MT.</p><p>The paragraph above may lead us to believe that we only need to worry about EDT in R2008a and earlier. Unfortunately, this is not the case. Modern GUI requires using many sub-components, that are attached to their main component (e.g., CellRenderers and CellEditors). <i><b>javacomponent</b></i> only bothers to place the main component on the EDT &#8211; not any of the sub-components. We need to handle these separately. Liberally auto-delegating components to the EDT seems like a safe and painless habit to have.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/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/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/common-javacomponent-problems/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Multi-line tooltips</title><link>http://undocumentedmatlab.com/blog/multi-line-tooltips/</link> <comments>http://undocumentedmatlab.com/blog/multi-line-tooltips/#comments</comments> <pubDate>Thu, 03 Nov 2011 02:05:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Tooltip]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2518</guid> <description><![CDATA[Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I often use tooltips in my Matlab GUIs. They are a fantastically intuitive and unobtrusive visual helper for users to understand the GUI. In this blog, I have already posted several articles about how to tweak tooltip contents (<a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">here</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">here</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/">here</a>). Today I would like to expand on one of the aspects that were already covered, namely that of multi-line tooltips.</p><h3 id="Basic">Basic multi-line tooltips</h3><p>The basic multi-line tooltip consists of a simple string that includes the newline character. for example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'this is line 1'</span> <span style="color: #33f;">10</span> <span style="color:#A020F0;">'and this is line 2'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p>Or better (platform independent):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'this is line 1\nand this is line 2'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="A simple multi-line tooltip string" src="http://UndocumentedMatlab.com/images/tooltip_multi-line1.png" title="A simple multi-line tooltip string" width="120" /><p
class="wp-caption-text">A simple multi-line tooltip string</p></div></center></p><p>As can be seen, this is very simple to set up. Unfortunately, the font cannot be customized. Which leads us to:</p><h3 id="HTML">HTML-rendered multi-line tooltips</h3><p>Tooltips, like most other Matlab controls, <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">supports HTML rendering</a>. This is a natural corollary of the fact that Java Swing, on which all Matlab controls are based, <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/html.html">automatically supports HTML</a>. All we need to do is to add &#8216;<code>&lt;HTML&gt;</code>&#8216; at the very beginning of our tooltip string, and then we can embed HTML tags within the rest of the string:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'tooltipString'</span>, <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;line #1&lt;/b&gt;&lt;br /&gt;&lt;i&gt;&lt;font color=&quot;red&quot;&gt;line#2&lt;/font&gt;&lt;/i&gt;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Multi-line HTML'ed tooltip" src="http://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/html2.png" title="Multi-line HTML'ed tooltip" width="54" /><p
class="wp-caption-text">Multi-line HTML'ed tooltip</p></div></center></p><p>And a more sophisticated example, used within my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">createTable utility</a> on the File Exchange:</p><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="A more complex multi-line HTML-based tooltip" src="http://UndocumentedMatlab.com/images/table.png" title="A more complex multi-line HTML-based tooltip" width="597" /><p
class="wp-caption-text">A more complex multi-line HTML-based tooltip</p></div></center></p><p>Note that there is no need to close the final HTML tags in the tooltip string, although it&#8217;s not an error to do so (as I have done above).</p><h3 id="Problem">The problem with formatted multi-line tooltip</h3><p>Unfortunately, none of these two methods work when we wish to display formatted multi-line tooltips. For example, suppose that we wish to display struct data in a tooltip:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myData = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>,<span style="color: #0000FF;">pi</span>, <span style="color:#A020F0;">'b'</span>,-<span style="color: #33f;">4</span>, <span style="color:#A020F0;">'very_long_field_name'</span>,<span style="color:#A020F0;">'short txt'</span><span style="color: #080;">&#41;</span>
myData = 
                       a<span style="color: #F0F;">:</span> <span style="color: #33f;">3.14159265358979</span>
                       b<span style="color: #F0F;">:</span> -<span style="color: #33f;">4</span>
    very_long_field_name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'short txt'</span>
&nbsp;
&gt;&gt; myDataStr = <span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp(myData)'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, myDataStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line2.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>If we try to use HTML, the result looks even worse, because if the HTML-renderer detects a newline character embedded in the string, it simply uses the regular (non-HTML) renderer:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Failure to parse a string using HTML" src="http://UndocumentedMatlab.com/images/tooltip_multi-line3.png" title="Failure to parse a string using HTML" width="200" /><p
class="wp-caption-text">Failure to parse a string using HTML</p></div></center></p><p>Even if we fix this by replacing all line-separators with &#8216;<code>&lt;br/&gt;</code>&#8216;, we still get a badly-formatted tooltip, because HTML ignores all white spaces:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myDataStr2 = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>myDataStr, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\n'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'&lt;br /&gt;'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line4.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>(you can see that it&#8217;s HTML-formatted by the fact that the tooltip contents do not have internal margins like in the non-HTML tooltip above)</p><h3 id="Fixed-width">Fixed-width font multi-line tooltips</h3><p>We now recall the HTML <code>&lt;pre&gt;</code> tag, which tells HTML not to ignore white-spaces. In most web browsers, it also defaults to using a fixed-width font. Unfortunately, this is not the case here:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;'</span> myDataStr2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 250px"><img
alt="Still not quite right..." src="http://UndocumentedMatlab.com/images/tooltip_multi-line5.png" title="Still not quite right..." width="240" /><p
class="wp-caption-text">Still not quite right...</p></div></center></p><p>Which brings us to the final customization of the day, namely explicitly forcing the tooltip to use a fixed-width font:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;&lt;font face=&quot;courier new&quot;&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 330px"><img
alt="Finally - a well-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line6.png" title="Finally - a well-formatted multi-line tooltip" width="320" /><p
class="wp-caption-text">Finally - a well-formatted multi-line tooltip</p></div></center></p><p>Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.</html></pre><p></html></pre><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/multi-line-tooltips/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Uitable cell colors</title><link>http://undocumentedmatlab.com/blog/uitable-cell-colors/</link> <comments>http://undocumentedmatlab.com/blog/uitable-cell-colors/#comments</comments> <pubDate>Thu, 27 Oct 2011 01:51:53 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2476</guid> <description><![CDATA[A few Java-based customizations can transform a plain-looking data table into a lively colored one.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A client recently asked me to develop an application that will help discover technical indicators for trading on the stock market. The application was very data-intensive and the analysis naturally required visual presentations of number-crunching results in a readable manner. One of the requirements was to present numeric results in a data table, so we naturally use <i><b>uitable</b></i> for this.</p><p>Today I will show how using some not-so-complex Java we can transform the standard Matlab <i><b>uitable</b></i> into something much more useful.</p><h3 id="basic">First pass &#8211; basic data table</h3><p>We start by displaying the data in a simple <i><b>uitable</b></i>. The essence of the relevant code snippet was something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">headers = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Periods'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Any period&lt;br /&gt;returns&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg return&lt;br /&gt;signal&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Gain/draw&lt;br /&gt;ratio&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Random&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Signal&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Payout'</span>, <span style="color:#A020F0;">'% p-value'</span><span style="color: #080;">&#125;</span>;
hTable = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Data'</span>,data, <span style="color:#A020F0;">'ColumnEditable'</span>,<span style="color: #0000FF;">false</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnName'</span>,headers, <span style="color:#A020F0;">'RowName'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'numeric'</span>,<span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'bank'</span><span style="color: #080;">&#125;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnWidth'</span>,<span style="color:#A020F0;">'auto'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'Position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,.75,<span style="color: #33f;">1</span>,.25<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Plain data table - so boooooring..." src="http://UndocumentedMatlab.com/images/uitable_plain1.png" title="Plain data table - so boooooring..." width="596" /><p
class="wp-caption-text">Plain data table - so boooooring...</p></div></center></p><p>We can see from this simple example how I have used HTML to format the header labels into two rows, to enable compact columns. I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">described</a> using HTML formatting in Matlab controls in several articles on this website. I have not done this here, but you can easily use HTML formatting for other effect such as superscript (&lt;sup&gt;), subscript (&lt;sub&gt;), bold (&lt;b&gt;), italic (&lt;i&gt;), font sizes and colors (&lt;font&gt;) and other standard HTML effects.</p><p>Even with the multi-line headers, the default column width appears to be too wide. This is apparently an internal Matlab bug, not taking HTML into consideration. This causes only part of the information to be displayed on screen, and requires the user to either scroll right and left, or to manually resize the columns.</p><h3 id="auto-resizing">Second pass &#8211; auto-resizing that actually works&#8230;</h3><p>To solve the auto-resizing issue, we resort to a bit of Java magic powder. We start by using the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a> to get the table&#8217;s underlying Java reference handle. This is the containing scrollpane, and we are interested in the actual data table inside. We then use the <i>setAutoResizeMode</i>, as <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setAutoResizeMode%28int%29">described</a> in the official Java documentation.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jScroll = findjobj<span style="color: #080;">&#40;</span>hTable<span style="color: #080;">&#41;</span>;
jTable = jScroll.<span style="">getViewport</span>.<span style="">getView</span>;
jTable.<span style="">setAutoResizeMode</span><span style="color: #080;">&#40;</span>jTable.<span style="">AUTO_RESIZE_SUBSEQUENT_COLUMNS</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Auto-resized columns that actually work" src="http://UndocumentedMatlab.com/images/uitable_plain2.png" title="Auto-resized columns that actually work" width="596" /><p
class="wp-caption-text">Auto-resized columns that actually work</p></div></center></p><h3 id="colors">Third pass &#8211; adding colors</h3><p>There are still quite a few other customizations needed here: enable <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">sorting</a>; remove the border outline; set a white background; set row (rather than cell) selection and several other fixes that may seem trivial by themselves but together giver a much more stylish look to the table&#8217;s look and feel. I&#8217;ll skip these for now (interested readers can read all about them, and more, in my detailed <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-customization-report/"><i><b>uitable</b></i> customization report</a>).</p><p>One of the more important customizations is to add colors depending on the data. In my client&#8217;s case, there were three requirements:</p><ul><li>data that could be positive or negative should be colored in <b><font
color="green">green</font></b> or <b><font
color="red">red</font></b> foreground color respectively</li><li>large payouts (abs &gt; 2) should be colored in <b><font
color="blue">blue</font></b></li><li>data rows that have statistical significance (%p-value &lt; 5) should have a <span
style="background-color:#ffff00;">yellow background highlight</span></li></ul><p>While we could easily use HTML or CSS formatting to do this, this would be bad for performance in a large data table, may cause some issues when editing table cells or sorting columns. I chose to use the alternative method of <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer">cell renderers</a>.</p><p><code>ColoredFieldCellRenderer</code> is a simple table cell renderer that enables setting cell-specific foreground/background colors and tooltip messages (it also has a few other goodies like smart text alignment etc.). This requires some Java knowledge to program, but in this case you can simply <a
href="http://UndocumentedMatlab.com/files/ColoredFieldCellRenderer.zip">download</a> the ColoredFieldCellRenderer.zip file and use it, even if you don&#8217;t know Java. The source code is included in the zip file, for anyone who is interested.</p><p>After using <i><b>javaaddpath</b></i> to add the zip file to the dynamic Java classpath (you can add it to the static <i>classpath.txt</i> file instead), the contained Java class file is available for use in Matlab. We configure it according to our data and then assign it to all our table&#8217;s columns.</p><p>Java savvy readers might complain that the data-processing should perhaps be done in the renderer class rather than in Matlab. I have kept it in Matlab because this would enable very easy modification of the highlighting algorithm, without any need to modify the generic Java renderer class.</p><p>Unfortunately, in the new <i><b>uitable</b></i> design (the version available since R2008a), JIDE and Matlab have apparently broken the <a
target="_blank" rel="nofollow" href="http://java.sun.com/products/jfc/tsc/articles/architecture">standard MVC approach</a> by using a table model that not only controls the data but also sets the table&#8217;s appearance (row-striping background colors, for example), and disregards column cell-renderers. In order for our custom cell renderer to have any effect, we must therefore replace Matlab&#8217;s standard <code>DefaultUIStyleTableModel</code> with a simple <code>DefaultTableModel</code>. This in itself is not enough – we also need to ensure that the <i><b>uitable</b></i> has an empty <strong>ColumnFormat</strong> property, because if it is non-empty then it overrides our cell-renderer.</p><p>In the following code, note that Java row and column indices start at 0, not at 1 like in Matlab. We need to be careful about indices when programming java in Matlab.</p><p>The rest of the code should be pretty much self explanatory (again &#8211; more details can be found in the above-mentioned report):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Initialize our custom cell renderer class object</span>
javaaddpath<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ColoredFieldCellRenderer.zip'</span><span style="color: #080;">&#41;</span>;
cr = ColoredFieldCellRenderer<span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span>.<span style="">white</span><span style="color: #080;">&#41;</span>;
cr.<span style="">setDisabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% to bg-color the entire column</span>
&nbsp;
<span style="color: #228B22;">% Set specific cell colors (background and/or foreground)</span>
<span style="color: #0000FF;">for</span> rowIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">size</span><span style="color: #080;">&#40;</span>data,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Red/greed foreground color for the numeric data</span>
  <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">2</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
    <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &lt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% red</span>
    <span style="color: #0000FF;">elseif</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &gt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0.5</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% green</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Yellow background for significant rows based on p-value</span>
  <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">12</span><span style="color: #080;">&#41;</span> &lt; = <span style="color: #33f;">5</span> <span style="color: #F0F;">&amp;&amp;</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>~=<span style="color: #33f;">0</span>
    <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
      cr.<span style="">setCellBgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% yellow</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Bold blue foreground for significant payouts</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">abs</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt;= <span style="color: #33f;">2</span>
    cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,<span style="color: #33f;">10</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% blue</span>
&nbsp;
    <span style="color: #228B22;">% Note: the following could easily be done in the renderer, just like the colors</span>
    boldPayoutStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;'</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'%.2f'</span><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">'&lt;/b&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span>;
    <span style="color: #228B22;">%jTable.setValueAt(boldPayoutStr,rowIdx-1,10);  % this is no good: overridden when table model is replaced below...</span>
    dataCells<span style="color: #080;">&#123;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#125;</span> = boldPayoutStr;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Replace Matlab's table model with something more renderer-friendly...</span>
jTable.<span style="">setModel</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">table</span>.<span style="">DefaultTableModel</span><span style="color: #080;">&#40;</span>dataCells,headers<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTable,<span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Finally assign the renderer object to all the table columns</span>
<span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
  jTable.<span style="">getColumnModel</span>.<span style="">getColumn</span><span style="color: #080;">&#40;</span>colIdx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">setCellRenderer</span><span style="color: #080;">&#40;</span>cr<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 600px"><img
alt="Finally something lively!" src="http://UndocumentedMatlab.com/images/uitable_colored.png" title="Finally something lively!" width="590" /><p
class="wp-caption-text">Finally something lively!</p></div></center></p><p>This may not take a beauty contest prize, but you must admit that it now looks more useful than the original table at the top of this article.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-cell-colors/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Tri-state checkbox</title><link>http://undocumentedmatlab.com/blog/tri-state-checkbox/</link> <comments>http://undocumentedmatlab.com/blog/tri-state-checkbox/#comments</comments> <pubDate>Wed, 19 Oct 2011 14:04:20 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2467</guid> <description><![CDATA[Matlab checkboxes can easily be made to support tri-state functionality.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>When presenting information visually in graphical user interfaces (GUIs), we often need to present and enable interaction with state data (such as On/Off). In most cases, we would naturally use a checkbox to present the information and enable interaction. But What can we do if the data has three possible states. For example, Yes/No/Maybe, or: Full/Empty/Partial, or: Up/Down/Undetermined ?</p><p>Until today, Matlab GUIs had to resort to using drop-down (aka combo-box or popup-menu) or radio-button controls to present such information. However, would it not be nicer if we could still use a checkbox? Outside Matlab, such a control is known as a tri-state checkbox and many modern GUI frameworks support it. Well, surprise surprise, so does Matlab (although you would never guess it from the documentation).</p><h3 id="CheckBoxTree">CheckBoxTree</h3><p>Last year, I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/#Built-in-classes">described</a> a built-in Matlab tree control whose nodes have tri-state checkboxes:</p><p><center><div
class="wp-caption aligncenter" style="width: 321px"><img
alt="a regular MJTree (left) and a CheckBoxTree (right)" src="http://UndocumentedMatlab.com/images/CheckBoxTree.png" title="a regular MJTree (left) and a CheckBoxTree (right)" width="311" height="218" /><p
class="wp-caption-text">a regular MJTree (left) and a CheckBoxTree (right)</p></div></center></p><p>Today I will show how we can use these checkboxes as independent GUI controls.</p><h3 id="uicontrol">Modifying the standard Matlab checkbox uicontrol</h3><p>In order to modify the standard Matlab checkbox <i><b>uicontrol</b></i>, we need to first get its underlying Java component reference. This is done using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We then update its UI wrapper to be the same as the <code>CheckBoxTree</code>&#8216;s checkbox control.</p><p>To programmatically set a mixed state we update the &#8216;selectionState&#8217; client property to <code>SelectionState.MIXED</code> (<code>SelectionState</code> also has the <code>SELECTED</code> and <code>NOT_SELECTED</code> values).</p><p>Here is an end-to-end example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hCB = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jCB = findjobj<span style="color: #080;">&#40;</span>hCB<span style="color: #080;">&#41;</span>;
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">TriStateButtonUI</span><span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
newState = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">SelectionState</span>.<span style="">MIXED</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, newState<span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="Matlab checkboxes displaying mixed states" src="http://UndocumentedMatlab.com/images/CheckBox_TriState.png" title="Matlab checkboxes displaying mixed states" width="289" /><p
class="wp-caption-text">Matlab checkboxes displaying mixed states</p></div></center></p><h3 id="independent">Displaying as an independent Java control</h3><p>Instead of retrofitting a standard Matlab <i><b>uicontrol</b></i> as described above, we can directly use the standard <code>javax.swing.JCheckBox</code> which does not normally support tri-state (it only has two states), displaying it in our GUI with the built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/javacomponent/"><i><b>javacomponent</b></i> function</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = javax.<span style="">swing</span>.<span style="">JCheckBox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'JCheckBox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
import com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.*
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>TriStateButtonUI<span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, SelectionState.<span style="">MIXED</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p>Note that instead of using <code>javax.swing.JCheckBox</code>, we could use the internal Matlab class <code>com.mathworks.mwswing.MJCheckBox</code>, which directly extends <code>JCheckBox</code> and adds mnemonic (shortcut-key) support, but is otherwise fully compatible with <code>JCheckBox</code>. Actually, it is <code>MJCheckBox</code> which is the underlying Java component of the standard Matlab checkbox <i><b>uicontrol</b></i>.</p><h3 id="alternatives">Alternative controls</h3><p>Now that we have seen that Matlab includes built-in support (well, at least support in the technical sense, not the official customer-support sense), would you be surprised to learn that it includes similar support in other internal components as well?</p><p>The internal Matlab class <code>com.mathworks.mwt.MWCheckbox</code> directly supports a tri-state (yes/no/maybe) checkbox, without any need to update its UI, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = com.<span style="">mathworks</span>.<span style="">mwt</span>.<span style="">MWCheckbox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MWCheckbox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
jCB.<span style="">setMixedState</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Retrieve the current state</span>
isMixedFlag = jCB.<span style="">isMixedState</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% true/false</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="MJCheckBox vs. MWCheckbox" src="http://UndocumentedMatlab.com/images/CheckBox_MWT_MWSwing.png" title="MJCheckBox vs. MWCheckbox" width="289" /><p
class="wp-caption-text">MJCheckBox vs. MWCheckbox</p></div></center></p><p>Note that the State property, which controls the standard selected/unselected state, is entirely independent from the MixedState property. Both State and MixedState are boolean properties, so to get the actual checkbox state we need to query both of these properties.</p><p>Another internal Matlab class that we can use is JIDE&#8217;s <code>com.jidesoft.swing.TristateCheckBox</code> (which is pre-bundled in Matlab and is fully documented <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/TristateCheckBox.html">here</a>).</p><p>There are many other tri-state checkbox alternatives available online (for example, <a
target="_blank" rel="nofollow" href="https://forums.oracle.com/forums/search.jspa?threadID=&#038;q=%28tri-state+OR+tristate%29+AND+checkbox&#038;objID=c285&#038;dateRange=all&#038;userID=&#038;numResults=30&#038;rankBy=10001">here</a>, <a
target="_blank" rel="nofollow" href="http://www.javaspecialists.eu/archive/Issue145.html">here</a> and <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1263323/tristate-checkboxes-in-java">here</a>). We can easily include them in our Matlab GUI with the <i><b>javacomponent</b></i> function. Using external components we can be more certain of the compatibility issues in past and future Matlab releases. On the other hand, internal Matlab classes do have the advantage of being inherently accessible on all platforms of the same Matlab release, whereas non-Matlab components must be included in our deployment package.</p><p>Do you use any tri-state controls, either Matlab or external, in your work? If so, please share your experience in a <a
href="http://UndocumentedMatlab.com/blog/tri-state-checkbox/#respond">comment below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/tri-state-checkbox/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Uitable customization report</title><link>http://undocumentedmatlab.com/blog/uitable-customization-report/</link> <comments>http://undocumentedmatlab.com/blog/uitable-customization-report/#comments</comments> <pubDate>Wed, 03 Aug 2011 18:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitable report]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2396</guid> <description><![CDATA[In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and [...]<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In last week&#8217;s report about <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/"><i><b>uitable</b></i> sorting</a>, I offered a report that I have written which covers <i><b>uitable</b></i> customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and so in today&#8217;s post I will discuss in a bit more detail the highlights of what can be achieved to customize Matlab <i><b>uitable</b></i>s. For the fine details, well, <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=octahedron.ltd@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">get the report</a>&#8230;</p><h3 id="Introduction">1. Introduction</h3><p>Here I discuss the evolution of the <i><b>uitable</b></i> control over the past decade, from its initially semi-documented status to today. I explain the similarities and differences between the control&#8217;s versions and explain how they can both be accessed today.</p><p>I also provide references to online resources for both Matlab&#8217;s <i><b>uitable</b></i>&#8216;s underlying Java components, as well as multiple alternatives using different technologies, that have been used and reported over the years.</p><h3 id="Customization">2. Customizing uitable</h3><p>In this section I explore the sub-component hierarchy of the <i><b>uitable</b></i> control (both new and old). I show how the scrollbar sub-components can be accessed (this will be used in section 4 below), as well as the table header and data grid.<br
/><center><div
class="wp-caption aligncenter" style="width: 322px"><img
alt="annotated uitable sub-components" src="http://undocumentedmatlab.com/images/uitable_new2b.png" title="annotated uitable sub-components" width="312" /><p
class="wp-caption-text">annotated <i><b>uitable</b></i> sub-components</p></div></center></p><p>I explain how individual cells can be modified without requiring the entire data set to be updated. This is very important in large data sets, to prevent flicker and improve performance.</p><p>I show how HTML can be used to format data cell contents (even images) and tooltips:<br
/><center><div
class="wp-caption aligncenter" style="width: 259px"><img
alt="uitable with HTML cell contents and tooltip" src="http://undocumentedmatlab.com/images/uitable2.png" title="uitable with HTML cell contents and tooltip" width="249" /><p
class="wp-caption-text"><i><b>uitable</b></i> with HTML cell contents and tooltip</p></div></center></p><p>I then explain the role of the cell-renderer in the visual appearance of the cell, and of cell-editors in the way that cells interact with the user for data modification. I explain such customizations from the simple (setting a column&#8217;s background color) to the complex (cell-specific tooltips and colors; color-selection cell-editor):<br
/><center><div
class="wp-caption aligncenter" style="width: 590px"><img
alt="uitable with a non-standard cell-renderer" src="http://undocumentedmatlab.com/images/uitable5b.png" title="uitable with a non-standard cell-renderer" width="288" /><img
alt="uitable with a non-standard cell-editor" src="http://undocumentedmatlab.com/images/uitable_lookup.png" title="uitable with a non-standard cell-editor" width="280" /><p
class="wp-caption-text"><i><b>uitable</b></i> with a non-standard cell-renderer (left) and cell-editor (right)</p></div><br
/><div
class="wp-caption aligncenter" style="width: 318px"><img
alt="uitable with custom CellRenderer and CellEditor" src="http://UndocumentedMatlab.com/images/ColorCellEditor_Renderer.png" title="uitable with custom CellRenderer and CellEditor" width="308" /><p
class="wp-caption-text"><i><b>uitable</b></i> with custom CellRenderer and CellEditor</p></div></center></p><h3 id="Callbacks">3. Table callbacks</h3><p>This section presents the different callback properties that are settable in the old and new <i><b>uitable</b></i>, for events such as cell selection, data modification, key press, and mouse click. The discussion includes a working code example for validating user input and reverting invalid edits. The section also includes a discussion of how to avoid and overcome problems that may occur with the callback execution.</p><h3 id="Scrollbars">4. Customizing scrollbars, column widths and selection behavior</h3><p>This section explains how to control the scrollbars behavior. For example, hiding the horizontal (bottom) scrollbar, automatically displaying it when the data width is larger than the table width. I also show how to control the column widths.</p><p>I then show how to customize the data selection policy: Can multiple cells be selected? perhaps only one cell at a time? or maybe a single large interval of cells? &#8211; this is all customizable. I then explain how the selection can be done and accessed programmatically. Ever wanted to set the cursor on cell A1 after some system event has occurred? &#8211; this will show you how to do it. Ever wanted to use non-standard selection colors (background/foreground)? &#8211; this can also be done.</p><h3 id="Sorting">5. Data sorting</h3><p>Table sorting was discussed in last week&#8217;s <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">article</a>. This section expands on that article, and explains how the sorting can be customized, controlled, and accessed programmatically, and how sorted rows can be retrieved by the user.<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Multi-column sorting with blue sort-order numbers" src="http://UndocumentedMatlab.com/images/JIDE_Table_sort3a.png" title="Multi-column sorting with blue sort-order numbers" width="260" /><p
class="wp-caption-text">Multi-column sorting with blue sort-order numbers</p></div></center></p><h3 id="Filtering">6. Data filtering</h3><p>Data filtering is the ability to filter only a specified sub-set of rows for display (just like in Excel). This section explains how to do it (it&#8217;s so easy!).<br
/><center><div
class="wp-caption aligncenter" style="width: 418px"><img
alt="uitable data filtering" src="http://UndocumentedMatlab.com/images/004x013b.png" title="uitable data filtering" width="408" /><p
class="wp-caption-text"><i><b>uitable</b></i> data filtering</p></div></center></p><h3 id="JIDE">7. JIDE customizations</h3><p>The new <i><b>uitable</b></i> is based on an underlying JIDE table. This sectino explains how we can use this to our advantage for some simple and useful. Customization.</p><p>For example: have you wondered some time why is it that columns can only be resized by dragging the tiny divider in the table header? Why can&#8217;t the columns and rows be resized by dragging the grid lines? Well, it turns out that they can, with just a tiny bit of JIDE magic powder, explained here:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Resizing columns" src="http://UndocumentedMatlab.com/images/004x014.png" title="Resizing columns" width="260" /><p
class="wp-caption-text">Resizing columns</p></div></center></p><p>Similarly, this section explains how we can use JIDE to merge together adjacent cells:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Example of two table cell-spans (1x2 and 2x2)" src="http://UndocumentedMatlab.com/images/004x016.png" title="Example of two table cell-spans (1x2 and 2x2)" width="260" /><p
class="wp-caption-text">Example of two table cell-spans (1x2 and 2x2)</p></div></center></p><h3 id="Structure">8. Controlling the table structure (adding/removing rows)</h3><p>This section discusses the matter of dynamically adding and removing table rows. While this is easy to do in the old <i><b>uitable</b></i>, this is unfortunately not the case in the new <i><b>uitable</b></i>.</p><h3 id="Final">9. Final remarks</h3><p>Here I present a workaround for a long-time table bug. Also, I present my <i><b>createTable</b></i> utility that wraps table creation in Matlab:<br
/><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" src="http://UndocumentedMatlab.com/images/table.png" title="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" width="597" /><p
class="wp-caption-text"><i><b>createTable</b></i> utility screenshot (note the action buttons, sortable columns, and customized CellEditor)</p></div></center></p><h3 id="JIDE-Grids">Appendix – JIDE Grids</h3><p>Finally, this appendix presents an overview of the wide array of components provided by JIDE and available in Matlab. <i><b>uitable</b></i> uses only one of these components (the <code>SortableTable</code>). In fact, there are many more such controls that we can use in our GUIs.</p><p>These include a wide selection of combo-box (drop-down) controls &#8211; calculator, file/folder selection, date selection, color selection, multi-elements selection etc. In addition, a very wide selection of lists, trees and table types is available.</p><p>Also included is a set of specialized editbox controls for accepting IP addresses and credit card numbers:<br
/><center><div
class="wp-caption aligncenter" style="width: 360px"><img
alt="IP address entry box" src="http://UndocumentedMatlab.com/images/JIDE_IPTextField.png" title="IP address entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_invalid.png" title="credit-card entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_Visa.png" title="credit-card entry box" width="100" /><p
class="wp-caption-text">IP address and credit-card entry boxes</p></div></center></p><p>While not explaining all these controls in detail (this could take hundreds of pages), this section does say a few words on each of them, and includes links to online resources for further exploration.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-customization-report/feed/</wfw:commentRss> <slash:comments>11</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 19:51:44 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/gui/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      1.968s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337655102; expires=Wed, 22-May-2013 02:51:42 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 22 May 2012 02:51:44 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 22 May 2012 03:51:44 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               f2f4d8ecc9f90b466c31a84b2969c124
Content-Encoding:   gzip
-->
