<?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/"
	>

<channel>
	<title>Undocumented Matlab</title>
	<atom:link href="http://undocumentedmatlab.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://undocumentedmatlab.com</link>
	<description>Charting Matlab's unsupported hidden underbelly</description>
	<pubDate>Wed, 10 Mar 2010 07:21:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Matlab and the Event Dispatch Thread (EDT)</title>
		<link>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/</link>
		<comments>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 07:18:58 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Handle graphics]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[Semi-documented function]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[Undocumented feature]]></category>

		<category><![CDATA[callbacks]]></category>

		<category><![CDATA[Matt Whitaker]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1198</guid>
		<description><![CDATA[The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; 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/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='Permanent Link: setPrompt - Setting the Matlab Desktop prompt'>setPrompt - Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p><i>Once again I welcome guest blogger Matt Whitaker, with the long awaited EDT article.</i></p>
<h3 id="EDT">Java Swing&#8217;s Event Dispatch Thread (EDT)<br /><i>or: why does my GUI foul up?</i></h3>
<p>Matlab for the most part is a single threaded environment. That is, all commands are executed sequentially along a single execution thread. The main exception to this are the Handle Graphics (GUI) components whose operations execute on the <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html">Java Event Dispatch Thread (EDT)</a>. EDT effects are reflected even in mundane Matlab GUI operations.</p>
<p>If we execute the code below we will probably see nothing until the loop completes and the figure appears with the text label showing &#8216;10000&#8242;:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">h = <span style="color: #0000FF;">figure</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">for</span> n = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">10000</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>txt,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">int2str</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%for</span></pre></div></div>

<p>By adding a couple of <b><i>drawnow</i></b> commands we get the figure and text label to render and then we see the count progress to 10000.</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">h = <span style="color: #0000FF;">figure</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
<span style="color: #0000FF;">for</span> n = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">10000</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>txt,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">int2str</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%for</span></pre></div></div>

<p>The <b><i>drawnow</i></b> function allows the EDT queue to be flushed and the pending graphics operations to be evaluated. This will also happen with <b><i>pause</i></b> and several other commands.</p>
<p>If we want to use Swing (or AWT) components in our user interfaces we need to take this multi-threaded environment into account. The Swing toolkit designers decided to make all the Swing components thread <b><u>un</u></b>-safe in order to decrease their complexity. As a consequence, all access to Swing components should be done from the event dispatch thread (EDT), to ensure the operations are executed sequentially, at the exact order in which they were dispatched. Any action on a Swing component done on another thread (Matlab&#8217;s main processing thread in our case) risks a race-condition or deadlock with the EDT, which could (and often does) result in weird, non-deterministic and non-repetitive behavior – all of which should be avoided in any application which should behave in a precisely deterministic manner.</p>
<p>In Java, the usual pattern to accomplish EDT dispatching is to create a <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/lang/Runnable.html">Runnable</a> object, encapsulate the GUI code in the <i>run</i> method of the Runnable object, then pass the Runnable object to the static <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/awt/EventQueue.html#invokeLater(java.lang.Runnable)">EventQueue.invokeLater</a> (or <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/awt/EventQueue.html#invokeAndWait(java.lang.Runnable)">EventQueue.invokeAndWait</a> if we need to block operations to get a return value) method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Runnable</span> runnable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//GUI Code here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003399;">EventQueue</span>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span>runnable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are several functions in Matlab that implement this programming pattern for us: <b><i>javaObjectEDT</i></b>, <b><i>javaMethodEDT</i></b>, <b><i>awtinvoke</i></b>, <b><i>awtcreate</i></b> and <b><i>javacomponent</i></b>. <b><i>JavaMethodEDT</i></b> and <b><i>javaObjectEDT</i></b> were introduced in version R2008b (7.7) and are <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/javaobjectedt.html">minimally and only partially documented</a> although they have reasonably complete help comments. The other three are semi-documented (meaning they are unsupported but if you edit or type their m-file you&#8217;ll see a fairly detailed help section), and although there is some overlap in their functionality they are still available.</p>
<h3 id="javaObjectEDT">javaObjectEDT and javaMethodEDT</h3>
<p><b><i>javaObjectEDT</i></b> is the the preferred method since R2008b of creating swing components to be used on the EDT. An object created with <b><i>javaObjectEDT</i></b> will have all of its subsequent method calls run on the EDT. This is termed <i>Auto Delegation</i>. Auto-delegation greatly simplifies and increases the readability of code. Note that objects created as a result of method calls may not be implemented on the EDT.</p>
<p>If you have an existing Java object, you can pass it to <b><i>javaObjectEDT</i></b> at any time - all its subsequent calls will then onward run on the EDT. Note that this useful functionality is an under-documented <b><i>javaObjectEDT</i></b> feature: it is not mentioned in the main help section but only implied from the example.</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a button on the EDT</span>
btn = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% this will run on EDT since btn was javaObjectEDT-created</span>
btn.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Create a button NOT on the EDT</span>
btn2 = javax.<span style="">swing</span>.<span style="">JButton</span>;
<span style="color: #228B22;">% Dangerous! call will run on main Matlab thread</span>
btn2.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button2'</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% modify btn2 so its methods will start running on the EDT</span>
javaObjectEDT<span style="color: #080;">&#40;</span>btn2<span style="color: #080;">&#41;</span>;
btn2.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Button2'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p>The following example shows the use of <b><i>javaObjectEDT</i></b> and <b><i>javaMethodEDT</i></b> in a more complex situation using a JTable:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> tableExample
hFig = <span style="color: #0000FF;">figure</span>;
<span style="color: #0000FF;">drawnow</span>; <span style="color: #228B22;">%need to get figure rendered</span>
&nbsp;
<span style="color: #228B22;">%use Yair's createTable to add a javax.swing.JTable</span>
<span style="color: #228B22;">%http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table</span>
<span style="color: #228B22;">%wrap ceateTable in javaObjectEDT to put the ensuing method calls on the EDT</span>
f = java.<span style="">awt</span>.<span style="">Font</span><span style="color: #080;">&#40;</span>java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Dialog'</span><span style="color: #080;">&#41;</span>,java.<span style="">awt</span>.<span style="">Font</span>.<span style="">PLAIN</span>,<span style="color: #33f;">14</span><span style="color: #080;">&#41;</span>;
headers = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Selected'</span>,<span style="color:#A020F0;">'File'</span>,<span style="color:#A020F0;">'Analysis Routine'</span>,<span style="color:#A020F0;">'Task Status'</span><span style="color: #080;">&#125;</span>;
tbl = javaObjectEDT<span style="color: #080;">&#40;</span>createTable<span style="color: #080;">&#40;</span>hFig,headers,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF;">false</span>,<span style="color:#A020F0;">'Font'</span>,f<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set column 1 to use check boxes and set up a change callback</span>
tbl.<span style="">setCheckBoxEditor</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
jtable = javaObjectEDT<span style="color: #080;">&#40;</span>tbl.<span style="">getTable</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the underlying Java Table. IMPORTANT: we need to put jtable on the EDT</span>
columnModel = javaObjectEDT<span style="color: #080;">&#40;</span>jtable.<span style="">getColumnModel</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%now we can now do direct calls safely on jtable</span>
selectColumn = javaObjectEDT<span style="color: #080;">&#40;</span>columnModel.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
selectColumnCellEditor = selectColumn.<span style="">getCellEditor</span>;
chk = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getComponent'</span>,selectColumnCellEditor<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>chk,<span style="color:#A020F0;">'ItemStateChangedCallback'</span>,@chkChange_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%make column three a combo drop down</span>
analysisTable = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Analysis1'</span>;<span style="color:#A020F0;">'Analysis2'</span>;<span style="color:#A020F0;">'Analysis3'</span><span style="color: #080;">&#125;</span>;
cb = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'com.mathworks.mwswing.MJComboBox'</span>,analysisTable<span style="color: #080;">&#41;</span>;
cb.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
cb.<span style="">setFont</span><span style="color: #080;">&#40;</span>f<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>cb,<span style="color:#A020F0;">'ItemStateChangedCallback'</span>,@cbChange_Callback<span style="color: #080;">&#41;</span>;
editor = javaObjectEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.DefaultCellEditor'</span>,cb<span style="color: #080;">&#41;</span>;
analysisColumn = javaObjectEDT<span style="color: #080;">&#40;</span>columnModel.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
analysisColumn.<span style="">setCellEditor</span><span style="color: #080;">&#40;</span>editor<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set some column with restrictions</span>
selectColumn.<span style="">setMaxWidth</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>;
analysisColumn.<span style="">setPreferredWidth</span><span style="color: #080;">&#40;</span><span style="color: #33f;">300</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">%set the data</span>
SELECTED = java.<span style="">awt</span>.<span style="">event</span>.<span style="">ItemEvent</span>.<span style="">SELECTED</span>;
tbl.<span style="">setData</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color: #0000FF;">false</span>,<span style="color:#A020F0;">'file1'</span>,<span style="color:#A020F0;">'Analysis2'</span>,<span style="color:#A020F0;">'Analysis2'</span>;<span style="color: #F0F;">...</span>
             <span style="color: #0000FF;">true</span>,<span style="color:#A020F0;">'file2'</span>,<span style="color:#A020F0;">'Analysis3'</span>,<span style="color:#A020F0;">'Analysis3'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
&nbsp;
    <span style="color: #0000FF;">function</span> cbChange_Callback<span style="color: #080;">&#40;</span>src,ev<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        jRow = jtable.<span style="">getSelectedRow</span>;
        stateChange = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getStateChange'</span>,ev<span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">if</span> stateChange == SELECTED
            newData = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getItem'</span>,ev<span style="color: #080;">&#41;</span>;
            model = jtable.<span style="">getModel</span>;
            javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setValueAt'</span>,model,newData,jRow,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%cbChange</span>
&nbsp;
    <span style="color: #0000FF;">function</span> chkChange_Callback<span style="color: #080;">&#40;</span>src,ev<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        chkBox = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getItem'</span>,ev<span style="color: #080;">&#41;</span>;
        <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">logical</span><span style="color: #080;">&#40;</span>javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'isSelected'</span>,chkBox<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
            beep; <span style="color: #228B22;">%put useful code here</span>
        <span style="color: #0000FF;">else</span>
            beep;
            <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.1</span><span style="color: #080;">&#41;</span>
            beep; <span style="color: #228B22;">%put useful code here</span>
        <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkChange_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%tableExample</span></pre></div></div>

<p>If you are running Matlab R2008a or later, <b><i>javacomponent</i></b> uses the <b><i>javaObjectEDT</i></b> function to create the returned objects so you do not have to do anything further to these objects to have their calls dispatched on the EDT. Users need to take care that objects added directly to the components created by <b><i>javacomponent</i></b> are on the EDT as well as specialized sub-components (e.g. CellRenderers and CellEditors). The overhead of calling <b><i>javaMethodEDT</i></b> is fairly small so if in doubt, use it.</p>
<p><b><i>javaObjectEDT</i></b> and its kin first appeared in R2008a, although they only became supported in R2008b. Unfortunately, using them on R2008a sometimes causes hangs and all sorts of other mis-behaviors. This problem was fixed in the R2008b release, when <b><i>javaObjectEDT</i></b> became a fully-supported function. The problem with using <b><i>javaObjectEDT</i></b> in our application is that if it ever runs on an R2008a platform it might hang! (on Matlab release R2007b and earlier we will get an informative message saying that the <b><i>javaObjectEDT</i></b> function does not exist)</p>
<p>For this reason, I am using the following method in my projects:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> result = javaObjEDT<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%Placeholder of Matlab's buggy javaObjectEDT function on R2008a</span>
&nbsp;
<span style="color: #228B22;">% Programmed by Yair M. Altman: altmany(at)gmail.com</span>
<span style="color: #228B22;">% $Revision: 1.2 $  $Date: 2009/01/25 11:31:08 $</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
      <span style="color: #0000FF;">try</span>
          result = <span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
      <span style="color: #0000FF;">catch</span>
          result = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
      <span style="color: #0000FF;">end</span>
      v = <span style="color: #0000FF;">version</span>;
      <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">str2double</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt; <span style="color: #33f;">7.6</span>
          result = <span style="color: #0000FF;">builtin</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javaObjectEDT'</span>,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
      <span style="color: #228B22;">% never mind</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div>

<p>Note that <b><i>javaMethodEDT</i></b> has the method name as its first input argument, and the object name or reference as its second arg. This is inconsistent with many other Matlab/Java functions, which normally accept the target object as the first argument (compare: <b><i>invoke</i></b>, <b><i>awtinvoke</i></b>, <b><i>notify</i></b> etc.). It also means that we cannot use the familiar <i>obj.javaMethodEDT(methodName)</i> format.</p>
<p>One final note: when <b><i>javaObjectEDT</i></b> and <b><i>javaMethodEDT</i></b> first appeared in R2008a, they were complemented by the <b><i>javaObjectMT</i></b> and <b><i>javaMethodMT</i></b> functions, which create and delegate Java objects on the main Matlab computational thread. Their internal documentation says that there are cases when execution must occur on the MT rather than EDT, although I am personally not aware of any such case.</p>
<h3 id="awtcreate">awtcreate and awtinvoke</h3>
<p>For users with versions prior to R2008b the user must use the <b><i>awtcreate</i></b> function to create objects on the EDT. One huge disadvantage of this older function is that if you have to pass java objects in the parameter list you must use the very cumbersome <a target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html#wp276">JNI style notation</a>. For example, for the simple task of setting a button label, one has to use:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">btn = awtcreate<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JButton'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>btn,<span style="color:#A020F0;">'setText(Ljava/lang/String;)'</span>,<span style="color:#A020F0;">'click me'</span><span style="color: #080;">&#41;</span></pre></div></div>

<p>The other disadvantage is that creating the object using <b><i>awtcreate</i></b> does not ensure that its subsequent method calls will be executed on the EDT. The <b><i>awtinvoke</i></b> function must be used for each call.</p>
<p>Also, both <b><i>awtcreate</i></b> and <b><i>awtinvoke</i></b> have some limitations due to bugs in the private <b><i>parseJavaSignature</i></b> function (for example, invoking methods which accept a java.lang.Object) which forces one to use the direct call to the method, using the main Matlab thread. This can result in the undesired effects described above. In this situation the best workaround is to call <i><b>pause</b>(0.01)</i> to allow the event queue to clear.</p>
<p>Versions of <b><i>javacomponent</i></b> earlier than R2008a use <b><i>awtcreate</i></b> and objects created by these versions must have their subsequent methods called by <b><i>awtinvoke</i></b> to be used on the EDT.</p>
<p>A very rare <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388">CSSM thread</a> discusses the usage of <b><i>awtcreate</i></b> and <b><i>awtinvoke</i></b> with some very interesting remarks by MathWorks personnel. </p>
<p>There is an interesting option in <b><i>awtinvoke</i></b> that was not carried over into the newer <b><i>javaMethodEDT</i></b>. This option allows the user to pass a function handle in the argument list along with its parameters. This option creates an undocumented com.mathworks.jmi.Callback object that has a delayed callback. The delayed callback is dispatched on the EDT so that it will be called once the java method used in <b><i>awtinvoke</i></b> is finished. Note that the actual function will still execute on the main Matlab thread the delayed callback will just control when it is called. However this may be useful at times. It is possible to put this functionality into a separate function we can call to delay execution until the event queue is cleared.</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">%CALLBACKONEDTQUEUE will place a callback on the EDT to asynchronously</span>
<span style="color: #228B22;">%run a function.</span>
<span style="color: #228B22;">%CALLBBACKONEDTQUEUE(FCN) will run function handle FCN once all previous</span>
<span style="color: #228B22;">%methods dispatched to the EDT have completed.</span>
<span style="color: #228B22;">%CALLBBACKONEDTQUEUE(FCN,ARG1,ARG2,...) ill run function handle FCN with</span>
<span style="color: #228B22;">%arguments ARG1,ARG2...once all previous methods dispatched to the EDT</span>
<span style="color: #228B22;">%have completed.</span>
<span style="color: #228B22;">%Note that the function is still executing on the main Matlab thread. This</span>
<span style="color: #228B22;">%function just delays when it will be called.</span>
<span style="color: #0000FF;">function</span> callbackOnEDTQueue<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    validateattributes<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'function_handle'</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    callbackObj = handle<span style="color: #080;">&#40;</span>com.<span style="">mathworks</span>.<span style="">jmi</span>.<span style="">Callback</span>,<span style="color:#A020F0;">'callbackProperties'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>callbackObj,<span style="color:#A020F0;">'delayedCallback'</span>,<span style="color: #080;">&#123;</span>@cbEval,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    callbackObj.<span style="">postCallback</span>;
&nbsp;
    <span style="color: #0000FF;">function</span> cbEval<span style="color: #080;">&#40;</span>src,evt,args<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
        <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%cbEval</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%callbackOnEDTQueue</span></pre></div></div>



 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; 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/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='Permanent Link: setPrompt - Setting the Matlab Desktop prompt'>setPrompt - Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Setting desktop tab completions</title>
		<link>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/</link>
		<comments>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 16:03:09 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[Desktop]]></category>

		<category><![CDATA[Low risk of breaking in future versions]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[Undocumented feature]]></category>

		<category><![CDATA[pure Matlab]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1175</guid>
		<description><![CDATA[The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='Permanent Link: setPrompt - Setting the Matlab Desktop prompt'>setPrompt - Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Permanent Link: Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Permanent Link: Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Permanent Link: Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p>This site has lately focused on quite detailed Java-related topics. Next week I will present the promised EDT article, which will dive into even deeper Java territory. So I thought to take a short break and present an entirely pure-Matlab non-Java undocumented feature, which is simple and yet quite useful.</p>
<p>A few months ago, a CSSM reader <a target=_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/264550">asked</a> whether it is possible to customize <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2007/04/27/tab-completion-will-save-your-fingers/">Matlab tab-completion</a> for user-defined functions (see <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2009/08/10/tab-to-narrow-completions/">related</a>). A similar question on StackOverflow provided the necessary <a target=_blank" rel="nofollow" href="http://stackoverflow.com/questions/1842804/tab-completion-of-filenames-as-arguments-for-matlab-scripts">solution lead</a>:</p>
<p>Apparently, Matlab has a file called TC.xml in its [matlabroot '/toolbox/local/'] folder that contains the definitions of the tab-completable functions and their arguments. In order for a user-defined function&#8217;s arguments to support tab-completion, a new entry needs to be added to this XML file.</p>
<h3 id="TC.xml">TC.xml &amp; TC.xsd</h3>
<p>The full syntax of the TC.xml file can be found in the TC.xsd file, which is located in the same folder as TC.xml. Here are some sample definitions from my TC.xml file (which might vary across Matlab releases):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addpath&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;DIR&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;help&quot;</span>    <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN SUBFUN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clear&quot;</span>   <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN VAR&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;whos&quot;</span>    <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">previous</span>=<span style="color: #ff0000;">&quot;-file&quot;</span> <span style="color: #000066;">ctype</span>= <span style="color: #ff0000;">&quot;MATFILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;open&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR MATFILE FIGFILE MFILE MDLFILE FILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;openfig&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FIGFILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;new visible invisible reuse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mlint&quot;</span>   <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;2:10&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;-struct -string -id&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first example defines that an unlimited number of <i><b>addpath</b</i> arguments are all of type DIR. Therefore, when completing any argument of this function in the Command-Window, Matlab will present only relevant DIR (=folder) elements in the pop-up window (lexically sorted):<br />
<center><div class="wp-caption aligncenter" style="width: 460px"><img alt="Tab-completion of type DIR" src="http://UndocumentedMatlab.com/images/tabcomplete1.png" title="Tab-completion of type DIR" width="450" height="156" /><p class="wp-caption-text">Tab-completion of type DIR</p></div></center></p>
<p>Similarly, <b><i>help</i></b> defines all its arguments to be a function or sub-function type, so the popup-up will only be populated with the function names currently visible in the desktop:<br />
<center><div class="wp-caption aligncenter" style="width: 460px"><img alt="Tab-completion of types FUN &#038; SUBFUN" src="http://UndocumentedMatlab.com/images/tabcomplete2.png" title="Tab-completion of types FUN &#038; SUBFUN" width="450" height="174" /><p class="wp-caption-text">Tab-completion of types FUN &#038; SUBFUN</p></div></center></p>
<p>Similarly, <b><i>clear</i></b> defines all its arguments as function names or variables. Note that the list of available functions and variables may change depending on the current execution stack position. The full list of supported types is defined in the TC.xsd file. It is: VAR, FUN, SUBFUN, DIR, FILE, MFILE, MATFILE, FIGFILE and MDLFILE.</p>
<p>The <b><i>whos</i></b> function defines all its arguments as VAR, except the single MATFILE argument that follows a &#8216;-file&#8217; argument (look at <b><i>whos</i></b>&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/whos.html">help page</a> to understand why).</p>
<p>The <b><i>open</i></b> function defines tab completion only for its first argument (with plenty of possible types&#8230;). Likewise, <b><i>openfig</i></b> defines its first argument as a FIGFILE, and its second as VAR with a few extra <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/openfig.html">special-purpose strings</a> that are added to the popup-up menu.</p>
<p>Finally, the <b><i>mlint</i></b> example shows that multiple arguments can be defined using a single XML definition element. In this case, args #2-10 are defined as VAR (with three extra special-purpose strings), while arg #1 and 11+ are defined as FUN.</p>
<p>The careful user can edit the TC.xml file using any text editor (I strongly suggest saving a backup first):</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">edit</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'toolbox/local/TC.xml'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p>User-defined functions can easily be added to TC.xml, and we can even add/modify the built-in Matlab functions that are already defined. Note that changes to TC.xml only take effect after a Matlab restart. From then on, all future Matlab sessions will use the modification, so a really simple one-time edit can improve our workflow for a long time - at least until we upgrade Matlab, when we&#8217;ll need to redo our edits&#8230;</p>
<h3 id="TabComplete">TabComplete utility</h3>
<p>In order to facilitate TC.xml editing, I have created a utility called TabComplete, which is now <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26830-tabcomplete">available on the Matlab File Exchange</a>. The use of this utility is very simple. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">tabcomplete test file <span style="color:#A020F0;">'DIR +data -data nodata'</span> <span style="color: #0000FF;">VAR</span></pre></div></div>

<p>defines a user-defined function <b><i>test</i></b> that accepts a FILE argument, followed by a DIR argument with three special-purpose strings, followed by any number of VAR arguments. If I wished to define specific argument types without any default type, I would use:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">tabcomplete test file <span style="color:#A020F0;">'DIR +data -data nodata'</span> <span style="color:#A020F0;">''</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 418px"><img alt="Using TabComplete for user-defined functions" src="http://UndocumentedMatlab.com/images/tabcomplete.png" title="Using TabComplete for user-defined functions" width="408" height="147" /><p class="wp-caption-text">Using TabComplete for user-defined functions</p></div></center></p>
<p>TabComplete can also be used to retrieve the current list of tab-completion definitions:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; definitions = tabcomplete;
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    functionName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'addpath'</span>
     defaultType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'DIR'</span>
     extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        platform<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
    functionArgs<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    functionName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'openfig'</span>
     defaultType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
     extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        platform<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
    functionArgs<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x2 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>.<span style="">functionArgs</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    previousArg<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        argType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'FIGFILE'</span>
    extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>.<span style="">functionArgs</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    previousArg<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        argType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'VAR'</span>
    extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'new visible invisible reuse'</span></pre></div></div>

<p>TabComplete has a few limitations: it does not support the -previous option described above (you can do this by manually editing TC.xml). There are also some inherent limitations in Matlab&#8217;s TC functionality: changes take effect only after a Matlab restart (there might be a way to reload the definitions in the current Matlab session, but I do not know of any); the list of standard types cannot be modified; and the default type does not support extra special-purpose strings as do the numbered arguments.</p>
<p>There is another very annoying limitation: by default, TC.xml only supports lowercase function names. This is stupid, since Matlab has many function names with UPPERCASE characters, and certainly user-defined function names also do. Luckily, this last limitation can easily be overcome by editing the TC.xsd file (note that this is the TC.XSD file, not the TC.XML file). Instead of:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tcBindingNameType&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;xsd:token&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:pattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">'[A-Za-z_0-9]+(/[a-z_0-9]+)?'</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:restriction<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:simpleType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Change the <b>xsd:pattern</b> definition element to:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">&lt;!-- Yair 21/2/2010: added A-Z --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:pattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">'[A-Za-z_0-9]+(/[A-Za-z_0-9]+)?'</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>(note the way comments can be added to the XSD/XML files)</p>
<p>P.S. an entirely different customization, for user-defined class members, was <a target="_blank' rel="nofollow" href="http://www.tim.cz/en/nfaq/matlab-simulink/tab-completion.php">presented by Michal Kutil</a>.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='Permanent Link: setPrompt - Setting the Matlab Desktop prompt'>setPrompt - Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Permanent Link: Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Permanent Link: Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Permanent Link: Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Inactive Control Tooltips &amp; Event Chaining</title>
		<link>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/</link>
		<comments>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 22:34:46 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Handle graphics]]></category>

		<category><![CDATA[Hidden property]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Listeners]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[Stock Matlab function]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[Undocumented function]]></category>

		<category><![CDATA[callbacks]]></category>

		<category><![CDATA[listener]]></category>

		<category><![CDATA[Matt Whitaker]]></category>

		<category><![CDATA[uicontrol]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1153</guid>
		<description><![CDATA[Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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/continuous-slider-callback/' rel='bookmark' title='Permanent Link: Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: 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/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Permanent Link: Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p><i>Once again, I welcome guest blogger Matt Whitaker, who continues his series of articles.</i></p>
<p>In my last post I explored some <a target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">tips on tooltips</a>. One of these tips involved displaying tooltips on disabled uicontrols. I explained that displaying tooltips on inactive controls is problematic since Matlab appears to intercept mouse events to these inactive controls, so even setting the tooltip on the underlying Java object will not work: The java object appears not to receive the mouse-hover event and therefore does not &#8220;know&#8221; that it&#8217;s time to display the tooltip.</p>
<p>When Yair and I deliberated this issue, he pointed me to his <a target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/#comment-1173">comment on a previous article</a> showing an undocumented Java technique (Java also has some&#8230;) for forcing a tooltip to appear using the <b>ActionMap</b> of the uicontrol&#8217;s underlying Java object to get at a <i>postTip</i> action. We discussed using a <b>WindowButtonMotionFcn</b> callback to see if the mouse was above the inactive control, then triggering the forced tooltip display. Yair then went on to remind me and I quote: <i>&#8220;you&#8217;ll need to chain existing <b>WindowButtonMotionFcn</b> callbacks and take into account ModeManagers that override them.&#8221;</i></p>
<p>Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!</p>
<p>The Image Processing Toolbox has the nice pair of <b><i>iptaddcallback</i></b> and <b><i>iptremovecallback</i></b> functions that largely handle these issues. But  for general Matlab, there seemed to be no alternative until I remembered that events trigger callbacks. I decided to use a listener for the <i>WindowButtonMotion</i> event to detect the mouse motion. <a target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Event_Listener">Event listeners were briefly explained two weeks ago</a> and deserve a dedicated future article. The advantage of using an event listener is that we don&#8217;t disturb any existing <b>WindowButtonMotionFcn</b> callback. We still need to be somewhat careful that our listeners don&#8217;t do conflicting things, but it&#8217;s a lot easier than trying to manage everything through the single <b>WindowButtonMotionFcn</b>.</p>
<p>A demonstration of this appears below with some comments following (note that this code uses the <a target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> inactiveBtnToolTip
  <span style="color: #228B22;">%Illustrates how to make a tooltip appear on an inactive control</span>
  h = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'WindowButtonMotionFcn'</span>,@windowMotion,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">400</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  col = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'color'</span><span style="color: #080;">&#41;</span>;
  lbl = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">160</span>,<span style="color: #33f;">120</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Background'</span>,col, <span style="color:#A020F0;">'HorizontalAlignment'</span>,<span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
  btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'inactive'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'check'</span>, <span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Enable button tooltip'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Callback'</span>,@chkTooltipEnable, <span style="color:#A020F0;">'Value'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">180</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Background'</span>,col<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">drawnow</span>;
&nbsp;
  <span style="color: #228B22;">%create the tooltip and postTip action</span>
  jBtn = findjobj<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>;
  import java.<span style="">awt</span>.<span style="">event</span>.<span style="">ActionEvent</span>;
  javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jBtn,<span style="color:#A020F0;">'This button is inactive'</span><span style="color: #080;">&#41;</span>;
  actionMap = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getActionMap'</span>,jBtn<span style="color: #080;">&#41;</span>;
  action = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'get'</span>,actionMap,<span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
  actionEvent = ActionEvent<span style="color: #080;">&#40;</span>jBtn, ActionEvent.<span style="">ACTION_PERFORMED</span>, <span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%get the extents plus 2 pixels of the control to compare to the mouse position</span>
  btnPos = getpixelposition<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>+<span style="color: #080;">&#91;</span>-<span style="color: #33f;">2</span>,-<span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span>; <span style="color: #228B22;">%give a little band around the control</span>
  left = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  right = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  btm = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
  top =  <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% add a listener on mouse movement events</span>
  tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%tooltip manager</span>
  pointListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'WindowButtonMotionEvent'</span>,@figMouseMove<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%inControl is a flag to prevent multiple triggers of the postTip action</span>
  <span style="color: #228B22;">%while mouse remains in the button</span>
  inControl = <span style="color: #0000FF;">false</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> figMouseMove<span style="color: #080;">&#40;</span>src,evtData<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
    <span style="color: #228B22;">%get the current point</span>
    cPoint = evtData.<span style="">CurrentPoint</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &gt;= left <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &lt;= right <span style="color: #F0F;">&amp;&amp;...</span>
       <span style="">cPoint</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &gt;= btm  <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &lt;= top
&nbsp;
      <span style="color: #0000FF;">if</span> ~inControl <span style="color: #228B22;">%we just entered</span>
        inControl = <span style="color: #0000FF;">true</span>;
        action.<span style="">actionPerformed</span><span style="color: #080;">&#40;</span>actionEvent<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%show the tooltip</span>
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">if</span> inControl <span style="color: #228B22;">%we just existed</span>
        inControl = <span style="color: #0000FF;">false</span>;
        <span style="color: #228B22;">%toggle to make it disappear when leaving button</span>
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%gpMouseMove</span>
&nbsp;
  <span style="color: #0000FF;">function</span> windowMotion<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">%illustrate that we can still do a regular window button motion callback</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>lbl,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Mouse position: %d, %d'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'CurrentPoint'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%windowMotion</span>
&nbsp;
  <span style="color: #0000FF;">function</span> chkTooltipEnable<span style="color: #080;">&#40;</span>src,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>src,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkTooltipEnable</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%inactiveBtnToolTip</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 218px"><img alt="Tooltip on an inactive button" src="http://undocumentedmatlab.com/images/tooltip4.png" title="Tooltip on an inactive button" width="208" height="255" /><p class="wp-caption-text">Tooltip on an inactive button</p></div></center></p>
<p>Comments on the code:</p>
<ol>
<li>The code illustrates that we can successfully add an additional listener to listen for mouse motion events while still carrying out the original <b>WindowButtonMotionFcn</b> callback. This makes chaining callbacks much easier.</li>
<li>The <b><i>handle.listener</i></b> object has an <b>Enable</b> property that we can use to temporarily turn the listener on and off. This can be seen in the <i>chkTooltipEnable()</i> callback for the check box in the code above. If we wanted to permanently remove the listener we would simply use <i><b>delete</b>(pointListener)</i>. Note that <b><i>addlistener</i></b> adds a hidden property to the object being listened to, so that the listener is tied to the object&#8217;s lifecycle. If you create a listener directly using <b><i>handle.listener</i></b> you are responsible for it&#8217;s disposition. Unfortunately, <b><i>addlistener</i></b> fails for HG handles on pre-R2009 Matlab releases, so we use <b><i>handle.listener</i></b> directly.</li>
<li>The code illustrates a good practice when tracking rapidly firing events like mouse movement of handling reentry into the callback while it is still processing a previous callback. Here we use a flag called inControl to prevent the <i>postTip</i> action being continuously fired while the mouse remains in the control.</li>
<li>I was unable to determine if there is any corresponding action for the <i>postTip</i> to dismiss tips so I resorted to using the ToolTipManager to toggle its own <b>Enable</b> property to cleanly hide the tooltip as the mouse leaves the control.</li>
</ol>
<p>Each Matlab callback has an associated event with it. Some of the ones that might be immediately useful at the figure-level are <b>WindowButtonDown</b>, <b>WindowButtonUp</b>, <b>WindowKeyPress</b>, and <b>WindowKeyRelease</b>. They can all be accessed through <b><i>handle.listener</i></b> or <b><i>addlistener</i></b> as in the code above.</p>
<p>Unfortunately, events do not always have names that directly correspond to the callback names. In order to see the list of available events for a particular Matlab object, use the following code, which relies on another undocumented function - <b><i>classhandle</i></b>. Here we list the events for <b><i>gcf</i></b>:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Events'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'SerializeEvent'</span>
    <span style="color:#A020F0;">'FigureUpdateEvent'</span>
    <span style="color:#A020F0;">'ResizeEvent'</span>
    <span style="color:#A020F0;">'WindowKeyReleaseEvent'</span>
    <span style="color:#A020F0;">'WindowKeyPressEvent'</span>
    <span style="color:#A020F0;">'WindowButtonUpEvent'</span>
    <span style="color:#A020F0;">'WindowButtonDownEvent'</span>
    <span style="color:#A020F0;">'WindowButtonMotionEvent'</span>
    <span style="color:#A020F0;">'WindowPostChangeEvent'</span>
    <span style="color:#A020F0;">'WindowPreChangeEvent'</span></pre></div></div>

<p>Note that I have made extensive use of the <b><i>javaMethodEDT</b></i> function to execute Java methods that affect swing components on Swing&#8217;s Event Dispatch Thread. I plan to write about this and related functions in my next article.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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/continuous-slider-callback/' rel='bookmark' title='Permanent Link: Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: 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/additional-uicontrol-tooltip-hacks/' rel='bookmark' title='Permanent Link: Additional uicontrol tooltip hacks'>Additional uicontrol tooltip hacks</a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Additional uicontrol tooltip hacks</title>
		<link>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/</link>
		<comments>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 17:25:52 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Guest bloggers]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[FindJObj]]></category>

		<category><![CDATA[Matt Whitaker]]></category>

		<category><![CDATA[uicontrol]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1114</guid>
		<description><![CDATA[Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Permanent Link: Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Permanent Link: Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; 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/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p><i>Once again I&#8217;d like to welcome guest blogger Matthew Whitaker. Today Matt will discuss uicontrol tooltips hacks as the first of several posts that follow a logical thread culminating in the long promised article on the EDT.</i><br />
<br/></p>
<p>A while back Yair wrote a cool article, <a target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">Spicing up Matlab uicontrol tooltips</a>, describing use of HTML formatting and images in <b><i>uicontrol</i></b> tooltips. I want to share some limitations I&#8217;ve seen with tooltips and their solution using the Matlab control&#8217;s underlying Java object.</p>
<h3 id="disabled-controls">Situation 1: Displaying a tooltip on disabled controls</h3>
<p>One issue with the stock Matlab <b><i>uicontrol</i></b> tooltips is that if you turn the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol_props.html#bqxoiky">uicontrol&#8217;s <b>Enable</b> property</a> to &#8216;inactive&#8217; or &#8216;off&#8217;, its tooltip no longer displays. This is the behavior that we normally want, but occasionally we wish to display a tooltip on a disabled control, for example, to explain why the control is disabled.</p>
<p>You can use the <a target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>findjobj</i></b> utility</a> to find the Java handle for the <b><i>uicontrol</i></b>. This handle can then be used to set the tooltip text. The tooltip will display if you disable the control using its Java handle&#8217;s <b>Enabled</b> property rather than the Matlab handle&#8217;s <b>Enable</b> property:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hButton = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jButton= findjobj<span style="color: #080;">&#40;</span>hButton<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'Enabled'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jButton,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'This is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p>As customary for Java objects, its properties can also be set using their corresponding accessor methods:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,jButton,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jButton,<span style="color:#A020F0;">'Button is disabled for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 262px"><img alt="tooltip on a disabled uicontrol" src="http://UndocumentedMatlab.com/images/tooltip1.png" title="tooltip on a disabled uicontrol" width="252" height="46" /><p class="wp-caption-text">tooltip on a disabled uicontrol</p></div></center></p>
<p>Unfortunately, this hack does not work for &#8216;inactive&#8217; controls. There is no direct Java analogy for inactive controls - it&#8217;s a Matlab extension. It appears that Matlab somehow intercepts the mouse events associated with inactive controls. My next post will show how event callback can be used to display tooltips for inactive controls.</p>
<p>As an alternative for inactive edit-box controls, we can simulate the inactive behavior by setting the Java object&#8217;s <b>Editable</b> property (or by using its <i>setEditable()</i> accessor method), then setting the tooltip. Note that the extremely-useful Java <b>Editable</b> property is unavailable in the Matlab handle, for some inexplicable reason:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'Editable'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'ToolTipText'</span>,<span style="color:#A020F0;">'Text is inactive for a reason'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 242px"><img alt="tooltip on a non-editable editbox" src="http://UndocumentedMatlab.com/images/tooltip2.png" title="tooltip on a non-editable editbox" width="232" height="50" /><p class="wp-caption-text">tooltip on a non-editable editbox</p></div></center></p>
<h3 id="truncated_text">Situation 2: Displaying a tooltip on truncated text</h3>
<p>If we want to conditionally display a tooltip for an editbox <b><i>uicontrol</i></b> when the text exceeds the control&#8217;s width, we can use the <b>TipWhenTruncatedEnabled</b> property (or its corresponding <i>setTipWhenTruncatedEnabled()</i> method). This will display a tooltip with the editbox contents if the string is shown truncated. This saves the user having to scroll through the control to see its contents. I often use this for edit controls that may contain long path names:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.8</span>,<span style="color: #33f;">0.4</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Too Short'</span><span style="color: #080;">&#41;</span>;
hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.1</span>,<span style="color: #33f;">0.7</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.05</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Long Enough to Display a Tool Tip'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
jEditbox1 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jEditbox2 = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditbox1,<span style="color:#A020F0;">'TipWhenTruncatedEnabled'</span>,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% property-based alternative</span>
javaMethod<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setTipWhenTruncatedEnabled'</span>,jEditbox2,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% method-based alternative</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 260px"><img alt="TipWhenTruncatedEnabled tooltip" src="http://UndocumentedMatlab.com/images/tooltip3.png" title="TipWhenTruncatedEnabled tooltip" width="250" height="100" /><p class="wp-caption-text">TipWhenTruncatedEnabled tooltip</p></div></center></p>
<p>The <b>TipWhenTruncatedEnabled</b> property property is also available for multi-line editboxes, but has (obviously) no effect when scrollbars are present. Also note that setting the <b>TipWhenTruncatedEnabled</b> property to true overrides any previous tooltip that might have been set for the editbox.</p>
<p>Finally, note that the <b>TipWhenTruncatedEnabled</b> property can also be set for the editbox component of popup-menu (aka drop-down) controls, <u>after</u> they have been set to be editable using their Java <b>Editable</b> property (note that both properties are false by default for Matlab uicontrols). In the following screenshot, the drop-down&#8217;s editbox component contained an HTML snippet, that is shown unformatted within the edit-box and HTML-formatted in the de-truncated tooltip:</p>
<p><center><div class="wp-caption aligncenter" style="width: 125px"><img alt="de-truncated HTML-format tooltip" src="http://UndocumentedMatlab.com/images/popup_TipWhenTruncatedEnabled2.png" title="de-truncated HTML-format tooltip" width="115" height="72" /><p class="wp-caption-text">de-truncated HTML-format tooltip</p></div></center></p>
<h3 id="tooltip_timing">Situation 3: Controlling tooltip timing</h3>
<p>As you have probably noticed, there is a slight delay between the time your mouse enters the control and when the tooltip actually appears. If you display a tooltip over a control for sufficiently long the tooltip will then disappear. Sometimes the default delays are too slow or fast for your application. These times can be controlled through the <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/ToolTipManager.html">javax.swing.ToolTipManager</a>. The ToolTipManager sets these parameters globally (including for your Matlab desktop components), but they are not persistent between sessions.</p>
<p>Some examples using the ToolTipManager:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is a button.'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
txt = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'edit'</span>,<span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Edit Text'</span>,<span style="color:#A020F0;">'Tooltip'</span>,<span style="color:#A020F0;">'This is editable text'</span>,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">75</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%static method to get ToolTipManager object</span>
initialDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getInitialDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before display in milliseconds (=750 on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set tooltips to appear immediately</span>
&nbsp;
dismissDelay = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getDismissDelay'</span>,tm<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%get the delay before the tooltip disappears (=10000 (10 sec) on my system)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,<span style="color: #33f;">2000</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%set the dismiss delay to 2 seconds</span>
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%turns off all tooltips in system (including the Matlab desktop)</span>
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setInitialDelay'</span>,tm,initialDelay<span style="color: #080;">&#41;</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setDismissDelay'</span>,tm,dismissDelay<span style="color: #080;">&#41;</span>;</pre></div></div>

<p>Note that I have made extensive use of the undocumented built-in <b><i>javaMethodEDT</i></b> function to execute Java methods that affect Swing components on the Swing Event Dispatch Thread (EDT). I plan to write about EDT following my next post on event callback chaining.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Permanent Link: Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Permanent Link: Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; 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/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Continuous slider callback</title>
		<link>http://undocumentedmatlab.com/blog/continuous-slider-callback/</link>
		<comments>http://undocumentedmatlab.com/blog/continuous-slider-callback/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:13:39 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Listeners]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[callbacks]]></category>

		<category><![CDATA[FindJObj]]></category>

		<category><![CDATA[listener]]></category>

		<category><![CDATA[schema.prop]]></category>

		<category><![CDATA[uicontrol]]></category>

		<category><![CDATA[Undocumented feature]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1052</guid>
		<description><![CDATA[Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Permanent Link: Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Permanent Link: Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='Permanent Link: FindJObj GUI - display container hierarchy'>FindJObj GUI - 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>Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab&#8217;s <a target="_blank" rel="nofollow" href="http://blinkdagger.com/matlab/matlab-gui-tutorial-slider/">slider uicontrol</a> invokes the user callback only when the mouse button is released, and not continuously while the slider&#8217;s thumb is dragged. This functionality was again referred-to <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/272224">yesterday</a>, and I decided it merits a dedicated post.</p>
<p>There are three distinct simple ways to achieve continuous callbacks:</p>
<h3 id="Java_Callbacks">Using Java callbacks</h3>
<p>As explained in an earlier <a target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">article</a>, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders&#8217; underlying Java objects, which are really not <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">JSliders</a> but <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#operation">JScrollBars</a>, have an <b>AdjustmentValueChangedCallback</b> property that is useful for our purposes and is accessible using the <a target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj utility</i></b></a>. Simply download <b><i>FindJObj</i></b> from the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">File Exchange</a>, and then:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hSlider = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'slider'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jScrollBar = findjobj<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
jScrollBar.<span style="">AdjustmentValueChangedCallback</span> = @myCbFcn;
<span style="color: #228B22;">% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)</span></pre></div></div>

<p>Where <i>myCbFcn</i> is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider&#8217;s thumb is dragged.</p>
<h3 id="Event_Listener">Using an event listener</h3>
<p>An alternative to the Java route is to use Matlab&#8217;s undocumented <b><i>handle.listener</i></b> function to listen to the slider&#8217;s <b>Action</b> event, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSlider,<span style="color:#A020F0;">'ActionEvent'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div>

<p>This alternative is used by Matlab&#8217;s own <b><i>imscrollpanel</i></b> function:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isJavaFigure
   <span style="color: #228B22;">% Must use these ActionEvents to get continuous events fired as slider</span>
   <span style="color: #228B22;">% thumb is dragged. Regular callbacks on sliders give only one event</span>
   <span style="color: #228B22;">% when the thumb is released.</span>
   hSliderHorListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>;
   hSliderVerListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollVertical<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hScrollpanel,<span style="color:#A020F0;">'sliderListeners'</span>,<span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span>hSliderHorListener hSliderVerListener<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Unfortunately, the event route is only available with Java Figures,</span>
   <span style="color: #228B22;">% so platforms without Java Figure support get discrete events only</span>
   <span style="color: #228B22;">% when the mouse is released from dragging the slider thumb.</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color:#A020F0;">'callback'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color:#A020F0;">'callback'</span>,@scrollVertical<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div>

<h3 id="Property_Listener">Using a property listener</h3>
<p>The <b><i>handle.listener</i></b> function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following <b>Value</b> property updates, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">hhSlider = handle<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhSlider,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhSlider,hProp,<span style="color:#A020F0;">'PropertyPostSet'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div>

<p>In addition to &#8216;PropertyPostSet&#8217;, we could also listen on &#8216;PropertyPreSet&#8217;, which is triggered immediately <u>before</u> the property is modified. There are also corresponding &#8216;*Get&#8217; options. In relatively old Matlab releases (I believe R2007b and earlier, but I&#8217;m not certain), the option names were simply &#8216;PostSet&#8217;, &#8216;PreSet&#8217; etc., without the &#8216;Property&#8217; prefix.</p>
<p>Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Permanent Link: Inactive Control Tooltips &#038; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Permanent Link: Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Permanent Link: Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li><li><a href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='Permanent Link: FindJObj GUI - display container hierarchy'>FindJObj GUI - 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/continuous-slider-callback/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Solving a MATLAB bug by subclassing</title>
		<link>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/</link>
		<comments>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 23:57:25 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[Guest bloggers]]></category>

		<category><![CDATA[Hidden property]]></category>

		<category><![CDATA[Low risk of breaking in future versions]]></category>

		<category><![CDATA[Stock Matlab function]]></category>

		<category><![CDATA[Toolbox]]></category>

		<category><![CDATA[Matt Whitaker]]></category>

		<category><![CDATA[pure Matlab]]></category>

		<category><![CDATA[Undocumented property]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1110</guid>
		<description><![CDATA[Matlab's Image Processing Toolbox's impoint function contains an annoying bug that can be fixed using some undocumented properties.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: 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/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/html-support-in-matlab-uicomponents/' rel='bookmark' title='Permanent Link: 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/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p><i>I would like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/author/94021">Matthew Whitaker</a>. Many of Matt&#8217;s CSSM submissions offer important insight of internal Matlab functionality. As shall be seen by today&#8217;s article and some future submissions, Matt has plenty to share vis-a-vis Matlab&#8217;s undocumented functionality.</i></p>
<p>In my day-to-day work I make extensive use of MATLAB&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/products/image/">Image Processing Toolbox</a> (IPT). One area of the toolbox that has seen considerable change over the last few releases has been the development of a set of <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f17-6011.html">modular tools</a> to aid in GUI design for image processing applications. In this article, I examine a bug in one of those tools to illustrate how we can use the power of subclassing these objects (using an undocumented property) to design a simple and effective workaround.</p>
<h3>The problem</h3>
<p>The problem arose as I was refactoring some code that was written in R2006b to R2009b. The code in question uses the <b><i>impoint</i></b> tool on an image along with an associated <b><i>text</i></b> object that moves with the point to display information as it is dragged around the image. At the time of the R2006b release the <b><i>impoint</i></b> tool was written as an API. In R2006b the call to <b><i>impoint</i></b> returns a handle to an <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/hggroup.html"><b><i>hggroup</i></b></a> containing a structure of function handles in its application data under the tag &#8216;API&#8217;. This programming pattern was common before the advent of the new class syntax in MATLAB version 7.6 (R2008a).</p>
<p>Here is an example of how <b><i>impoint</i></b> would be used in R2006b:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> impointBehavior_R2006b
<span style="color: #228B22;">%IMPOINTBEHAVIOR_R2006B shows how impoint would be used in R2006b</span>
<span style="color: #228B22;">%Note: RUN UNDER R2006B (will run under R2009b but actually uses</span>
<span style="color: #228B22;">%classdef impoint so it will show the same issue)</span>
&nbsp;
  <span style="color: #228B22;">% Display the image in a figure window</span>
  <span style="color: #0000FF;">figure</span>;  imshow<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'rice.png'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% In R2006b calling impoint returns the hggroup handle</span>
  h = impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% In 2006b iptgetapi returns a structure of function handles</span>
  api = iptgetapi<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Add a new position callback to set the text string</span>
  api.<span style="">addNewPositionCallback</span><span style="color: #080;">&#40;</span>@newPos_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Construct boundary constraint function so we can't go outside the axes</span>
  fcn = makeConstrainToRectFcn<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'impoint'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'XLim'</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'YLim'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  api.<span style="">setDragConstraintFcn</span><span style="color: #080;">&#40;</span>fcn<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Fire callback so we get initial text</span>
  newPos_Callback<span style="color: #080;">&#40;</span>api.<span style="">getPosition</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> newPos_Callback<span style="color: #080;">&#40;</span>newPos<span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">% Display the current point position in a text label</span>
    api.<span style="">setString</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'(%1.0f,%1.0f)'</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%newPos_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointBehavior_R2006b</span></pre></div></div>

<p>The code above, when run in R2006b, produces the desired behavior of displaying a text object containing the point coordinates that moves around with the point as it is dragged around the axes.</p>
<p>In R2009b, <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/impoint.html"><b><i>impoint</i></b></a> is now a true MATLAB class using the new <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brqy3km-6.html"><b><i>classdef</i></b></a> syntax, so I wanted to update the existing code. Initially this appeared to be a straightforward translation of the code to make use of the new <b><i>impoint</i></b> class syntax. The first attempt to rewrite the code was:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> impointBehavior_R2009b
<span style="color: #228B22;">%IMPOINTBEHAVIOR_R2009B shows the undesirable behavior when</span>
<span style="color: #228B22;">%using the setString method in R2009b. </span>
&nbsp;
  <span style="color: #228B22;">% Display the image in a figure window</span>
  <span style="color: #0000FF;">figure</span>;  imshow<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'rice.png'</span><span style="color: #080;">&#41;</span>;
  h = impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Add a new position callback to set the text string</span>
  h.<span style="">addNewPositionCallback</span><span style="color: #080;">&#40;</span>@newPos_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Construct boundary constraint function so we can't go outside the axes</span>
  fcn = makeConstrainToRectFcn<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'impoint'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'XLim'</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'YLim'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Enforce boundary constraint function</span>
  h.<span style="">setPositionConstraintFcn</span><span style="color: #080;">&#40;</span>fcn<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Fire callback so we get initial text</span>
  newPos_Callback<span style="color: #080;">&#40;</span>h.<span style="">getPosition</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> newPos_Callback<span style="color: #080;">&#40;</span>newPos<span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">% Display the current point position in a text label</span>
    h.<span style="">setString</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'(%1.0f,%1.0f)'</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%newPos_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointBehavior_R2009b</span></pre></div></div>

<p>Unfortunately, when this code is run, dragging the mouse around the axes produces a trail of labels as shown below:<br />
<center><div class="wp-caption aligncenter" style="width: 305px"><img alt="Before - buggy behavior" src="http://UndocumentedMatlab.com/images/impoint_before.png" title="Before - buggy behavior" width="295" height="295" /><p class="wp-caption-text">Before - buggy behavior</p></div></center></p>
<p>Opening up <b><i>impoint</i></b>.m in the editor and tracing the code revealed that <b><i>impoint</i></b>&#8217;s <i>setString</i> method creates a new <b><i>text</i></b> object each time it is used. I reported this to MATLAB and the bug is now documented on the MathWorks Support site (<a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/bugreports/574846">574846</a>).</p>
<h3>The solution</h3>
<p>So how do we work around this bug to get to the behavior we want? One solution would be to rewrite the offending MATLAB code but this is somewhat risky in terms of maintainability and compatibility.</p>
<p>A more elegant solution is to subclass the <b><i>impoint</i></b> class and substitute the <i>setString</i> behavior we want. Looking at the <b><i>impoint</i></b> code we find that <b><i>impoint</i></b> is a subclass of <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/imroi.html"><b><i>imroi</i></b></a>. In the <b><i>imroi</i></b> property declarations we see a number of undocumented properties that are protected. We can access these properties in a subclass but not outside the class. One of these undocumented properties is h_group which is an <b><i>hggroup</i></b> that contains the handle graphic objects that make up the <b><i>impoint</i></b> on the screen. The label, when created, becomes part of this <b><i>hggroup</i></b> with its <b>Tag</b> property set to &#8216;label&#8217;. When performing the <i>setString</i> method the behavior we want to see is that if the <b><i>text</i></b> object exists we want to update its <b>String</b> property. If it does not exist we want it to perform its existing functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">classdef</span> impointtextupdate &lt; impoint
<span style="color: #228B22;">%IMPOINTTEXTUPDATE subclasses impoint to override the setString</span>
<span style="color: #228B22;">%method of impoint so that it does not create a new text object</span>
<span style="color: #228B22;">%each time it is called.   </span>
&nbsp;
  <span style="color: #0000FF;">methods</span>
    <span style="color: #0000FF;">function</span> obj = impointtextupdate<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
      obj = obj@impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointtextupdate</span>
&nbsp;
    <span style="color: #0000FF;">function</span> setString<span style="color: #080;">&#40;</span>obj,str<span style="color: #080;">&#41;</span>
      <span style="color: #228B22;">%override impoint setString</span>
&nbsp;
      <span style="color: #228B22;">%check to see if there is already a text label</span>
      label = <span style="color: #0000FF;">findobj</span><span style="color: #080;">&#40;</span>obj.<span style="">h_group</span>,<span style="color:#A020F0;">'Type'</span>,<span style="color:#A020F0;">'text'</span>,<span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'label'</span><span style="color: #080;">&#41;</span>;
&nbsp;
      <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>label<span style="color: #080;">&#41;</span>
        <span style="color: #228B22;">%do the default behavior</span>
        setString@impoint<span style="color: #080;">&#40;</span>obj,str<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">else</span>
        <span style="color: #228B22;">%update the existing tag</span>
        <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>label<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'String'</span>,str<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%setString</span>
&nbsp;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%methods</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointtextupdate</span></pre></div></div>

<p>Substituting calls to <b><i>impoint</i></b> with the new <b><i>impointupdatetext</i></b> subclass now produces the desired effect as shown below:<br />
<center><div class="wp-caption aligncenter" style="width: 305px"><img alt="After - expected behavior" src="http://UndocumentedMatlab.com/images/impoint_after.png" title="After - expected behavior" width="295" height="295" /><p class="wp-caption-text">After - expected behavior</p></div></center></p>
<h3>Conclusions</h3>
<p>This case illustrates a couple of points:</p>
<ul>
<li>Much of the existing code in the MATLAB toolboxes is being updated to the new object oriented syntax. This presents many opportunities to easily and elegantly modify the default behavior without modifying provided toolbox code In the example above we retain all the desirable behavior of <b><i>impoint</i></b> while overriding the undesirable behavior.</li>
<li>Many of the properties and methods in the provided toolbox objects are hidden or protected and are undocumented. It takes some simple detective work to find these out through examining the code. MATLAB is very generous in providing much of the existing code openly. Open the functions and classes you use in the editor to really find out how they work. Over the years I&#8217;ve learned and adopted a lot of useful MATLAB programming patterns by examining the code in the various toolboxes (there are a few coding horrors as well!).</li>
</ul>
<p>I hope to explore some other under-documented features of the IPT and other toolboxes in future posts.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Permanent Link: 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/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/html-support-in-matlab-uicomponents/' rel='bookmark' title='Permanent Link: 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/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Customizing listbox &amp; editbox scrollbars</title>
		<link>http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/</link>
		<comments>http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 17:40:03 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Medium risk of breaking in future versions]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[FindJObj]]></category>

		<category><![CDATA[uicontrol]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=994</guid>
		<description><![CDATA[Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/' rel='bookmark' title='Permanent Link: Rich Matlab editbox contents'>Rich Matlab editbox contents</a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Permanent Link: Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Permanent Link: Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p>A few days ago, a CSSM forum reader asked how to <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/271023">modify Matlab&#8217;s listbox scrollbars</a>. Another user asked how to <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/271303">configure line-wrapping</a>. I thought this is a good opportunity to describe how listbox and editbox scrollbars can be customized. The timing is particularly opportune, after I have recently described how the Matlab <a target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/">Editbox can be customized</a> by accessing its underlying Java object using the <a target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>.</p>
<p>Both the listbox and the multi-line editbox uicontrols share a similar design: a multi-line Java control embedded within a <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JViewport.html">JViewport</a> within a <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/JScrollPane.html">JScrollPane</a> (note that for historical reasons, the Java view-port class is called JView<b>p</b>ort rather than the more standard <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/CamelCase">camel-cased</a> JView<b>P</b>ort). In addition to the view-port, the containing scroll-pane also contains two scrollbars (horizontal and vertical), as expected from <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html">standard Java scroll-panes</a>. </p>
<p><center><div class="wp-caption aligncenter" style="width: 266px"><img alt="JScrollPane components" src="http://undocumentedmatlab.com/images/JScrollPane.gif" title="JScrollPane components" width="256" height="248" /><p class="wp-caption-text">JScrollPane components</p></div></center></p>
<h3 id="scrollbarPolicies">Scrollbar policies</h3>
<p>Control of the scroll-pane&#8217;s scrollbar behavior is done via the JScrollPane&#8217;s <b>VerticalScrollBarPolicy</b> and <b>HorizontalScrollBarPolicy</b> properties.</p>
<p><b>VerticalScrollBarPolicy</b> accepts the self-explanatory values of:
<ul>
<li>VERTICAL_SCROLLBAR_ALWAYS (=22)</li>
<li>VERTICAL_SCROLLBAR_NEVER (=21)</li>
<li>and VERTICAL_SCROLLBAR_AS_NEEDED (=20)</li>
</ul>
<p><b>HorizontalScrollBarPolicy</b> accepts:
<ul>
<li>HORIZONTAL_SCROLLBAR_ALWAYS (=32)</li>
<li>HORIZONTAL_SCROLLBAR_NEVER (=31)</li>
<li>and HORIZONTAL_SCROLLBAR_AS_NEEDED (=30)</li>
</ul>
<p>All these properties are static enumerated constants that can be referred using either their Java notation (e.g., JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) or their equivalent numeric values. Using the non-numeric format is better, since it is more readable and the numeric values may change, but the choice is yours.</p>
<p>By default, Matlab implements a <b>VerticalScrollBarPolicy</b> of VERTICAL_SCROLLBAR_ALWAYS for sufficiently tall uicontrols (&gt;20-25 pixels, which practically means always) and VERTICAL_SCROLLBAR_NEVER for shorter uicontrols.</p>
<p>For the horizontal scrollbar, Matlab implements a <b>HorizontalScrollBarPolicy</b> of HORIZONTAL_SCROLLBAR_NEVER for all editboxes and for narrow listboxes (&lt;35 pixels), and HORIZONTAL_SCROLLBAR_AS_NEEDED for wide listboxes.</p>
<p>These settings are generally satisfactory. However, in some cases users may wish to modify the settings. For example, the default <b>VerticalScrollBarPolicy</b> setting of VERTICAL_SCROLLBAR_ALWAYS causes the vertical scrollbar to appear even when unneeded (the entire editbox content is visible). Also, we may wish to have a horizontal scrollbar on narrow listboxes and editboxes, something that the standard HORIZONTAL_SCROLLBAR_NEVER prevents. In both cases, a *_SCROLLBAR_AS_NEEDED policy might be more appropriate.</p>
<p>To modify these settings, we simply need to get the uicontrol&#8217;s underlying Java reference handle (using the <b><i>FindJObj</i></b> utility), and modify the appropriate property. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a multi-line (Max&gt;1) editbox uicontrol</span>
hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'max'</span>,<span style="color: #33f;">5</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the Java scroll-pane container reference</span>
jScrollPane = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Modify the scroll-pane's scrollbar policies</span>
<span style="color: #228B22;">% (note the equivalent alternative methods used below)</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'VerticalScrollBarPolicy'</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED</span>
jScrollPane.<span style="">setHorizontalScrollBarPolicy</span><span style="color: #080;">&#40;</span><span style="color: #33f;">30</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 210px"><img alt="default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)" src="http://UndocumentedMatlab.com/images/editbox7a.png" title="default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)" width="75" height="60" /><p class="wp-caption-text">default scrollbars (VERTICAL_SCROLLBAR_ALWAYS)</p></div>  <div class="wp-caption aligncenter" style="width: 230px"><img alt="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" src="http://UndocumentedMatlab.com/images/editbox7b.png" title="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" width="75" height="60" /> &nbsp;&nbsp;&nbsp; <img alt="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" src="http://UndocumentedMatlab.com/images/editbox7c.png" title="non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)" width="75" height="60" /><p class="wp-caption-text">non-default scrollbars (VERTICAL_SCROLLBAR_AS_NEEDED)</p></div></center></p>
<p>Note that updating the uicontrol handle (hEditbox)&#8217;s <b>Position</b> property has the side-effect of automatically reverting the scrollbar policies to their default values (HORIZONTAL_SCROLLBAR_NEVER and VERTICAL_SCROLLBAR_ALWAYS/NEVER). This also happens whenever the uicontrol is resized interactively (by resizing its container figure window, for example). It is therefore advisable to set jScrollPane&#8217;s <b>ComponentResizedCallback</b> property to &#8220;unrevert&#8221; the policies:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">cbStr = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'set(gcbo,'</span><span style="color:#A020F0;">'VerticalScrollBarPolicy'</span><span style="color:#A020F0;">',%d)'</span>, <span style="color: #F0F;">...</span>
                <span style="">jScrollPane</span>.<span style="">VERTICAL_SCROLLBAR_AS_NEEDED</span><span style="color: #080;">&#41;</span>;
hjScrollPane = handle<span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hjScrollPane,<span style="color:#A020F0;">'ComponentResizedCallback'</span>,cbStr<span style="color: #080;">&#41;</span>;</pre></div></div>

<h3 id="lineWrap">Line-wrapping</h3>
<p>By default, line-wrapping is turned on, effectively disabling horizontal scrolling (which is why Matlab set the <b>HorizontalScrollBarPolicy</b> to HORIZONTAL_SCROLLBAR_NEVER. However, in some cases it may be more useful to turn line-wrapping off and horizontal scrolling on using the TextArea&#8217;s <i>setWrapping()</i> method. Here&#8217;s a usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">jViewPort = jScrollPane.<span style="">getViewport</span>;
jEditbox = jViewPort.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setWrapping</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% do *NOT* use set(...)!!!</span>
newPolicy = jScrollPane.<span style="">HORIZONTAL_SCROLLBAR_AS_NEEDED</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jScrollPane,<span style="color:#A020F0;">'HorizontalScrollBarPolicy'</span>,newPolicy<span style="color: #080;">&#41;</span>;</pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 150px"><img alt="multi-line editbox with wrapping on" src="http://UndocumentedMatlab.com/images/editbox11a.png" title="multi-line editbox with wrapping on" width="140" height="60" /><p class="wp-caption-text">multi-line editbox with wrapping on</p></div>  <div class="wp-caption aligncenter" style="width: 150px"><img alt="multi-line editbox with wrapping off" src="http://UndocumentedMatlab.com/images/editbox11b.png" title="multi-line editbox with wrapping off" width="140" height="60" /><p class="wp-caption-text">multi-line editbox with wrapping off</p></div></center></p>
<p>Notes:</p>
<ol>
<li><i>setWrapping()</i> only works for the default EditorKit, and fails for <a target="_blank" href="http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#EditorKit">HTMLEditorKit</a> – This is due to HTML&#8217;s inherent wrapping behavior, as can easily be seen in any browser webpage.</li>
<li>while <i>setWrapping()</i> may seem like a regular setter method for a <b>Wrapping</b> property, in reality it is not. Actually, <b><i>set</i></b>(jEditbox,&#8217;wrapping&#8217;,flag) may crash Matlab. So, always use the <i>setWrapping(flag)</i> method variant, which is entirely safe.</li>
</ol>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/' rel='bookmark' title='Permanent Link: Rich Matlab editbox contents'>Rich Matlab editbox contents</a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Permanent Link: Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Permanent Link: Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/feed/</wfw:commentRss>
		</item>
		<item>
		<title>setPrompt - Setting the Matlab Desktop prompt</title>
		<link>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/</link>
		<comments>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 15:30:54 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[Desktop]]></category>

		<category><![CDATA[High risk of breaking in future versions]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[Listeners]]></category>

		<category><![CDATA[Semi-documented function]]></category>

		<category><![CDATA[Undocumented feature]]></category>

		<category><![CDATA[callbacks]]></category>

		<category><![CDATA[listener]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=965</guid>
		<description><![CDATA[The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Permanent Link: Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Permanent Link: Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='Permanent Link: EditorMacro v2 - setting Command Window key-bindings'>EditorMacro v2 - setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p>A few days ago, a reader emailed me with a challenge to modify the standard matlab Command-Window prompt from &#8220;&gt;&gt; &#8221; to some other string, preferably a dynamic prompt with the current timestamp. At first thought this cannot be done: The Command-Window prompts are hard-coded and to the best of my knowledge cannot be modified via <a target="_blank" href="http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/">properties</a> or <a target="_blank" href="http://undocumentedmatlab.com/blog/changing-system-preferences-programmatically/">system preferences</a>.</p>
<p>So the prompt can (probably) not be modified in advance, but what if it could be modified after being displayed? It is true that my <a target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><b<i>cprintf</i></b> utility</a> modifies the Command-Window contents in order to display formatted text in a variety of font colors. But this case is different since <b<i>cprintf</i></b> runs once synchronously (user-invoked), whereas the prompt appears asynchronously multiple times.</p>
<p>There are two methods of handling multiple asynchronous events in Matlab: setting a callback on the object, and setting a PostSet <b><i>handle.listener</i></b> (or <b><i>schema.listener</i></b>) on the relevant object property. The first of these methods is a well-known Matlab practice, although we shall see that it uses an undocumented callback and functionality; the PostSet method is entirely undocumented and not well-known and shall be described in some later article. I decided to use the callback method to set the prompt - interested readers can try the PostSet method.</p>
<h3>Setting the Command Window&#8217;s callback</h3>
<p>The solution involved finding the Command-Window reference handle, and setting one of its <a target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">many callbacks</a>, in our case <b>CaretUpdateCallback</b>. This callback is fired whenever the desktop text is modified, which is an event we trap to replace the displayed prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the reference handle to the Command Window text area</span>
jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
<span style="color: #0000FF;">try</span>
  cmdWin = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Command Window'</span><span style="color: #080;">&#41;</span>;
  jTextArea = cmdWin.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getViewport</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  commandwindow;
  jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Instrument the text area's callback</span>
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#123;</span>@setPromptFcn,newPrompt<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div>

<p>Now that we have the Command-Window object callback set, we need to set the logic of prompt replacement - this is done in the internal Matlab function <i>setPromptFcn</i>. Here is its core code:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Does the displayed text end with the default prompt?</span>
<span style="color: #228B22;">% Note: catch a possible trailing newline</span>
<span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
cwText = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'Text'</span><span style="color: #080;">&#41;</span>;
pos = strfind<span style="color: #080;">&#40;</span>cwText<span style="color: #080;">&#40;</span><span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Short prompts need to be space-padded</span>
  newLen = jTextArea.<span style="">getCaretPosition</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&lt;<span style="color: #33f;">3</span>
    newPrompt<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span>+<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = <span style="color:#A020F0;">' '</span>;
  <span style="color: #0000FF;">elseif</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&gt;<span style="color: #33f;">3</span>
    fprintfStr = newPrompt<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span>end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
    newLen = newLen + <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% The Command-Window text should be modified on the EDT</span>
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
            <span style="">newPrompt</span><span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, newLen-<span style="color: #33f;">3</span>, newLen<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div>

<p>In this code snippet, note that we space-pad prompt string that are shorter than 3 characters: this is done to prevent an internal-Matlab mixup when displaying additional text - Matlab &#8220;knows&#8221; the Command-Window&#8217;s text position and it gets mixed up if it turns out to be shorter than expected.</p>
<p>Also note that I use the semi-documented <b><i>awtinvoke</i></b> function to replace the default prompt (and an automatically-appended space) on the Event-Dispatch Thread (more on this in a future article). Since Matlab R2008a, I could use the more convenient <b><i>javaMethodEDT</i></b> function, but I wanted my code to work on all prior Matlab 7 versions, where <b><i>javaMethodEDT</i></b> was not yet available.</p>
<h3>Preventing callback re-entry</h3>
<p>The callback snippet above would enter an endless loop if not changed: whenever the prompt is modified the callback would have been re-fired, the prompt re-modified and so on endlessly. There are many methods of preventing callback re-entry - here&#8217;s the one I chose:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> setPromptFcn<span style="color: #080;">&#40;</span>jTextArea,eventData,newPrompt<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Prevent overlapping reentry due to prompt replacement</span>
  <span style="color: #0000FF;">persistent</span> inProgress
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>inProgress<span style="color: #080;">&#41;</span>
    inProgress = <span style="color: #33f;">1</span>;  <span style="color: #228B22;">%#ok unused</span>
  <span style="color: #0000FF;">else</span>
    <span style="color: #0000FF;">return</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% *** Prompt modification code goes here ***</span>
&nbsp;
    <span style="color: #228B22;">% force prompt-change callback to fizzle-out...</span>
    <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.02</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - ignore errors...</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Enable new callbacks now that the prompt has been modified</span>
  inProgress = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptFcn</span></pre></div></div>

<h3>Handling multiple prompt types</h3>
<p>I now wanted my function to handle both static prompt strings (like: &#8216;[Yair] &#8216;) and dynamic prompts (like: &#8216;[25-Jan-2010 01:00:51] &#8216;). This is done by accepting string-evaluable strings/functions:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Try to evaluate the new prompt as a function</span>
<span style="color: #0000FF;">try</span>
  origNewPrompt = newPrompt;
  newPrompt = <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  <span style="color: #0000FF;">try</span>
    newPrompt = <span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - probably a string...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>origNewPrompt<span style="color: #080;">&#41;</span>
  newPrompt = origNewPrompt;
<span style="color: #0000FF;">end</span></pre></div></div>

<h3>File Exchange submission</h3>
<p>I then added some edge-case error handling and wrapped everything in a single utility called <b><i>setPrompt</i></b> that is now <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26471">available on the File Exchange</a>.</p>
<p>In the future, if I find time, energy and interest, maybe I&#8217;ll combine <b><i>cprintf</i></b>&#8217;s font-styling capabilities, to enable setting colored prompts.</p>
<h3>Setting a continuously-updated timestamp prompt</h3>
<p>Using the code above, we can now display a dynamic timestamp prompt, as follows:<br />
<center><div class="wp-caption aligncenter" style="width: 438px"><img alt="setPrompt usage examples" src="http://UndocumentedMatlab.com/images/setPrompt.png" title="setPrompt usage examples" width="428" height="224" /><p class="wp-caption-text"><b><i>setPrompt</i></b> usage examples</p></div></center></p>
<p>However, the displayed timestamp is somewhat problematic in the sense that it indicates the time of prompt <u>creation</u> rather than the time that the associated Command-Window command was <u>executed</u>. In the screenshot above, [25-Jan-2010 01:29:42] is the time that the <b><i>234</i></b> command was executed, not the time that the <b><i>setPrompt</i></b> command was executed. This is somewhat misleading. It would be better if the last (current) timestamp was continuously updated and would therefore always display the latest command&#8217;s execution time. This can be done using a Matlab <b><i>timer</i></b> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% This is entered in the main function before setting the prompt:</span>
stopPromptTimers;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'timestamp'</span><span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Update initial prompt &amp; prepare a timer to continuously update it</span>
  newPrompt = @<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'['</span>,<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'] '</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  start<span style="color: #080;">&#40;</span>timer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'ExecutionMode'</span>,<span style="color:#A020F0;">'fixedDelay'</span>, <span style="color:#A020F0;">'ObjectVisibility'</span>,<span style="color:#A020F0;">'off'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'Period'</span>,<span style="color: #33f;">0.99</span>, <span style="color:#A020F0;">'StartDelay'</span>,<span style="color: #33f;">0.5</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'TimerFcn'</span>,<span style="color: #080;">&#123;</span>@setPromptTimerFcn,jTextArea<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Stop &amp; delete any existing prompt timer(s)</span>
<span style="color: #0000FF;">function</span> stopPromptTimers
  <span style="color: #0000FF;">try</span>
    timers = timerfindall<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>
      stop<span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% stopPromptTimers</span>
&nbsp;
<span style="color: #228B22;">% Internal timer callback function</span>
<span style="color: #0000FF;">function</span> setPromptTimerFcn<span style="color: #080;">&#40;</span>timerObj,eventData,jTextArea<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
    pos = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'setPromptPos'</span><span style="color: #080;">&#41;</span>;
    newPrompt = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
              <span style="">newPrompt</span>, pos, pos+<span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptTimerFcn</span></pre></div></div>

<p>Can you come up with some innovative prompts? If so, please share them in a comment below.</p>
<p><u>Update 2010-Jan-26</u>: The code in this article was updated since it was first published yesterday.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Permanent Link: Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Permanent Link: Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Permanent Link: 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/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='Permanent Link: EditorMacro v2 - setting Command Window key-bindings'>EditorMacro v2 - setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rich Matlab editbox contents</title>
		<link>http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/</link>
		<comments>http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 21:04:11 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[High risk of breaking in future versions]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[FindJObj]]></category>

		<category><![CDATA[uicontrol]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=944</guid>
		<description><![CDATA[The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/' rel='bookmark' title='Permanent Link: Customizing listbox &#038; editbox scrollbars'>Customizing listbox &#038; editbox scrollbars</a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Permanent Link: Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p>In an earlier post, I mentioned that <a target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">most Matlab uicontrols support HTML strings</a>. Unfortunately, HTML is not supported in multi-line editbox contents. Today I will show how this limitation can be removed for a multi-line editbox, thereby enabling rich contents (enabling HTML for a single-line editbox needs a different solution).</p>
<p>We first need to get the editbox&#8217;s underlying Java object, as explained in my previous article about <a target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">the <b><i>findjobj</i></b> utility</a>. Since a multi-line editbox is contained within a scroll-pane, we need to dig within the scrollpane container to find the actual editable area object:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a multi-line (Max&gt;1) editbox uicontrol</span>
hEditbox = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'edit'</span>, <span style="color:#A020F0;">'max'</span>,<span style="color: #33f;">5</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get the Java scroll-pane container reference</span>
jScrollPane = findjobj<span style="color: #080;">&#40;</span>hEditbox<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% List the scroll-pane's contents:</span>
&gt;&gt; jScrollPane.<span style="">list</span>
com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,100x50,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 javax.<span style="">swing</span>.<span style="">JViewport</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,81x48,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">EditTextPeer</span>$hgTextEditMultiline<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,81x48,<span style="color: #F0F;">...</span>,kit=javax.<span style="">swing</span>.<span style="">text</span>.<span style="">StyledEditorKit</span>@ce05fc,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span>$<span style="color: #33f;">1</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">82</span>,<span style="color: #33f;">1</span>,17x48,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">31</span>,17x17,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,17x17,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
 com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">utils</span>.<span style="">UIScrollPane</span>$<span style="color: #33f;">2</span><span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
  com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsScrollBarUI</span>$WindowsArrowButton<span style="color: #080;">&#91;</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,0x0,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span></pre></div></div>

<p>In this listing, we see that jScrollPane contains a JViewport and two scrollbars (horizontal and vertical), as expected from <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html">standard Java scroll-panes</a>. We need the internal hgTextEditMultiline object:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">jViewPort = jScrollPane.<span style="">getViewport</span>;
jEditbox = jViewPort.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p>The retrieved jEditbox reference, is an object of class com.mathworks.hg.peer.EditTextPeer$hgTextEditMultiline, which indirectly extends the standard <a target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html">javax.swing.JTextPane</a>. The default Matlab implementation of the editbox uicontrol simply enables a multi-line vertical-scrollable text area using the system font. However, the underlying JTextPane object enables many important customizations, including the ability to specify different font attributes (size/color/bold/italic etc.) and paragraph attributes (alignment etc.) for text segments (called style runs) and the ability to embed images, HTML and other controls.</p>
<p>Setting rich contents can be done in several alternative ways. From easiest to hardest:</p>
<h3 id="pageURL">Setting page URL</h3>
<p>Use the <i>setPage(url)</i> method to load a text page from the specified URL (any pre-existing editbox content will be erased). The page contents may be plain text, HTML  or RTF. The content type will automatically be determined and the relevant <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.html">StyledEditorKit</a> and <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledDocument.html">StyledDocument</a> will be chosen for that content. Additional StyledEditorKit content parsers can be registered to handle additional content types.  Here&#8217;s an example loading an HTML page:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setPage</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'http://tinyurl.com/c27zpt'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p>where the URL&#8217;s contents are:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/dukeWaveRed.gif&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;64&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;64&quot;</span>&gt;</span>
This is an uneditable <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">code</span>&gt;</span>JEditorPane<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">code</span>&gt;</span>, which was
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">em</span>&gt;</span>initialized<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">em</span>&gt;</span> with <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>HTML<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> text 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">font</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span>-<span style="color: #cc66cc;">2</span>&gt;</span>from<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">font</span>&gt;</span> a <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">font</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span>+<span style="color: #cc66cc;">2</span><span style="color: #ff0000;">&quot;&gt;</span></span>URL<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">font</span>&gt;</span>.
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>An editor pane uses specialized editor kits to read, write,
display, and edit text of different formats. The Swing text 
package includes editor kits for plain text, HTML, and RTF. 
You can also develop custom editor kits for other formats. 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;JavaScript&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/js/omi/jsc/s_code_remote.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 460px"><img alt="Matlab editbox initialized from an HTML webpage URL" src="http://UndocumentedMatlab.com/images/editbox6a.png" title="Matlab editbox initialized from an HTML webpage URL" width="450" height="180" /><p class="wp-caption-text">Matlab editbox initialized from an HTML webpage URL</p></div></center></p>
<h3 id="EditorKit">Setting the EditorKit and ContentType</h3>
<p>Set the requested StyledEditorKit (via <i>setEditorKit()</i>) or ContentType properties and then use <i>setText()</i> to set the text, which should be of the appropriate content type. Note that setting EditorKit or ContentType clears any existing text and left-aligns the contents (hgTextEditMultiline is center aligned by default). Also note that HTML &lt;div&gt;s get their own separate lines and that &lt;html&gt; and &lt;body&gt; opening and closing tags are accepted but unnecessary. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setEditorKit</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">text</span>.<span style="">html</span>.<span style="">HTMLEditorKit</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% alternative: jEditbox.setContentType('text/html');</span>
htmlStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;b&gt;&lt;div style=&quot;font-family:impact;color:green&quot;&gt;'</span><span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Matlab&lt;/div&gt;&lt;/b&gt; GUI is &lt;i&gt;'</span> <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;font color=&quot;red&quot;&gt;highly&lt;/font&gt;&lt;/i&gt; customizable'</span><span style="color: #080;">&#93;</span>;
jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span>htmlStr<span style="color: #080;">&#41;</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 210px"><img alt="HTML contents in a Matlab editbox" src="http://UndocumentedMatlab.com/images/editbox6b.png" title="HTML contents in a Matlab editbox" width="200" height="50" /><p class="wp-caption-text">HTML contents in a Matlab editbox</p></div></center></p>
<p>Let&#8217;s show another usage example, of an event log file, spiced with icons and colored text based on event severity. First, define the logging utility function (the icon filenames may need to be changed based on your Matlab release):</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> logMessage<span style="color: #080;">&#40;</span>jEditbox,text,severity<span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Ensure we have an HTML-ready editbox</span>
   HTMLclassname = <span style="color:#A020F0;">'javax.swing.text.html.HTMLEditorKit'</span>;
   <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isa</span><span style="color: #080;">&#40;</span>jEditbox.<span style="">getEditorKit</span>,HTMLclassname<span style="color: #080;">&#41;</span>
      jEditbox.<span style="">setContentType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% Parse the severity and prepare the HTML message segment</span>
   <span style="color: #0000FF;">if</span> nargin&lt;<span style="color: #33f;">3</span>,  severity=<span style="color:#A020F0;">'info'</span>;  <span style="color: #0000FF;">end</span>
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">lower</span><span style="color: #080;">&#40;</span>severity<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'i'</span>,  icon = <span style="color:#A020F0;">'greenarrowicon.gif'</span>; color=<span style="color:#A020F0;">'gray'</span>;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'w'</span>,  icon = <span style="color:#A020F0;">'demoicon.gif'</span>;       color=<span style="color:#A020F0;">'black'</span>;
      <span style="color: #0000FF;">otherwise</span>, icon = <span style="color:#A020F0;">'warning.gif'</span>;        color=<span style="color:#A020F0;">'red'</span>;
   <span style="color: #0000FF;">end</span>
   icon = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'toolbox/matlab/icons'</span>,icon<span style="color: #080;">&#41;</span>;
   iconTxt =<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;img src=&quot;file:///'</span>,icon,<span style="color:#A020F0;">'&quot; height=16 width=16&gt;'</span><span style="color: #080;">&#93;</span>;
   msgTxt = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&amp;nbsp;&lt;font color='</span>,color,<span style="color:#A020F0;">'&gt;'</span>,text,<span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span>;
   newText = <span style="color: #080;">&#91;</span>iconTxt,msgTxt<span style="color: #080;">&#93;</span>;
   endPosition = jEditbox.<span style="">getDocument</span>.<span style="">getLength</span>;
   <span style="color: #0000FF;">if</span> endPosition&gt;<span style="color: #33f;">0</span>, newText=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;br/&gt;'</span> newText<span style="color: #080;">&#93;</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% Place the HTML message segment at the bottom of the editbox</span>
   currentHTML = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>jEditbox.<span style="">getText</span><span style="color: #080;">&#41;</span>;
   jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>currentHTML,<span style="color:#A020F0;">'&lt;/body&gt;'</span>,newText<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   endPosition = jEditbox.<span style="">getDocument</span>.<span style="">getLength</span>;
   jEditbox.<span style="">setCaretPosition</span><span style="color: #080;">&#40;</span>endPosition<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% end of content</span>
<span style="color: #0000FF;">end</span></pre></div></div>

<p>Now, let&#8217;s use this logging utility function to log some messages:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a regular info message...'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a warning message...'</span>, <span style="color:#A020F0;">'warn'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'an error message!!!'</span>, <span style="color:#A020F0;">'error'</span><span style="color: #080;">&#41;</span>;
logMessage<span style="color: #080;">&#40;</span>jEditbox, <span style="color:#A020F0;">'a regular message again...'</span>, <span style="color:#A020F0;">'info'</span><span style="color: #080;">&#41;</span>;</pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 210px"><img alt="Rich editbox contents (a log file)" src="http://UndocumentedMatlab.com/images/editbox10.png" title="Rich editbox contents (a log file)" width="200" height="60" /><p class="wp-caption-text">Rich editbox contents (a log file)</p></div></center></p>
<p>HTML editboxes are normally editable, images included. In actual applications, we may wish to prevent editing the display log. To do this, simply call jEditbox.<i>setEditable(false)</i>.</p>
<p>Setting a hyperlink handler is easy: first we need to ensure that we&#8217;re using an HTML content-type document. Next, set the editbox to be uneditable (hyperlinks display correctly when the editbox is editable, but are unclickable), using jEditbox.<i>setEditable(false)</i>. Finally, set the callback function in the editbox&#8217;s HyperlinkUpdateCallback property:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">jEditbox.<span style="">setContentType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'text/html'</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link: &lt;a href= &quot;http://UndocumentedMatlab.com&quot;&gt;UndocumentedMatlab.com&lt;/a&gt;'</span><span style="color: #080;">&#41;</span>;
jEditbox.<span style="">setEditable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
hjEditbox = handle<span style="color: #080;">&#40;</span>jEditbox,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hjEditbox,<span style="color:#A020F0;">'HyperlinkUpdateCallback'</span>,@linkCallbackFcn<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">function</span> linkCallbackFcn<span style="color: #080;">&#40;</span>src,eventData<span style="color: #080;">&#41;</span>
   url = eventData.<span style="">getURL</span>;      <span style="color: #228B22;">% a java.net.URL object</span>
   description = eventData.<span style="">getDescription</span>; <span style="color: #228B22;">% URL string</span>
   jEditbox = eventData.<span style="">getSource</span>;
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">ENTERED</span><span style="color: #080;">&#41;</span>
               <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link hover enter'</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">EXITED</span><span style="color: #080;">&#41;</span>
               <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'link hover exit'</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">case</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>eventData.<span style="">getEventType</span>.<span style="">ACTIVATED</span><span style="color: #080;">&#41;</span>
               jEditbox.<span style="">setPage</span><span style="color: #080;">&#40;</span>url<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 230px"><img alt="Hyperlink in editbox" src="http://UndocumentedMatlab.com/images/editbox12.png" title="Hyperlink in editbox" width="220" height="60" /><p class="wp-caption-text">Hyperlink in editbox</p></div></center></p>
<h3 id="styles">Setting the style runs programmatically</h3>
<p>Setting the styles programmatically, one style run after another, can be done via the text-pane&#8217;s Document property object. Individual character ranges can be set using the Document&#8217;s <i>setCharacterAttributes</i> method,  or entire style runs can be inserted via <i>insertString</i>. Attributes are updated using the static methods available in javax.swing.text.StyleConstants. These methods include setting character attributes (font/size/bold/italic/strike-through/underline/subscript/superscript and foreground/background colors), paragraph attributes (indentation/spacing/tab-stops/bidi), image icons and any Swing Component (buttons etc.). Here is the end result:<br />
<center><div class="wp-caption aligncenter" style="width: 460px"><img alt="Rich editbox contents: images, controls &#038; font styles" src="http://UndocumentedMatlab.com/images/editbox6c.png" title="Rich editbox contents: images, controls &#038; font styles" width="450" height="250" /><p class="wp-caption-text">Rich editbox contents: images, controls &#038; font styles</p></div></center></p>
<p>Note that if a styled multi-line editbox is converted to a single-line editbox (by setting hEditbox&#8217;s Max property to 1), it loses all style information, embedded images and components. Returning to multi-line mode will therefore show only the plain-text.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/' rel='bookmark' title='Permanent Link: Customizing listbox &#038; editbox scrollbars'>Customizing listbox &#038; editbox scrollbars</a> <small>Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Permanent Link: Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: 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/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/feed/</wfw:commentRss>
		</item>
		<item>
		<title>FindJObj GUI - display container hierarchy</title>
		<link>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/</link>
		<comments>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 13:47:18 +0000</pubDate>
		<dc:creator>Yair Altman</dc:creator>
		
		<category><![CDATA[Figure window]]></category>

		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Handle graphics]]></category>

		<category><![CDATA[High risk of breaking in future versions]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[UI controls]]></category>

		<category><![CDATA[Undocumented feature]]></category>

		<category><![CDATA[FindJObj]]></category>

		<category><![CDATA[JIDE]]></category>

		<category><![CDATA[uicontrol]]></category>

		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=908</guid>
		<description><![CDATA[The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks.

 
<pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='Permanent Link: cprintf - display formatted color text in the Command Window'>cprintf - display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Permanent Link: Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I explained how the <b><i>findjobj</i></b> utility can be used to access a Matlab component&#8217;s underlying Java component. <b><i>Findjobj</i></b> has another role: displaying the component hierarchy of complex Matlab containers such as the <a target="_blank" href="http://undocumentedmatlab.com/blog/figure-toolbar-customizations/">figure window</a>, <a target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/">GUIDE</a> or the <a target="_blank" href="http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/">Editor</a>.</p>
<p>When <b><i>findjobj</i></b> is called with no output arguments, the function infers that the user requests to see the GUI version, rather than to get the control&#8217;s Java handle:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; findjobj<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: findjobj(gcf)</span></pre></div></div>

<p><center><div class="wp-caption aligncenter" style="width: 460px"><a href="http://UndocumentedMatlab.com/images/findjobj9.png" target="_blank"><img alt="FindJObj GUI (click to zoom)" src="http://UndocumentedMatlab.com/images/findjobj9.png" title="FindJObj GUI (click to zoom)" width="450" height="296" /></a><p class="wp-caption-text">FindJObj GUI (click to zoom)</p></div></center></p>
<p>There are several note-worthy aspects in this graphical hierarchy presentation:</p>
<p>The hierarchy tree itself is displayed using the internal com.mathworks.hg.peer.UITreePeer Java object. This is the object that underlies the semi-documented <b><i>uitree</i></b> function. The hierarchy sub-components are presented as tree nodes, each having a separate icon based on the component type. In some cases (toolbar buttons for example), the component&#8217;s icon image is used for its corresponding tree node. A javax.swing.JProgressBar is presented while the tree is being populated, an action that can take a few seconds depending on the target figure&#8217;s complexity. Some tree branches which are normally uninteresting are automatically collapsed: hidden containers (these are also grayed-out), menubars, toolbars and scrollbars. In parallel to the Java container hierarchy, a separate tree branch is presented with the corresponding Matlab (Handle-Graphics, or HG) hierarchy.</p>
<p><center><div class="wp-caption aligncenter" style="width: 430px"><img alt="Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container" src="http://undocumentedmatlab.com/images/statusbar6d2.png" title="Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container" width="420" height="509" /><p class="wp-caption-text">Another GUI example - note the hidden (gray) items, the HG tree branch and the auto-collapsed MJToolBar container</p></div></center></p>
<p>Each node item gets a unique tooltip (see top screenshot above). Similarly, a unique context-menu (right-click menu) is attached to each node item with actions that are relevant for that node:</p>
<p><center><div class="wp-caption aligncenter" style="width: 321px"><img alt="Item-specific context-menu" src="http://UndocumentedMatlab.com/images/findjobj_contextmenu.png" title="Item-specific context-menu" width="311" height="171" /><p class="wp-caption-text">Item-specific context-menu</p></div></center></p>
<p>Finally, a node-selection callback is attached to the tree, that will flash a red border around the GUI control when its corresponding Java node-item is clicked/selected:</p>
<p><center><div class="wp-caption aligncenter" style="width: 460px"><a href="http://undocumentedmatlab.com/images/findjobj.png" target="_blank"><img alt="FindJObj - flashing red border around a toolbar icon" src="http://undocumentedmatlab.com/images/findjobj.png" title="FindJObj - flashing red border around a toolbar icon" width="450" height="405" /></a><p class="wp-caption-text">FindJObj - flashing red border around a toolbar icon</p></div></center></p>
<p>Once the tree was done, I set out to display and enable modifications of component properties and callbacks in separate adjacent panels. I used the internal com.mathworks.mlwidgets.inspector.PropertyView component to display the properties (this is the JIDE component that underlies the built-in <b><i>inspect</i></b> function). To prevent a JIDE run-time alert, I called com.mathworks.mwswing.MJUtilities.initJIDE. A label is added to the table&#8217;s header, displaying the currently selected sub-component&#8217;s class (e.g., &#8220;javax.swing.JButton&#8221;), and a tooltip with a color-coded list of all the control&#8217;s properties.</p>
<p>The callbacks table was implemented using com.jidesoft.grid.TreeTable to enable easy column resizing, but this is otherwise used as a simple data table. A checkbox was added to filter out the 30-odd standard <a target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">Swing callbacks</a>, which are non-unique to the selected sub-component (tree node). All the panels - tree, properties and callbacks - are then placed in resizable javax.swing.JSplitPanes and presented to the user.</p>
<p>I have omitted mention of some other undocumented features in <b><i>findjobj</i></b>. After all, space here is limited and the function is over 2500 lines long. I encourage you to <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">download the utility</a> and explore the code, and I gladly welcome your feedback.</p>


 <p><pre> </pre>Related posts:<ol><li><a href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='Permanent Link: FindJObj - find a Matlab component&#8217;s underlying Java object'>FindJObj - find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='Permanent Link: cprintf - display formatted color text in the Command Window'>cprintf - display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Permanent Link: Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Permanent Link: Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded>
			<wfw:commentRss>http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
