<?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>Karthik Ponudurai &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/karthik-ponudurai/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 10 Jul 2014 16:21:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>Using pure Java GUI in deployed Matlab apps</title>
		<link>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-pure-java-gui-in-deployed-matlab-apps</link>
					<comments>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 10 Jul 2014 16:21:09 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Karthik Ponudurai]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4909</guid>

					<description><![CDATA[<p>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps">Using pure Java GUI in deployed Matlab apps</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/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to welcome repeat guest blogger Karthik Ponudurai, who has previously written here about the <a target="_blank" href="/articles/jtattoo-look-and-feel-demo">JTattoo look-and-feel</a>, and about <a target="_blank" href="/articles/matlab-java-interface-using-static-control">integration of Java controls&#8217; events with Matlab callbacks</a>. Today, Karthik discusses a problem when we wish to use pure-Java GUIs in deployed Matlab programs, and the undocumented simple solution.<br />
<b><u>Reminder</u></b>: I will be visiting several US cities (Minneapolis, Boston and New York) in July 22-31 (<a target="_blank" href="/articles/usa-visit-july-2014">details</a>). Let me know if you&#8217;d like to meet me there.</i></p>
<h3 id="introduction">Introduction</h3>
<p>Using a pure-Java Swing-based Graphical User Interface (GUI) has several important advantages compared to using a pure-Matlab GUI:</p>
<ul>
<li>Java GUI widget toolkit provides a large collection of components (scrollbar, slider, etc&#8230;) and layouts (card, spring, etc&#8230;)</li>
<li>Multiple event handling options</li>
<li>Mature third-party Java Swing IDEs for quick and easy GUI development (Netbeans, Eclipse, etc&#8230;)</li>
<li>Supports pluggable look and feel that allows applications to have a look and feel that is unrelated to the underlying platform.</li>
<li>Java Swing&#8217;s window icon can be modified, whereas Mathworks apparently places an [<i>entirely unreasonable</i>] license restriction on modifying the Matlab figure window icon.</li>
</ul>
<p>When Matlab is in development (non-deployed) mode, we can load and display a Java <code>JFrame</code> using the following Matlab code:</p>
<pre lang='matlab'>
% Add Java library to dynamic Java classpath
javaaddpath([pwd '\ExampleWindow.jar']);
% Get example Java window from the library
jFrame = examplewindow.JavaWindow();
% Get Java buttons
% Note: see https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a
plotMeshButton = handle(jFrame.getPlotMeshButton(),    'CallbackProperties');
showWarnButton = handle(jFrame.getShowWarnDlgButton(), 'CallbackProperties');
% Set Java button callbacks
set(plotMeshButton, 'ActionPerformedCallback', @myPlotMeshCallback);
set(showWarnButton, 'ActionPerformedCallback', @myShowWarnCallback);
% Display the Java window
jFrame.setVisible(true);
</pre>
<p><center><a target="_blank" href="/images/JFrame1a.gif"><img decoding="async" alt="Java JFrame created in Matlab (click for full-size image)" src="https://undocumentedmatlab.com/images/JFrame1a.gif" title="Java JFrame created in Matlab (click for full-size image)" width="100%" style="max-width: 407px;" /></a><br />
Java JFrame created in Matlab (click for full-size image)</center></p>
<h3 id="problem">The problem</h3>
<p>All this works great when ran within the Matlab environment. However, when the Matlab source code file (.m) is compiled as a Windows standalone executable application, when running the resulting executable the Java window appears for a short period and then terminates (exits). Without a visible Matlab figure, the compiled Matlab application does not retain the Java window open. The JFrame flashes on for a split-second, and then vanishes.<br />
<span id="more-4909"></span></p>
<h3 id="solution">The solution</h3>
<p>To fix this problem, it turns out that we only need to add the Matlab <i><b>waitfor</b></i> command after making the Java window visible:</p>
<pre lang='matlab'>
% (earlier code as above)
% Display the Java window
jFrame.setVisible(true);
if isdeployed
    waitfor(jFrame);
end
</pre>
<p><center><a target="_blank" href="/images/JFrame2a.gif"><img decoding="async" alt="Java JFrame in deployed Matlab (click for full-size image)" src="https://undocumentedmatlab.com/images/JFrame2a.gif" title="Java JFrame in deployed Matlab (click for full-size image)" width="100%" style="max-width: 982px;" /></a><br />
Java JFrame in deployed Matlab (click for full-size image)</center><br />
The <i><b>waitfor</b></i> command holds the Java window open, but still enables full interactivity with the window: When a control event is triggered, the specified callback is executed, and then Matlab returns to the <i><b>waitfor</b></i>.<br />
A zip file containing the Matlab source-code and its resulting executable can be <a target="_blank" href="/files/JFrame_example_window.zip">downloaded from here</a>. You might get a security notification when downloading the zip file, due to the contained exe file. Also note that to run the executable, you need to install Matlab MCR R2012b (8.0) or later, otherwise you will get a run-time error.<br />
Finally, note that closing the Java GUI exits the JVM and by extension the entire containing program (Matlab). Naturally, this has no impact in deployed mode, but to prevent it from exiting the Matlab environment in non-deployed mode, don&#8217;t close the Java window by clicking its &#8220;X&#8221; button etc., but rather by calling <code>jFrame.dispose();</code> in your Matlab code or the Matlab Desktop&#8217;s Command Window.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps">Using pure Java GUI in deployed Matlab apps</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/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps/feed</wfw:commentRss>
			<slash:comments>27</slash:comments>
		
		
			</item>
		<item>
		<title>JTattoo look-and-feel demo</title>
		<link>https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jtattoo-look-and-feel-demo</link>
					<comments>https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 20 Mar 2013 16:00:11 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Karthik Ponudurai]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3687</guid>

					<description><![CDATA[<p>A demo GUI that shows the effects of using different look-and-feels, including the JTatoo library, is presented. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo">JTattoo look-and-feel demo</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/modifying-matlab-look-and-feel" rel="bookmark" title="Modifying Matlab&#039;s Look-and-Feel">Modifying Matlab&#039;s Look-and-Feel </a> <small>Matlab's entire Look-and-Feel (PLAF, or L&F) can be modified at the control or application level - this article shows how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/real-time-trading-system-demo" rel="bookmark" title="Real-time trading system demo">Real-time trading system demo </a> <small>A real-time Matlab-based end-to-end trading system demo is presented ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Three years ago, I wrote an article about modifying <a target="_blank" href="/articles/modifying-matlab-look-and-feel/">Matlab&#8217;s look-and-feel</a> (L&#038;F, or LnF), using Java&#8217;s built-in support for replaceable LnFs. To date, that article has 27 comments by 13 different commenters (plus 20 responses by me), making it the second most active article on this website. I decided to follow up on that article with a demo that shows the effects that different L&#038;Fs have on GUI controls, and a demonstration of the JTattoo library of professional L&#038;Fs.<br />
Today&#8217;s article and the demo are based on original work by <a target="_blank" href="/articles/tag/karthik-ponudurai/">Karthik Ponudurai</a>, who has written a guest article here about an interesting <a target="_blank" href="/articles/matlab-java-interface-using-static-control/">technique to interface a Java GUI to a Matlab application</a>, a couple of years ago.<br />
<center><figure style="width: 500px" class="wp-caption aligncenter"><a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/40866-jtattoo-look-and-feel-demo-gui"><img fetchpriority="high" decoding="async" alt="Demo of using different Look-and-Feels in Matlab GUI" src="https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/40866/versions/2/screenshot.gif" title="Demo of using different Look-and-Feels in Matlab GUI" width="500" height="421" /></a><figcaption class="wp-caption-text">Demo of using different Look-and-Feels in Matlab GUI</figcaption></figure></center><br />
<!-- My local image version: https://undocumentedmatlab.com/images/JTattooDemo_animated.gif --><br />
<span id="more-3687"></span><br />
<a target="_blank" rel="nofollow" href="http://www.jtattoo.net/">JTattoo</a> is a 3rd-party open-source library. The purpose of including it in the demo, in addition to its natural use as a professional set of L&#038;Fs, is to demonstrate how easy it is to integrate 3rd-party L&#038;Fs in Matlab. In the demo I use the current latest <a target="_blank" rel="nofollow" href="http://www.jtattoo.net/Download.html">available</a> JTattoo library (1.6.7), but you can always download the latest version and replace the <i>JTattoo.jar</i> file. JTattoo contains a large set of different L&#038;Fs that can be used independently (<a target="_blank" rel="nofollow" href="http://www.jtattoo.net/ScreenShots.html">screenshots</a>). The nice thing about L&#038;Fs is that since all Matlab GUI is based on Java Swing, the new L&#038;Fs automatically affect Matlab controls just like native Java ones.<br />
The demo can be <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/40866-jtattoo-look-and-feel-demo-gui">downloaded</a> from the Matlab File Exchange. After downloading, unzip it into any folder on your Matlab path and run <i>JTattooDemo.m</i>.<br />
A Matlab figure is displayed with two panels, one containing Matlab uicontrols (within a simple <a target="_blank" href="/articles/matlab-layout-managers-uicontainer-and-relatives/"><i><b>uiflowcontainer</b></i></a>) and the other containing Java components.<br />
Two main menus are available: <i>System</i> enables selecting the standard Swing L&#038;Fs that are installed on your system (this varies a bit between platforms and Matlab releases); <i>JTattoo</i> enables selecting one of the JTattoo L&#038;Fs. Once the user selects any of the L&#038;F menu items, the entire figure is updated. This is done by calling <code>javax.swing.SwingUtilities.updateComponentTreeUI()</code> on the figure&#8217;s <a target="_blank" href="/articles/minimize-maximize-figure-window/#JavaFrame">Java Frame</a>&#8216;s content pane. Both the Matlab and the Java controls within the figure are automatically updated by this Swing function to reflect the newly-selected L&#038;F. Care is taken to update the L&#038;F on the <a target="_blank" href="/articles/matlab-and-the-event-dispatch-thread-edt/">EDT</a>, to prevent racing-condition issues.<br />
It should be noted that the demo resets the L&#038;F after updating the figure, otherwise any new figure or window would open using the newly-selected L&#038;F. This is done in the <i>updateInterface</i> function as follows:</p>
<pre lang='matlab'>
function updateInterface( lookandfeel )
    % Preserve the original L&F, before updating
    originalLnF = javax.swing.UIManager.getLookAndFeel;
    % Update the L&F in the demo figure as requested
    ... (all the existing code within the function)
    % Restore the original L&F for any new figure/window
    javax.swing.UIManager.setLookAndFeel(originalLnF);
end  % updateInterface
</pre>
<p>Note that after changing the L&#038;Fs several times, some L&#038;F properties night get &#8220;mixed-up&#8221; causing odd-looking L&#038;Fs. The simplest solution in this case is to restart Matlab&#8230;<br />
<center><figure style="width: 350px" class="wp-caption alignright"><a target="_blank" rel="nofollow" href="http://www.johnbryce.co.il/content/%D7%99%D7%95%D7%9D-%D7%A4%D7%AA%D7%95%D7%97-%D7%90%D7%9C%D7%A7%D7%98%D7%A8%D7%95%D7%A0%D7%99%D7%A7%D7%94/?source=divur_martze"><img decoding="async" alt="Matlab open training day (Israel) - click for details" src="https://undocumentedmatlab.com/courses/Matlab_open_day_350x225b.png" title="Matlab open training day (Israel) - click for details" width="350" height="225"/></a><figcaption class="wp-caption-text">Matlab open training day (Israel) - click for details</figcaption></figure></center><br />
Readers in Israel are invited to attend a free training seminar that I will present on advanced Matlab topics in Herzliya, on Thursday April 4, 2013. The seminar is free, but requires registration. <a target="_blank" rel="nofollow" href="http://www.johnbryce.co.il/content/%D7%99%D7%95%D7%9D-%D7%A4%D7%AA%D7%95%D7%97-%D7%90%D7%9C%D7%A7%D7%98%D7%A8%D7%95%D7%A0%D7%99%D7%A7%D7%94/?source=divur_martze">Additional details here</a>. I will speak in Hebrew, but the presentation will be in English and I will be happy to answer questions in English.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo">JTattoo look-and-feel demo</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/modifying-matlab-look-and-feel" rel="bookmark" title="Modifying Matlab&#039;s Look-and-Feel">Modifying Matlab&#039;s Look-and-Feel </a> <small>Matlab's entire Look-and-Feel (PLAF, or L&F) can be modified at the control or application level - this article shows how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/real-time-trading-system-demo" rel="bookmark" title="Real-time trading system demo">Real-time trading system demo </a> <small>A real-time Matlab-based end-to-end trading system demo is presented ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo/feed</wfw:commentRss>
			<slash:comments>13</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab-Java interface using a static control</title>
		<link>https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-java-interface-using-static-control</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 13 Oct 2010 18:15:43 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Karthik Ponudurai]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1917</guid>

					<description><![CDATA[<p>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control">Matlab-Java interface using a static control</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/jmi-java-to-matlab-interface" rel="bookmark" title="JMI &#8211; Java-to-Matlab Interface">JMI &#8211; Java-to-Matlab Interface </a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/static-java-classpath-hacks" rel="bookmark" title="Static Java classpath hacks">Static Java classpath hacks </a> <small>Several hacks that facilitate using the static Java classpath in Matlab are explained. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matclipse-eclipse-matlab-interface" rel="bookmark" title="Matclipse &#8211; Eclipse-Matlab interface">Matclipse &#8211; Eclipse-Matlab interface </a> <small>Matclipse is an open-source plugin for the popular Eclipse IDE that connects it with Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to welcome guest blogger Karthik Ponudurai. Today, Karthik will introduce an interesting technique for interfacing Java GUI to a Matlab application.</i><br />
The current method of interfacing Java GUI controls and Matlab is by retrieving the underlying Java callback and assigning it to a Matlab callback, as shown in the following example:</p>
<pre lang="matlab">
% Create a Java button
jButton = javacomponent(javax.swing.JButton('click me'), [20,20,60,20], gcf);
% Assign a Matlab callback
hButton = handle(jButton, 'CallbackProperties');
set(hButton, 'ActionPerformedCallback', @myCallback);
% Matlab callback function
function myCallback(hObject, hEventData)
    % Insert your code here
end
</pre>
<p>This approach works fine for simple pure-Java GUI created using an IDE (for example, <a target="_blank" rel="nofollow" href="http://netbeans.org/">NetBeans</a> or <a target="_blank" rel="nofollow" href="http://eclipse.org/">Eclipse</a>) that only have a small number of controls. You can retrieve all the Java controls in Matlab and set the callbacks as shown in the above example.<br />
Unfortunately, practical GUIs often contain numerous controls. Assigning each control a different Matlab callback can be quite tedious.</p>
<h3 id="">Using a callback switchyard function</h3>
<p>A workaround to this problem can be achieved using an invisible/hidden static Java JButton. During Matlab initialization, assign a single callback for the static Java JButton:</p>
<pre lang="matlab">
% Return a reference to the GUI's static trigger button
triggerButton = statictrigger.triggerCls.getTrigger();
% Assign Matlab switchyard callback for triggerButton
buttonProps = handle(triggerButton, 'callbackproperties');
set(triggerButton, 'ActionPerformedCallback',@TriggerCallback);
</pre>
<p>When an event is triggered by any monitored control in the Java GUI, then within the Java code set the static button&#8217;s action command string to the desired Matlab callback name (a String) and invoke the static button&#8217;s <i>doClick()</i> method to trigger the Matlab TriggerCallback callback. Based on the action command string, this callback invokes the appropriate Matlab function. In this manner, Matlab&#8217;s TriggerCallback() acts as a <a target="_blank" rel="nofollow" href="http://www.mathworks.com/company/newsletters/news_notes/win00/prog_patterns.html">switchyard function</a> to the actual callback functions:</p>
<pre lang="matlab">
function TriggerCallback(hObject, hEventData)
   actionCmd = char( hObject.getActionCommand() );
   hgfeval(actionCmd, hObject, hEventData);
end
</pre>
<p>Note how the <i><b>char</b></i> function converts the Java action command (a java.lang.String object) into Matlab char array. The requested Matlab function is then dynamically invoked using Matlab&#8217;s semi-documented <a target="_blank" href="/articles/hgfeval/"><i><b>hgfeval</b></i> function</a>, which Yair will describe in another article later this month.<br />
The next few sections will look into the sample application in more detail. This application can be downloaded from <a target="_blank" href="/files/StaticButtonTrigger.zip">here</a>.</p>
<h3 id="GUI">Java GUI</h3>
<p>Firstly, let&#8217;s look into the Java GUI:<br />
<center><figure style="width: 408px" class="wp-caption aligncenter"><img decoding="async" alt="Main Java GUI" src="https://undocumentedmatlab.com/images/StaticButton_Java_Frame.png" title="Main Java GUI" width="408" height="325" /><figcaption class="wp-caption-text">Main Java GUI</figcaption></figure></center><br />
When the &#8220;Show Result in Matlab Dialog Box&#8221; button is clicked, the form information&#8217;s (Name, Age, Sex etc&#8230;) is saved in a hash map. This hash map is then serialized to a local file (C:\streamdata). Next, the static button&#8217;s action command string is set to &#8216;show&#8217; and finally its <i>doClick()</i> method is invoked.<br />
Below is the Java code corresponding to button click event:</p>
<pre lang="java">
private void showActionPerformed(java.awt.event.ActionEvent evt)
{
    // Add form values to hash map
    hMap.put(name.getName(), name.getText());
    hMap.put(age.getName(), age.getValue());
    hMap.put(sex.getName(), (String)sex.getSelectedItem());
    hMap.put(address.getName(), address.getText());
    hMap.put(email.getName(), email.getText());
    // Serialize hash map
    WriteObject(hMap);
    // Set trigger
    triggerCls.setTrigger(evt.getActionCommand());
}
</pre>
<h3 id="Matlab">Matlab callbacks</h3>
<p>The Java GUI sources are compiled and packaged in the statictrigger.jar file. The Matlab StaticButtonTriggerExample.m source file adds the Java library to the dynamic Java classpath:</p>
<pre lang="matlab">
% Add dynamic java class path
javaclasspath([pwd '\javalib\statictrigger.jar']);
</pre>
<p>The main.m function contains the code for creating the Java GUI and assigning the static button callback:</p>
<pre lang="matlab">
% create the statictrigger object
app = statictrigger.app();
% Set static trigger button listener
triggerButton = statictrigger.triggerCls.getTrigger();
buttonProps = handle(triggerButton,'callbackproperties');
set(buttonProps,'ActionPerformedCallback', {@StaticButtonTriggerCallback, app});
% Show the Java Frame
app.setVisible(1);
</pre>
<p>statictrigger.triggerCls.<i>getTrigger()</i> is a static Java public method that returns the static button object, for which we assign our callback switchyard function.<br />
The following is the StaticButtonTriggerCallback.m code, which displays the information in a Matlab message-box:<br />
<center><figure style="width: 399px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab message-box triggered by the Java GUI" src="https://undocumentedmatlab.com/images/StaticButton_Matlab_Msgbox.png" title="Matlab message-box triggered by the Java GUI" width="399" height="133" /><figcaption class="wp-caption-text">Matlab message-box triggered by the Java GUI</figcaption></figure></center></p>
<pre lang="matlab">
% Get action command string
actionCommand = src.getActionCommand();
% Read in serialized hash map
hashMap = app.ReadObject();
% Convert hash map to Matlab structure
data = Hash2Struct(hashMap);
% Display data in Matlab dialog box
msg = ['Name: ' char(hashMap.get('name')) ...
       ', Age: ' num2str(hashMap.get('age')) ...
       ', Sex: ' char(hashMap.get('sex')) ...
       ', Address: ' char(hashMap.get('address')) ...
       ', Email: ' char(hashMap.get('email')) ];
uiwait(helpdlg(msg, 'Form Details'));
</pre>
<p>You can download the example&#8217;s files from <a target="_blank" href="/files/StaticButtonTrigger.zip">here</a>. Extract the zip file, <i><b>cd</b></i> to the newly-generated StaticButtonTrigger folder, then invoke the StaticButtonTriggerExample.m function to run the example.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control">Matlab-Java interface using a static control</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/jmi-java-to-matlab-interface" rel="bookmark" title="JMI &#8211; Java-to-Matlab Interface">JMI &#8211; Java-to-Matlab Interface </a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/static-java-classpath-hacks" rel="bookmark" title="Static Java classpath hacks">Static Java classpath hacks </a> <small>Several hacks that facilitate using the static Java classpath in Matlab are explained. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matclipse-eclipse-matlab-interface" rel="bookmark" title="Matclipse &#8211; Eclipse-Matlab interface">Matclipse &#8211; Eclipse-Matlab interface </a> <small>Matclipse is an open-source plugin for the popular Eclipse IDE that connects it with Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control/feed</wfw:commentRss>
			<slash:comments>27</slash:comments>
		
		
			</item>
	</channel>
</rss>
