<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388</link>
    <title>MATLAB Central Newsreader - Profiling Java in Matlab</title>
    <description>Feed for thread: Profiling Java in Matlab</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 19 Sep 2007 07:00:51 -0400</pubDate>
      <title>Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#393020</link>
      <author>Michael </author>
      <description>I have some difficulties of profiling java source code from &lt;br&gt;
Matlab. I &amp;#8216;m creating an application which is invoked by a &lt;br&gt;
Simulink block instead of the &quot;Function Bllock Parameters&quot; &lt;br&gt;
dialog. Sometimes my application will runs in a deadlock &lt;br&gt;
and I don&amp;#8217;t know exactly were the error occurs. In addition &lt;br&gt;
I would just like to mention that I&amp;#8217;m using SWT and JFace &lt;br&gt;
libraries.&lt;br&gt;
&lt;br&gt;
I would be delighted if you send me tips for profiling java &lt;br&gt;
code from Matlab.</description>
    </item>
    <item>
      <pubDate>Tue, 09 Oct 2007 14:42:04 -0400</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#395792</link>
      <author>Wolfgang Ulmer</author>
      <description>Hi Michael,&lt;br&gt;
&lt;br&gt;
while using Swing with Java, I discovered the same problem&lt;br&gt;
which is due to the fact, that Matlab uses the AWT/Swing&lt;br&gt;
Event  Handling Thread to do some computation.&lt;br&gt;
&lt;br&gt;
You can even easily create a deadlock if you enter the&lt;br&gt;
following commands on the prompt:&lt;br&gt;
&amp;gt;&amp;gt; import com.mathworks.jmi.*;&lt;br&gt;
&amp;gt;&amp;gt; matlab = Matlab;&lt;br&gt;
&amp;gt;&amp;gt; matlab.eval('1+1'); &lt;br&gt;
&lt;br&gt;
I think, that one solution is not to use the java event&lt;br&gt;
mechanisms but the Matlab methods for defining callback&lt;br&gt;
routines.&lt;br&gt;
&lt;br&gt;
Example:&lt;br&gt;
&lt;br&gt;
button = JButton('Close');&lt;br&gt;
set(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
&lt;br&gt;
function buttonClosePressed(handle,event)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;doSomething();&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
-------------&lt;br&gt;
&lt;br&gt;
See &lt;a href=&quot;http://xtargets.com/snippets/tag/java&quot;&gt;http://xtargets.com/snippets/tag/java&lt;/a&gt; for details and&lt;br&gt;
further examples.&lt;br&gt;
&lt;br&gt;
Don't expect Mathworks to answer your question... they do&lt;br&gt;
not support Java GUI-Matlab interaction.&lt;br&gt;
&lt;br&gt;
Wolfgang</description>
    </item>
    <item>
      <pubDate>Wed, 31 Oct 2007 18:54:40 -0400</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#399260</link>
      <author>Michael Bushe</author>
      <description>&quot;Wolfgang Ulmer&quot; &amp;lt;spam@wulmer.de&amp;gt; wrote in message&lt;br&gt;
&amp;lt;feg3vs$6ha$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi Michael,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; while using Swing with Java, I discovered the same problem&lt;br&gt;
&amp;gt; which is due to the fact, that Matlab uses the AWT/Swing&lt;br&gt;
&amp;gt; Event  Handling Thread to do some computation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You can even easily create a deadlock if you enter the&lt;br&gt;
&amp;gt; following commands on the prompt:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; import com.mathworks.jmi.*;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; matlab = Matlab;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; matlab.eval('1+1'); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I think, that one solution is not to use the java event&lt;br&gt;
&amp;gt; mechanisms but the Matlab methods for defining callback&lt;br&gt;
&amp;gt; routines.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; button = JButton('Close');&lt;br&gt;
&amp;gt; set(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function buttonClosePressed(handle,event)&lt;br&gt;
&amp;gt;    doSomething();&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -------------&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; See &lt;a href=&quot;http://xtargets.com/snippets/tag/java&quot;&gt;http://xtargets.com/snippets/tag/java&lt;/a&gt; for details and&lt;br&gt;
&amp;gt; further examples.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Don't expect Mathworks to answer your question... they do&lt;br&gt;
&amp;gt; not support Java GUI-Matlab interaction.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Wolfgang&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Wolfgang's example will do most of what you want, and he is&lt;br&gt;
right - you need to use MATLAB-provided APIs for accessing&lt;br&gt;
Swing Events.  It is not correct, however, that MATLAB does&lt;br&gt;
computation on the AWT/Swing thread, this would lock up&lt;br&gt;
GUIs.  Also, using jmi classes is not a good idea.  They are&lt;br&gt;
not public or supported and it's easy to use them improperly&lt;br&gt;
(even for developers inside the MathWorks).  Lastly, there&lt;br&gt;
is a memory leak in the example, the workaround is detailed&lt;br&gt;
below, but first, let's discuss Swing threading.&lt;br&gt;
&lt;br&gt;
The Swing threading problem is not specific to MATLAB.  In&lt;br&gt;
pure Java if you write the following innocent looking code,&lt;br&gt;
it is erroneous:&lt;br&gt;
&lt;br&gt;
package example;&lt;br&gt;
&lt;br&gt;
import javax.swing.*;&lt;br&gt;
&lt;br&gt;
public class SwingApp {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JFrame frame = new JFrame();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JButton button = new JButton(&quot;Do Something&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void showFrame() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.getContentPane().add(getButton());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.pack();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.setVisible(true);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public JButton getButton() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return button;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Wrong! Swing object construction not on the&lt;br&gt;
EventDispatchThread&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SwingApp app = new SwingApp();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Wrong! Swing component access not on the&lt;br&gt;
EventDispatchThread&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;app.showFrame();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
The problem is that the class is making Swing calls on&lt;br&gt;
Java's main thread.  Sun asks developers to follow the rule&lt;br&gt;
that all access to Swing components must occur on Swing's&lt;br&gt;
EventDispatchThread (there may be some leeway, but it's best&lt;br&gt;
to always follow the rule).  This may look like it works&lt;br&gt;
fine, but every so often you'll encounter lock ups and&lt;br&gt;
strange behavior like listeners not firing.&lt;br&gt;
&lt;br&gt;
This rule is broken when calling Swing components from the&lt;br&gt;
MATLAB command line in the same manner since Swing calls are&lt;br&gt;
being made from the MATLAB thread:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; javaaddpath c:\myapp\classes&lt;br&gt;
&amp;gt;&amp;gt; app = example.SwingApp;  % Wrong! Swing object&lt;br&gt;
construction not on the EventDispatchThread&lt;br&gt;
&amp;gt;&amp;gt; app.showFrame(); % Wrong! Swing component access not on&lt;br&gt;
the EventDispatchThread&lt;br&gt;
&lt;br&gt;
In pure Java, the right way to write this code is to wrap&lt;br&gt;
all Swing calls in a Runnable that is later called on the&lt;br&gt;
EventDispatchThread:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String args[]) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SwingUtilities.invokeLater(new Runnable() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void run() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SwingApp app = new SwingApp();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;app.showFrame();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
How can you do the same from MATLAB?  There are additional&lt;br&gt;
undocumented APIs that you can use to avoid Java Swing&lt;br&gt;
threading issues, specifically awtcreate, awtinvoke and&lt;br&gt;
javacomponent.  These APIs are not yet final and are&lt;br&gt;
therefore undocumented and can change in future releases. &lt;br&gt;
Please don&amp;#8217;t expect this to be the best way to work with&lt;br&gt;
Swing Components going forward as we are planning to change&lt;br&gt;
these APIs.  We appreciate any feedback you can give us to&lt;br&gt;
help us deliver APIs that meet your needs.  &lt;br&gt;
&lt;br&gt;
The functions awtcreate and awtinvoke both ship undocumented&lt;br&gt;
&amp;#8211; open the M-file for example usage.  They both ensure that&lt;br&gt;
the work for the M command that interacts with the Java&lt;br&gt;
component happens on Java's EventDispathThread and not&lt;br&gt;
MATLAB's main thread.  &lt;br&gt;
&lt;br&gt;
For the SwingApp, you can ensure the calls are pushed onto&lt;br&gt;
the EventDispatchThread by using awtcreate and awtinvoke&lt;br&gt;
like so:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; app = awtcreate('example.SwingApp');&lt;br&gt;
&amp;gt;&amp;gt; awtinvoke(app, 'showFrame');&lt;br&gt;
&lt;br&gt;
The undocumented awtcreate and awtinvoke commands are like&lt;br&gt;
wrapping each M line in a SwingUtiities.invokeLater call. &lt;br&gt;
Though unsupported now, it works well.  &lt;br&gt;
&lt;br&gt;
If you are writing your own Java classes, you may want to&lt;br&gt;
consider not using awtcreate and awtinvoke and push&lt;br&gt;
Swing-related Java code to your own classes.  This will make&lt;br&gt;
for better performance since it is one push to Swing's&lt;br&gt;
EventDispatchThread, not many.  &lt;br&gt;
&lt;br&gt;
For example, you could run the example SwingApp above with&lt;br&gt;
the main() corrected to use SwingUtilities.invokeLater,&lt;br&gt;
directly from M:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; example.SwingApp.main('')&lt;br&gt;
&lt;br&gt;
Since the class's main() method calls invokeLater, this is&lt;br&gt;
safe.  &lt;br&gt;
&lt;br&gt;
Rewriting your original example using awtcreate and&lt;br&gt;
awtinvoke would look like this:&lt;br&gt;
&lt;br&gt;
button = awtcreate('javax.swing.JButton');&lt;br&gt;
awtinvoke(button,'setLabel','Close');&lt;br&gt;
&lt;br&gt;
% make the button a handle object before calling set&lt;br&gt;
button = handle(button,'callbackproperties');&lt;br&gt;
set(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
javacomponent(button)&lt;br&gt;
&lt;br&gt;
In addition to the awtcreate and awtinvoke usage, there are&lt;br&gt;
two other things to notice about the rewritten example:&lt;br&gt;
1) It's better to use the &quot;handle&quot; command before calling&lt;br&gt;
&quot;set&quot; (this will prevent a memory leak).&lt;br&gt;
2) The javacomponent command, also undocumented, puts any&lt;br&gt;
Java Swing component in a figure window &amp;#8211; open the M-file&lt;br&gt;
for example usage.&lt;br&gt;
&lt;br&gt;
This is tricky stuff especially since the threading issues&lt;br&gt;
are not immediately apparent.  We are interested in&lt;br&gt;
providing the best Java GUI experience we can in MATLAB. &lt;br&gt;
Any feedback is greatly appreciated.</description>
    </item>
    <item>
      <pubDate>Wed, 31 Oct 2007 21:59:55 -0400</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#399282</link>
      <author>Yair Altman</author>
      <description>Michael,&lt;br&gt;
&lt;br&gt;
I'm absolutely thrilled that someone at TMW, finally after&lt;br&gt;
all these years, has taken the time to publicly comment on&lt;br&gt;
this issue. Please be assured that readers of this thread&lt;br&gt;
understand that the entire issue of Swing/JMI integration is&lt;br&gt;
not officially supported, and that the API is subject to&lt;br&gt;
change and might contain bugs.&lt;br&gt;
&lt;br&gt;
This said, please let us know if we may approach you (or&lt;br&gt;
Bill, who has shown a flicker of Java-related interest) with&lt;br&gt;
Java-related bugs/requests. All official doors are closed to&lt;br&gt;
us, you know, because of the unsupported nature. You've&lt;br&gt;
shown an interest in advancing ML-Java integration - user&lt;br&gt;
inputs may help.&lt;br&gt;
&lt;br&gt;
To the point: awtinvoke (and related parseJavaSignature)&lt;br&gt;
have several limitations/bugs that prevent its usage in some&lt;br&gt;
cases. In other cases, problems in the Java objects&lt;br&gt;
reflection visibility, which is used by awtinvoke, cause&lt;br&gt;
awtinvoke to fail. In all these cases we have to resort to&lt;br&gt;
using the direct calling syntax:&lt;br&gt;
&lt;br&gt;
jbutton.setLabel('Close');&lt;br&gt;
&lt;br&gt;
Unfortunately, starting with R2007b, this syntax displays an&lt;br&gt;
ugly warning and stack dump on the Command Window, although&lt;br&gt;
the call itself works well. One has to use another&lt;br&gt;
undocumented feature to turn the warnings off (I don't think&lt;br&gt;
this has ever been reported yet): &lt;br&gt;
&lt;br&gt;
com.mathworks.mwswing.MJUtilities.setThreadingChecksEnabled(0);&lt;br&gt;
&lt;br&gt;
Another major problem using Java in Matlab is that AFAIK&lt;br&gt;
Java exceptions cannot be caught (via matlab's try-catch)&lt;br&gt;
nor suppressed from the Command Window.&lt;br&gt;
&lt;br&gt;
I think I'll stop here (there's plenty more where this came&lt;br&gt;
from). If you, Bill or anyone picks up the ball I'll be&lt;br&gt;
happy to expand, and I'm sure others will too.&lt;br&gt;
&lt;br&gt;
Yair Altman</description>
    </item>
    <item>
      <pubDate>Fri, 02 Nov 2007 17:32:11 -0400</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#399612</link>
      <author>Bill York</author>
      <description>Yair, I'm glad to hear your enthusiasm!&lt;br&gt;
&lt;br&gt;
Feel free to contact me directly with questions about Java.&lt;br&gt;
&lt;br&gt;
Michael and I will craft a reply and possibly post it here.&lt;br&gt;
&lt;br&gt;
Bill</description>
    </item>
    <item>
      <pubDate>Mon, 12 Nov 2007 16:13:43 -0500</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#400882</link>
      <author>Arwel Hughes</author>
      <description>Hi Guys,&lt;br&gt;
I too am very pleased to see these Java / ML issues being &lt;br&gt;
adressed! I'm writing an application at the moment which &lt;br&gt;
has a large, multi frame Swing MDI sitting on top of a &lt;br&gt;
Matlab program. It seems to be working okay, but I guess &lt;br&gt;
I've just been lucky as I've not taken any special &lt;br&gt;
precautions for threading issues, so I read the above with &lt;br&gt;
great interest.&lt;br&gt;
&lt;br&gt;
My main approach is to deal with most of the Swing in their &lt;br&gt;
own pure classes, and invoke these from Matlab. But, &lt;br&gt;
there's been a great deal of bits where I've felt I havent &lt;br&gt;
been able to do this, because I didn't know how to access &lt;br&gt;
the 'ActionPerformedCallback' etc for objects instantiated &lt;br&gt;
in Java.&lt;br&gt;
So for example, if I have something along the lines of....&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
import java.awt.*;&lt;br&gt;
import javax.swing.*;&lt;br&gt;
&lt;br&gt;
public class myButtons extends JFrame {&lt;br&gt;
&amp;nbsp;&amp;nbsp;public static void main(String[] args) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new myButtons();&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;public myButtons() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Container content = getContentPane();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;content.setLayout(new FlowLayout());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JButton button1 = new JButton(&quot;Java&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;content.add(button1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;button1.addActionListener(this)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pack();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;setVisible(true);&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;public void ActionPerformed(ActionEvent event){&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;!HOW TO GET THIS TO POINT TO MATLAB?!&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
...I have not been able to work out how to get Matlab &lt;br&gt;
respond to the button events. So, I've had to do this in ML &lt;br&gt;
on lots of things....&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
myFrame = JFrame();&lt;br&gt;
button = JButton;&lt;br&gt;
myFrame.getContentPane().add(button);&lt;br&gt;
set(button,'ActionPerformedCallback',@myFun..&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
So it seems from the examples above that if I could find &lt;br&gt;
the relevant objects in a pure Swing made GUI, then I could &lt;br&gt;
use the &lt;br&gt;
&lt;br&gt;
button = handle(button,'callbackproperties');&lt;br&gt;
set(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
&lt;br&gt;
approach to set the callback to point to Matlab. This would &lt;br&gt;
actually be very nice indeed, as then in principle one &lt;br&gt;
could make a very nice looking GUI using something like the &lt;br&gt;
Netbeans GUI designer, and then just invoke this from &lt;br&gt;
Matlab, and use the 'handle' way of setting up the relevant &lt;br&gt;
callbacks??&lt;br&gt;
&lt;br&gt;
Another drawback with my Swing based MDI of course is that &lt;br&gt;
I've not been able to put Matlab graphics into a Java &lt;br&gt;
container. I've had a look at Yair's  'GetJFrame' and hoped &lt;br&gt;
that I could just steal the underlying Jpanel from a matlab &lt;br&gt;
window and pop that into a Java frame, but when you do that &lt;br&gt;
it produces a whole load of errors because of the &lt;br&gt;
unexpected moving of the container, I think (something like &lt;br&gt;
that. I've not looked at this in detail). I suppose one &lt;br&gt;
would have to clone the container or something, but of &lt;br&gt;
course you can't clone a JPanel. Anyway, for now my MDI &lt;br&gt;
uses JFreeChart objects created from Matlab for the &lt;br&gt;
plotting, which is frustrating but it works.&lt;br&gt;
&lt;br&gt;
Anyway, very good to see this thread.&lt;br&gt;
Cheers,&lt;br&gt;
arwel </description>
    </item>
    <item>
      <pubDate>Mon, 12 Nov 2007 18:47:06 -0500</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#400906</link>
      <author>Malcolm Lidierth</author>
      <description>I vote with Yair. I use swing components extensively for my &lt;br&gt;
GUIs. They are more extensive, controllable, flexible and a &lt;br&gt;
lot more attractive than the MATLAB GUIs. They also produce &lt;br&gt;
more consistent results across platforms.&lt;br&gt;
&lt;br&gt;
I have not seen the exceptions Yair comments on in R2007B &lt;br&gt;
but saw something similar in its pre-release. The fix from &lt;br&gt;
TMW support then was &lt;br&gt;
&quot;To prevent these warning messages from appearing, you may &lt;br&gt;
create a java.opts file in the $MATLAB/bin/$ARCH &lt;br&gt;
(Where $MATLAB is your root MATLAB directory and $ARCH is &lt;br&gt;
your architecture.) &lt;br&gt;
or in the current directory when you start MATLAB, &lt;br&gt;
containing the following command:&lt;br&gt;
-Dmathworks.WarnIfWrongThread=false&quot;</description>
    </item>
    <item>
      <pubDate>Fri, 16 Nov 2007 12:05:25 -0500</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#401667</link>
      <author>Arwel Hughes</author>
      <description>&quot;Michael Bushe&quot; &amp;lt;michael.bushe-removeme@mathworks.com&amp;gt; &lt;br&gt;
wrote in message &amp;lt;fgaj1g$imo$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Wolfgang Ulmer&quot; &amp;lt;spam@wulmer.de&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;feg3vs$6ha$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi Michael,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; while using Swing with Java, I discovered the same &lt;br&gt;
problem&lt;br&gt;
&amp;gt; &amp;gt; which is due to the fact, that Matlab uses the AWT/Swing&lt;br&gt;
&amp;gt; &amp;gt; Event  Handling Thread to do some computation.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; You can even easily create a deadlock if you enter the&lt;br&gt;
&amp;gt; &amp;gt; following commands on the prompt:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; import com.mathworks.jmi.*;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; matlab = Matlab;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; matlab.eval('1+1'); &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I think, that one solution is not to use the java event&lt;br&gt;
&amp;gt; &amp;gt; mechanisms but the Matlab methods for defining callback&lt;br&gt;
&amp;gt; &amp;gt; routines.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Example:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; button = JButton('Close');&lt;br&gt;
&amp;gt; &amp;gt; set&lt;br&gt;
(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; function buttonClosePressed(handle,event)&lt;br&gt;
&amp;gt; &amp;gt;    doSomething();&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; -------------&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; See &lt;a href=&quot;http://xtargets.com/snippets/tag/java&quot;&gt;http://xtargets.com/snippets/tag/java&lt;/a&gt; for details &lt;br&gt;
and&lt;br&gt;
&amp;gt; &amp;gt; further examples.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Don't expect Mathworks to answer your question... they &lt;br&gt;
do&lt;br&gt;
&amp;gt; &amp;gt; not support Java GUI-Matlab interaction.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Wolfgang&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Wolfgang's example will do most of what you want, and he &lt;br&gt;
is&lt;br&gt;
&amp;gt; right - you need to use MATLAB-provided APIs for accessing&lt;br&gt;
&amp;gt; Swing Events.  It is not correct, however, that MATLAB &lt;br&gt;
does&lt;br&gt;
&amp;gt; computation on the AWT/Swing thread, this would lock up&lt;br&gt;
&amp;gt; GUIs.  Also, using jmi classes is not a good idea.  They &lt;br&gt;
are&lt;br&gt;
&amp;gt; not public or supported and it's easy to use them &lt;br&gt;
improperly&lt;br&gt;
&amp;gt; (even for developers inside the MathWorks).  Lastly, there&lt;br&gt;
&amp;gt; is a memory leak in the example, the workaround is &lt;br&gt;
detailed&lt;br&gt;
&amp;gt; below, but first, let's discuss Swing threading.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The Swing threading problem is not specific to MATLAB.  In&lt;br&gt;
&amp;gt; pure Java if you write the following innocent looking &lt;br&gt;
code,&lt;br&gt;
&amp;gt; it is erroneous:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; package example;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; import javax.swing.*;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; public class SwingApp {&lt;br&gt;
&amp;gt;     JFrame frame = new JFrame();&lt;br&gt;
&amp;gt;     JButton button = new JButton(&quot;Do Something&quot;);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     public void showFrame() {&lt;br&gt;
&amp;gt;       frame.getContentPane().add(getButton());&lt;br&gt;
&amp;gt;       frame.setDefaultCloseOperation&lt;br&gt;
(JFrame.DISPOSE_ON_CLOSE);&lt;br&gt;
&amp;gt;         frame.pack();&lt;br&gt;
&amp;gt;         frame.setVisible(true);&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     public JButton getButton() {&lt;br&gt;
&amp;gt;         return button;&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     public static void main(String args[]) {&lt;br&gt;
&amp;gt;         //Wrong! Swing object construction not on the&lt;br&gt;
&amp;gt; EventDispatchThread&lt;br&gt;
&amp;gt;         SwingApp app = new SwingApp();&lt;br&gt;
&amp;gt;         //Wrong! Swing component access not on the&lt;br&gt;
&amp;gt; EventDispatchThread&lt;br&gt;
&amp;gt;         app.showFrame();&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The problem is that the class is making Swing calls on&lt;br&gt;
&amp;gt; Java's main thread.  Sun asks developers to follow the &lt;br&gt;
rule&lt;br&gt;
&amp;gt; that all access to Swing components must occur on Swing's&lt;br&gt;
&amp;gt; EventDispatchThread (there may be some leeway, but it's &lt;br&gt;
best&lt;br&gt;
&amp;gt; to always follow the rule).  This may look like it works&lt;br&gt;
&amp;gt; fine, but every so often you'll encounter lock ups and&lt;br&gt;
&amp;gt; strange behavior like listeners not firing.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This rule is broken when calling Swing components from the&lt;br&gt;
&amp;gt; MATLAB command line in the same manner since Swing calls &lt;br&gt;
are&lt;br&gt;
&amp;gt; being made from the MATLAB thread:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; javaaddpath c:\myapp\classes&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; app = example.SwingApp;  % Wrong! Swing object&lt;br&gt;
&amp;gt; construction not on the EventDispatchThread&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; app.showFrame(); % Wrong! Swing component access not on&lt;br&gt;
&amp;gt; the EventDispatchThread&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In pure Java, the right way to write this code is to wrap&lt;br&gt;
&amp;gt; all Swing calls in a Runnable that is later called on the&lt;br&gt;
&amp;gt; EventDispatchThread:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     public static void main(String args[]) {&lt;br&gt;
&amp;gt;         SwingUtilities.invokeLater(new Runnable() {&lt;br&gt;
&amp;gt;             public void run() {&lt;br&gt;
&amp;gt;                 SwingApp app = new SwingApp();&lt;br&gt;
&amp;gt;                 app.showFrame();&lt;br&gt;
&amp;gt;             }&lt;br&gt;
&amp;gt;         });&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How can you do the same from MATLAB?  There are additional&lt;br&gt;
&amp;gt; undocumented APIs that you can use to avoid Java Swing&lt;br&gt;
&amp;gt; threading issues, specifically awtcreate, awtinvoke and&lt;br&gt;
&amp;gt; javacomponent.  These APIs are not yet final and are&lt;br&gt;
&amp;gt; therefore undocumented and can change in future releases. &lt;br&gt;
&amp;gt; Please don&amp;#8217;t expect this to be the best way to work with&lt;br&gt;
&amp;gt; Swing Components going forward as we are planning to &lt;br&gt;
change&lt;br&gt;
&amp;gt; these APIs.  We appreciate any feedback you can give us to&lt;br&gt;
&amp;gt; help us deliver APIs that meet your needs.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The functions awtcreate and awtinvoke both ship &lt;br&gt;
undocumented&lt;br&gt;
&amp;gt; &amp;#8211; open the M-file for example usage.  They both ensure &lt;br&gt;
that&lt;br&gt;
&amp;gt; the work for the M command that interacts with the Java&lt;br&gt;
&amp;gt; component happens on Java's EventDispathThread and not&lt;br&gt;
&amp;gt; MATLAB's main thread.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For the SwingApp, you can ensure the calls are pushed onto&lt;br&gt;
&amp;gt; the EventDispatchThread by using awtcreate and awtinvoke&lt;br&gt;
&amp;gt; like so:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; app = awtcreate('example.SwingApp');&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; awtinvoke(app, 'showFrame');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The undocumented awtcreate and awtinvoke commands are like&lt;br&gt;
&amp;gt; wrapping each M line in a SwingUtiities.invokeLater call. &lt;br&gt;
&amp;gt; Though unsupported now, it works well.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you are writing your own Java classes, you may want to&lt;br&gt;
&amp;gt; consider not using awtcreate and awtinvoke and push&lt;br&gt;
&amp;gt; Swing-related Java code to your own classes.  This will &lt;br&gt;
make&lt;br&gt;
&amp;gt; for better performance since it is one push to Swing's&lt;br&gt;
&amp;gt; EventDispatchThread, not many.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For example, you could run the example SwingApp above with&lt;br&gt;
&amp;gt; the main() corrected to use SwingUtilities.invokeLater,&lt;br&gt;
&amp;gt; directly from M:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; example.SwingApp.main('')&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since the class's main() method calls invokeLater, this is&lt;br&gt;
&amp;gt; safe.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Rewriting your original example using awtcreate and&lt;br&gt;
&amp;gt; awtinvoke would look like this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; button = awtcreate('javax.swing.JButton');&lt;br&gt;
&amp;gt; awtinvoke(button,'setLabel','Close');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % make the button a handle object before calling set&lt;br&gt;
&amp;gt; button = handle(button,'callbackproperties');&lt;br&gt;
&amp;gt; set(button,'ActionPerformedCallback',@buttonClosePressed);&lt;br&gt;
&amp;gt; javacomponent(button)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In addition to the awtcreate and awtinvoke usage, there &lt;br&gt;
are&lt;br&gt;
&amp;gt; two other things to notice about the rewritten example:&lt;br&gt;
&amp;gt; 1) It's better to use the &quot;handle&quot; command before calling&lt;br&gt;
&amp;gt; &quot;set&quot; (this will prevent a memory leak).&lt;br&gt;
&amp;gt; 2) The javacomponent command, also undocumented, puts any&lt;br&gt;
&amp;gt; Java Swing component in a figure window &amp;#8211; open the M-file&lt;br&gt;
&amp;gt; for example usage.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This is tricky stuff especially since the threading issues&lt;br&gt;
&amp;gt; are not immediately apparent.  We are interested in&lt;br&gt;
&amp;gt; providing the best Java GUI experience we can in MATLAB. &lt;br&gt;
&amp;gt; Any feedback is greatly appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Michael,&lt;br&gt;
I tried playing around with the example, and couldn't get &lt;br&gt;
it to work by calling the 'main' method as you suggest. It &lt;br&gt;
runs, but the callback doesn't seem to respond.&lt;br&gt;
&lt;br&gt;
So, if I have the 'public static void main..' in there, and &lt;br&gt;
do this...&lt;br&gt;
&lt;br&gt;
app = swingApp();&lt;br&gt;
app.main('');&lt;br&gt;
button = app.getButton();&lt;br&gt;
buttonProps = handle(button,'callbackproperties');&lt;br&gt;
set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&lt;br&gt;
... the @buttonPressed callback doesn't get called on a &lt;br&gt;
button press.&lt;br&gt;
&lt;br&gt;
However, if I comment out the 'public static...main' &lt;br&gt;
method, and call ShowFrame directly from Matlab like &lt;br&gt;
this....&lt;br&gt;
&lt;br&gt;
app = swingApp()&lt;br&gt;
app.ShowFrame();&lt;br&gt;
button = app.getButton();&lt;br&gt;
buttonProps = handle(button,'callbackproperties');&lt;br&gt;
set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&lt;br&gt;
.. it works fine. Am I doing something wrong?  The complete &lt;br&gt;
codes of the example I'm trying are below, matlab first &lt;br&gt;
(and yes, I know theres none of the threading bits in there &lt;br&gt;
yet.)&lt;br&gt;
&lt;br&gt;
-mTest.m&lt;br&gt;
&lt;br&gt;
function mTest()&lt;br&gt;
clear&lt;br&gt;
javaaddpath c:\java\matlabthreads2\Main\build\classes&lt;br&gt;
&lt;br&gt;
app = swingApp();&lt;br&gt;
%app.main('');&lt;br&gt;
app.ShowFrame();&lt;br&gt;
&lt;br&gt;
button = app.getButton();&lt;br&gt;
buttonProps = handle(button,'callbackproperties');&lt;br&gt;
set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&lt;br&gt;
disp('debug')&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
function buttonPressed(src,ev)&lt;br&gt;
disp('Button Pressed Callback');&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
- swingApp.java&lt;br&gt;
&lt;br&gt;
import javax.swing.*;&lt;br&gt;
import java.awt.*;&lt;br&gt;
&lt;br&gt;
public class swingApp {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JFrame frame = new JFrame();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JButton button1 = new JButton(&quot;Do Something&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void ShowFrame() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.getContentPane().add(getButton());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.setDefaultCloseOperation&lt;br&gt;
(JFrame.DISPOSE_ON_CLOSE);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.pack();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.setSize(400,400);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;frame.setVisible(true);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public JButton getButton() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return button1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;/*   public static void main(String[] args) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;swingApp app = new swingApp();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;app.ShowFrame();&lt;br&gt;
}*/&lt;br&gt;
}</description>
    </item>
    <item>
      <pubDate>Tue, 11 Dec 2007 18:23:07 -0500</pubDate>
      <title>Re: Profiling Java in Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/156388#405755</link>
      <author>Michael Bushe</author>
      <description>&quot;Arwel Hughes&quot; &amp;lt;a.v.hughes@rl.ac.uk&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fhk125$8rq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; Michael,&lt;br&gt;
&amp;gt; I tried playing around with the example, and couldn't get &lt;br&gt;
&amp;gt; it to work by calling the 'main' method as you suggest. It &lt;br&gt;
&amp;gt; runs, but the callback doesn't seem to respond.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, if I have the 'public static void main..' in there, and &lt;br&gt;
&amp;gt; do this...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; app = swingApp();&lt;br&gt;
&amp;gt; app.main('');&lt;br&gt;
&amp;gt; button = app.getButton();&lt;br&gt;
&amp;gt; buttonProps = handle(button,'callbackproperties');&lt;br&gt;
&amp;gt; set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ... the @buttonPressed callback doesn't get called on a &lt;br&gt;
&amp;gt; button press.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, if I comment out the 'public static...main' &lt;br&gt;
&amp;gt; method, and call ShowFrame directly from Matlab like &lt;br&gt;
&amp;gt; this....&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; app = swingApp()&lt;br&gt;
&amp;gt; app.ShowFrame();&lt;br&gt;
&amp;gt; button = app.getButton();&lt;br&gt;
&amp;gt; buttonProps = handle(button,'callbackproperties');&lt;br&gt;
&amp;gt; set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; .. it works fine. Am I doing something wrong?  The complete &lt;br&gt;
&amp;gt; codes of the example I'm trying are below, matlab first &lt;br&gt;
&amp;gt; (and yes, I know theres none of the threading bits in there &lt;br&gt;
&amp;gt; yet.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -mTest.m&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function mTest()&lt;br&gt;
&amp;gt; clear&lt;br&gt;
&amp;gt; javaaddpath c:\java\matlabthreads2\Main\build\classes&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; app = swingApp();&lt;br&gt;
&amp;gt; %app.main('');&lt;br&gt;
&amp;gt; app.ShowFrame();&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; button = app.getButton();&lt;br&gt;
&amp;gt; buttonProps = handle(button,'callbackproperties');&lt;br&gt;
&amp;gt; set(buttonProps,'ActionPerformedCallback',@buttonPressed);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; disp('debug')&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function buttonPressed(src,ev)&lt;br&gt;
&amp;gt; disp('Button Pressed Callback');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; - swingApp.java&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; import javax.swing.*;&lt;br&gt;
&amp;gt; import java.awt.*;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; public class swingApp {&lt;br&gt;
&amp;gt;         JFrame frame = new JFrame();&lt;br&gt;
&amp;gt;         JButton button1 = new JButton(&quot;Do Something&quot;);&lt;br&gt;
&amp;gt;         &lt;br&gt;
&amp;gt;     public void ShowFrame() {&lt;br&gt;
&amp;gt;         frame.getContentPane().add(getButton());&lt;br&gt;
&amp;gt;         frame.setDefaultCloseOperation&lt;br&gt;
&amp;gt; (JFrame.DISPOSE_ON_CLOSE);&lt;br&gt;
&amp;gt;         frame.pack();&lt;br&gt;
&amp;gt;         frame.setSize(400,400);&lt;br&gt;
&amp;gt;         frame.setVisible(true);&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     public JButton getButton() {&lt;br&gt;
&amp;gt;         return button1;&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;  /*   public static void main(String[] args) {&lt;br&gt;
&amp;gt;         swingApp app = new swingApp();&lt;br&gt;
&amp;gt;         app.ShowFrame();&lt;br&gt;
&amp;gt; }*/&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Sorry to take so long to get back to you. &lt;br&gt;
&lt;br&gt;
The problem with calling main is that it causes a threading&lt;br&gt;
problem.  I said it was safe in my post, but it is not.  See&lt;br&gt;
this threading stuff is tricky!  :-)&lt;br&gt;
&lt;br&gt;
The reason why calling main is not thread-safe is that&lt;br&gt;
MATLAB just calls main and returns.  MATLAB does not know&lt;br&gt;
that main() calls SwingUtilities.  What is likely happening&lt;br&gt;
is that the call to main() returns before the&lt;br&gt;
EventDispatchThread can initialize and run the first event.&lt;br&gt;
&amp;nbsp;This messes up the listener registration.  &lt;br&gt;
&lt;br&gt;
I recommend that you change your code to make it thread&lt;br&gt;
safe.  It looks like you can get away with what you are&lt;br&gt;
doing, but a stitch in time saves nine.&lt;br&gt;
&lt;br&gt;
Michael</description>
    </item>
  </channel>
</rss>

