<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>uitree &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/uitree/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Tue, 06 Aug 2013 07:10:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>treeTable</title>
		<link>https://undocumentedmatlab.com/articles/treetable?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=treetable</link>
					<comments>https://undocumentedmatlab.com/articles/treetable#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 06 Aug 2013 07:10:20 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[JIDE]]></category>
		<category><![CDATA[uitable]]></category>
		<category><![CDATA[uitree]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4062</guid>

					<description><![CDATA[<p>A description of a sortable, groupable tree-table control that can be used in Matlab is provided. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/treetable">treeTable</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/uitable-cell-colors" rel="bookmark" title="Uitable cell colors">Uitable cell colors </a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/multi-line-uitable-column-headers" rel="bookmark" title="Multi-line uitable column headers">Multi-line uitable column headers </a> <small>Matlab uitables can present long column headers in multiple lines, for improved readability. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/checkboxlist" rel="bookmark" title="CheckboxList">CheckboxList </a> <small>Matlab listboxes can be enhanced with checkboxes next to each item. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jide-property-grids" rel="bookmark" title="JIDE Property Grids">JIDE Property Grids </a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Since Matlab 7.0 (R14), Matlab has included a built-in GUI table control (<i><b>uitable</b></i>), at first as a <a target="_blank" href="/articles/legend-semi-documented-feature/#Semi-documented">semi-documented</a> function and in release 7.6 (R2008a) as a <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/ref/uitable.html">fully-documented</a> function. Useful as this control is, it lacks many features that are expected in modern GUIs, including sorting, filtering, cell-specific appearance and behavior, gridline customization etc. In <a target="_blank" href="/articles/tag/uitable/">past articles</a> I have explained how <i><b>uitable</b></i> can be customized to achieve a more professional-looking table. I expanded on this in my <a target="_blank" href="/matlab-java-book/">book</a> and my detailed <a target="_blank" href="/articles/uitable-customization-report/"><i><b>uitable</b></i> customization report</a>.<br />
Today I explain how a grouping hierarchy can be achieved in a table control that can be used in Matlab GUI. Such a control, which is a combination of a <a target="_blank" href="/articles/uitree/"><i><b>uitree</b></i></a> and <i><b>uitable</b></i>, is typically called a <i>tree-table</i>. We can find numerous examples of it in everyday usage. I have encapsulated the functionality in a utility called <i><b>treeTable</b></i> on the Matlab File Exchange (<a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/42946-treetable-create-a-sortable-table-control-with-collapsible-row-groups">#42946</a>). The utility is provided with full source code and open-source license, and readers are welcome to use and modify it. A detailed explanation of the technicalities follows below, but if you&#8217;re just interested in having a sortable, rearrangeable, customizable, groupable table control, then all you need to do is download and use the utility.<br />
<figure style="width: 407px" class="wp-caption alignright"><img fetchpriority="high" decoding="async" alt="treeTable utility" src="https://undocumentedmatlab.com/images/treeTable.jpg" title="treeTable utility" width="407" height="364" /><figcaption class="wp-caption-text">treeTable utility</figcaption></figure></p>
<pre lang='matlab'>
headers = {'ID','Label','Logical1','Logical2','Selector','Numeric'};
data = {1,'M11',true, false,'One', 1011;  ...
        1,'M12',true, true, 'Two', 12;   ...
        1,'M13',false,false,'Many',13.4; ...
        2,'M21',true, false,'One', 21;  ...
        2,'M22',true, true, 'Two', 22;   ...
        3,'M31',true, true, 'Many',31;   ...
        3,'M32',false,true, 'One', -32;  ...
        3,'M33',false,false,'Two', 33; ...
        3,'M34',true, true, 'Many',34;  ...
};
selector = {'One','Two','Many'};
colTypes = {'label','label','char','logical',selector,'double'};
colEditable = {true, true, true, true, true}
icons = {fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif'), ...
         fullfile(matlabroot,'/toolbox/matlab/icons/file_open.png'), ...
         fullfile(matlabroot,'/toolbox/matlab/icons/foldericon.gif'), ...
}
% Create the table in the current figure
jtable = treeTable('Container',gcf, 'Headers',headers, 'Data',data, ...
                   'ColumnTypes',colTypes, 'ColumnEditable',colEditable, ...
                   'IconFilenames',icons, 'Groupable',true, 'InteractiveGrouping',false);
% Collapse Row #6, sort descending column #5 (both are 0-based)
jtable.expandRow(5,false);  % 5=row #6; false=collapse
jtable.sortColumn(4,true,false);  % 4=column #5; true=primary; false=descending
</pre>
<p><span id="more-4062"></span></p>
<h3 id="implementation">The basic implementation concept</h3>
<p>Unfortunately, <i><b>uitable</b></i> as-is cannot be customized to have groupable rows. It derives from JIDE&#8217;s <code>SortableTable</code>, rather than one of its groupable derived classes. On the other hand, <i><b>uitree</b></i> (<i>don&#8217;t you agree that after a decade of this so-useful function being semi-documented it ought to be made official?</i>) uses a different class hierarchy outside <code>com.jidesoft.grid</code>, and so it cannot be easily customized to display rows (as opposed to simple labels).<br />
These limitations mean that we cannot use <i><b>uitable</b></i> or <i><b>uitree</b></i> and need to use a custom component. Luckily, such a component is available in all Matlab installations, on all platforms. It is part of the grid components package, on which Matlab GUI has relied for many years, by <a target="_blank" rel="nofollow" href="http://www.jidesoft.com">JIDE Software</a>. JIDE Grids is a collection of components that extend the standard Java Swing <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> component, and is included in each Matlab installation (<i>/java/jarext/jide/jide-grids.jar</i> under the Matlab root). I discussed <a target="_blank" href="/articles/tag/jide/">multiple JIDE controls</a> in this blog over the years. You can find further details on JIDE Grids in the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf">Developer Guide</a> and the <a target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/">Javadoc documentation</a>.<br />
In fact, there are no less than three different components that we could use in our case: <code>TreeTable</code>, <code>GroupTable</code> and <code>HierarchicalTable</code>:<br />
<center><figure style="width: 417px" class="wp-caption aligncenter"><img decoding="async" alt="JIDE Grids class hierarchy (we normally use only one of the marked classes)" src="https://undocumentedmatlab.com/images/JIDE_Grids.png" title="JIDE Grids class hierarchy (we normally use only one of the marked classes)" width="417" height="233"/><figcaption class="wp-caption-text">JIDE Grids class hierarchy (we normally use only one of the marked classes)</figcaption></figure></center><br />
Note that we cannot use <code>PropertyTable</code> since that component is limited to only two columns. This is perfect for presenting property names and values, which is the reason it is used by Matlab&#8217;s <i><b>inspect</b></i> function and my generic <a target="_blank" href="/articles/propertiesgui/"><i><b>propertiesGUI</b></i> utility</a> or Levente Hunyadi&#8217;s <a target="_blank" href="/articles/jide-property-grids/">Property Grid utility</a>. But in this case we wish to display a general multi-column table, so <code>PropertyTable</code> is a no-go.<br />
<code>TreeTable</code> and <code>GroupTable</code> enable data rows that have similar type (class), whereas <code>HierarchicalTable</code> enables more flexibility, by allowing display of any component type (including full tables) in child rows. This flexibility comes with a price that customizing a <code>HierarchicalTable</code> is more difficult than <code>TreeTable</code> or <code>GroupTable</code>. These latter two components are quite similar; we use <code>GroupTable</code>, which extends <code>TreeTable</code> with the ability to automatically group rows that have the same value in a certain column.</p>
<h3 id="model">Data model</h3>
<p>The above-mentioned table classes all derive from <code>SortableTable</code> (which also underlies <i><b>uitable</b></i>). Unfortunately, something in the Matlab-Java interface breaks the ability of the JIDE table classes (or rather, their data model) to understand numeric values for what they are. As a result, sorting columns is done lexically, i.e. &#8220;123&#8221; &lt; &#8220;34&#8221; &lt; &#8220;9&#8221;. To fix this, I&#8217;ve included a custom Java table model (<code>MultiClassTableModel</code>) with the <i><b>treeTable</b></i> utility, which automatically infers the column type (class) based on the value in the top row (by overriding the <i>getColumnClass()</i> method).<br />
Using this new class is pretty easy:</p>
<pre lang='matlab'>
% Create the basic data-type-aware model using our data and headers
javaaddpath(fileparts(mfilename('fullpath')));  % add the Java class file to the dynamic java class-path
model = MultiClassTableModel(data, headers);  % data=2D cell or numeric array; headers=cell array of strings
% Wrap the standard model in a JIDE GroupTableModel
com.mathworks.mwswing.MJUtilities.initJIDE;  % initialize JIDE
model = com.jidesoft.grid.DefaultGroupTableModel(model);
model.addGroupColumn(0);  % make the first column the grouping column
model.groupAndRefresh;  % update the model data
% Use the generated model as the data-model of a JIDE GroupTable
jtable = eval('com.jidesoft.grid.GroupTable(model);');  % prevent JIDE alert by run-time (not load-time) evaluation
jtable = handle(javaObjectEDT(jtable), 'CallbackProperties');  % ensure that we're using EDT
% Enable multi-column sorting
jtable.setSortable(true);
% Present the tree-table within a scrollable viewport on-screen
scroll = javaObjectEDT(JScrollPane(jtable));
[jhscroll,hContainer] = javacomponent(scroll, tablePosition, hParent);
set(hContainer,'units', 'normalized','pos',[0,0,1,1]);  % this will resize the table whenever its container is resized
</pre>
<p>Here, <em>com.mathworks.mwswing.MJUtilities.initJIDE</em> is called to initialize JIDE&#8217;s usage within Matlab. Without this call, we may see a JIDE warning message in some Matlab releases. We only need to <em>initJIDE</em> once per Matlab session, although there is no harm in repeated calls.<br />
<i><b>javacomponent</b></i> is the undocumented built-in Matlab function that adds Java Swing components to a Matlab figure, using the given dimensions and parent handle. I discussed it <a target="_blank" href="/articles/javacomponent/">here</a>.</p>
<h3 id="callbacks">Callbacks</h3>
<p>There are two main callbacks that can be used with <i><b>treeTable</b></i>:</p>
<ul>
<li>table data update &#8211; this can be set by uncommenting line #237 of <i>treeTable.m</i>:
<pre lang='matlab'>set(handle(getOriginalModel(jtable),'CallbackProperties'), 'TableChangedCallback', {@tableChangedCallback, jtable});</pre>
<p>which activates the sample <code>tableChangedCallback()</code> function (lines #684-694). Naturally, you can also set your own custom callback function.</p>
<pre lang='matlab'>
% Sample table-editing callback
function tableChangedCallback(hModel,hEvent,jtable)
    % Get the modification data
    modifiedRow = get(hEvent,'FirstRow');
    modifiedCol = get(hEvent,'Column');
    label   = hModel.getValueAt(modifiedRow,1);
    newData = hModel.getValueAt(modifiedRow,modifiedCol);
    % Now do something useful with this info
    fprintf('Modified cell %d,%d (%s) to: %s\n', modifiedRow+1, modifiedCol+1, char(label), num2str(newData));
end  % tableChangedCallback
</pre>
</li>
<li>row selection update &#8211; this is currently enabled in line #238 of <i>treeTable.m</i>:
<pre lang='matlab'>set(handle(jtable.getSelectionModel,'CallbackProperties'), 'ValueChangedCallback', {@selectionCallback, jtable});</pre>
<p>which activates the sample <code>selectionCallback()</code> function (lines #696-705). Naturally, you can also set your own custom callback function.</p>
<pre lang='matlab'>
% Sample table-selection callback
function selectionCallback(hSelectionModel,hEvent,jtable)
    % Get the selection data
    MinSelectionIndex  = get(hSelectionModel,'MinSelectionIndex');
    MaxSelectionIndex  = get(hSelectionModel,'MaxSelectionIndex');
    LeadSelectionIndex = get(hSelectionModel,'LeadSelectionIndex');
    % Now do something useful with this info
    fprintf('Selected rows #%d-%d\n', MinSelectionIndex+1, MaxSelectionIndex+1);
end  % selectionCallback
</pre>
</li>
</ul>
<h3 id="features">Some useful features of <i>treeTable</i></h3>
<p><center><figure style="width: 342px" class="wp-caption alignright"><img decoding="async" alt="treeTable with InteractiveGrouping, multi-column sorting, column rearranging" src="https://undocumentedmatlab.com/images/treeTable_InteractiveGrouping.jpg" title="treeTable with InteractiveGrouping, multi-column sorting, column rearranging" width="342" height="366" /><figcaption class="wp-caption-text">treeTable with interactive grouping, multi-column sorting, column rearranging</figcaption></figure></center></p>
<ul>
<li>Sortable columns (including numeric columns)</li>
<li>Rearrangeable columns (drag headers left/right)</li>
<li>Auto-fit the table columns into the specified container width</li>
<li>Manually resize columns by dragging the column divider gridlines (not just the header dividers as in <i><b>uitable</b></i>)</li>
<li>Flat or groupable table, based on the specified <b>Groupable</b> parameter (default=true)</li>
<li>Ability to interactively group columns (just like Microsoft Outlook) using the <b>InteractiveGrouping</b> parameter (default=false)</li>
<li>Selector cells only show the drop-down arrow of the currently-selected cell (unlike <i><b>uitable</b></i> which shows it for all the column cells)</li>
<li>Selector cells enable editing, unlike <i><b>uitable</b></i> that only enables selecting among pre-defined values</li>
<li>Ability to attach user-defined icons for the leaf rows and the grouping rows (expanded/collapsed)</li>
<li>Easily attach custom cell editors or renderers to any table column (see my <a target="_blank" href="/matlab-java-book/">book</a> and my detailed <a target="_blank" href="/articles/uitable-customization-report/"><i><b>uitable</b></i> customization report</a> for details)</li>
</ul>
<p>All of these features can be turned on/off using the control&#8217;s properties. Again, see my book or report for details (or <a target="_blank" href="/consulting/">ask me</a> to do it for you&#8230;).<br />
I remind readers that I will be visiting clients in Austria (Vienna, Salzburg) and Switzerland (Zurich) in August 18-29. If you live in the area, I will be happy to meet you to discuss how I could bring value to your needs, as consultant, contractor or trainer (<a target="_blank" href="/articles/sparse-data-math-info/#visit">more details</a>).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/treetable">treeTable</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/uitable-cell-colors" rel="bookmark" title="Uitable cell colors">Uitable cell colors </a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/multi-line-uitable-column-headers" rel="bookmark" title="Multi-line uitable column headers">Multi-line uitable column headers </a> <small>Matlab uitables can present long column headers in multiple lines, for improved readability. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/checkboxlist" rel="bookmark" title="CheckboxList">CheckboxList </a> <small>Matlab listboxes can be enhanced with checkboxes next to each item. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jide-property-grids" rel="bookmark" title="JIDE Property Grids">JIDE Property Grids </a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/treetable/feed</wfw:commentRss>
			<slash:comments>90</slash:comments>
		
		
			</item>
	</channel>
</rss>
