Path: news.mathworks.com!not-for-mail
From: "Michael Bushe" <michael.bushe-removeme@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Profiling Java in Matlab
Date: Tue, 11 Dec 2007 18:23:07 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 110
Message-ID: <fjmkib$fni$1@fred.mathworks.com>
References: <fcqhf3$fcj$1@fred.mathworks.com> <feg3vs$6ha$1@fred.mathworks.com> <fgaj1g$imo$1@fred.mathworks.com> <fhk125$8rq$1@fred.mathworks.com>
Reply-To: "Michael Bushe" <michael.bushe-removeme@mathworks.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1197397387 16114 172.30.248.38 (11 Dec 2007 18:23:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 11 Dec 2007 18:23:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 887095
Xref: news.mathworks.com comp.soft-sys.matlab:442032



"Arwel Hughes" <a.v.hughes@rl.ac.uk> wrote in message
<fhk125$8rq$1@fred.mathworks.com>...

> Michael,
> I tried playing around with the example, and couldn't get 
> it to work by calling the 'main' method as you suggest. It 
> runs, but the callback doesn't seem to respond.
> 
> So, if I have the 'public static void main..' in there, and 
> do this...
> 
> app = swingApp();
> app.main('');
> button = app.getButton();
> buttonProps = handle(button,'callbackproperties');
> set(buttonProps,'ActionPerformedCallback',@buttonPressed);
> 
> ... the @buttonPressed callback doesn't get called on a 
> button press.
> 
> However, if I comment out the 'public static...main' 
> method, and call ShowFrame directly from Matlab like 
> this....
> 
> app = swingApp()
> app.ShowFrame();
> button = app.getButton();
> buttonProps = handle(button,'callbackproperties');
> set(buttonProps,'ActionPerformedCallback',@buttonPressed);
> 
> .. it works fine. Am I doing something wrong?  The complete 
> codes of the example I'm trying are below, matlab first 
> (and yes, I know theres none of the threading bits in there 
> yet.)
> 
> -mTest.m
> 
> function mTest()
> clear
> javaaddpath c:\java\matlabthreads2\Main\build\classes
> 
> app = swingApp();
> %app.main('');
> app.ShowFrame();
> 
> button = app.getButton();
> buttonProps = handle(button,'callbackproperties');
> set(buttonProps,'ActionPerformedCallback',@buttonPressed);
> 
> disp('debug')
> 
> 
> 
> function buttonPressed(src,ev)
> disp('Button Pressed Callback');
> 
> 
> 
> 
> 
> - swingApp.java
> 
> import javax.swing.*;
> import java.awt.*;
> 
> public class swingApp {
>         JFrame frame = new JFrame();
>         JButton button1 = new JButton("Do Something");
>         
>     public void ShowFrame() {
>         frame.getContentPane().add(getButton());
>         frame.setDefaultCloseOperation
> (JFrame.DISPOSE_ON_CLOSE);
>         frame.pack();
>         frame.setSize(400,400);
>         frame.setVisible(true);
>     }
>     
>     
>     public JButton getButton() {
>         return button1;
>     }
>     
>  /*   public static void main(String[] args) {
>         swingApp app = new swingApp();
>         app.ShowFrame();
> }*/
> }
> 
> 
> 

Sorry to take so long to get back to you. 

The problem with calling main is that it causes a threading
problem.  I said it was safe in my post, but it is not.  See
this threading stuff is tricky!  :-)

The reason why calling main is not thread-safe is that
MATLAB just calls main and returns.  MATLAB does not know
that main() calls SwingUtilities.  What is likely happening
is that the call to main() returns before the
EventDispatchThread can initialize and run the first event.
 This messes up the listener registration.  

I recommend that you change your code to make it thread
safe.  It looks like you can get away with what you are
doing, but a stitch in time saves nine.

Michael