Using pure Java GUI in deployed Matlab apps

I would like to welcome repeat guest blogger Karthik Ponudurai, who has previously written here about the JTattoo look-and-feel, and about integration of Java controls’ events with Matlab callbacks. Today, Karthik discusses a problem when we wish to use pure-Java GUIs in deployed Matlab programs, and the undocumented simple solution.

Reminder: I will be visiting several US cities (Minneapolis, Boston and New York) in July 22-31 (details). Let me know if you’d like to meet me there.


Using a pure-Java Swing-based Graphical User Interface (GUI) has several important advantages compared to using a pure-Matlab GUI:

  • Java GUI widget toolkit provides a large collection of components (scrollbar, slider, etc…) and layouts (card, spring, etc…)
  • Multiple event handling options
  • Mature third-party Java Swing IDEs for quick and easy GUI development (Netbeans, Eclipse, etc…)
  • Supports pluggable look and feel that allows applications to have a look and feel that is unrelated to the underlying platform.
  • Java Swing’s window icon can be modified, whereas Mathworks apparently places an [entirely unreasonable] license restriction on modifying the Matlab figure window icon.

When Matlab is in development (non-deployed) mode, we can load and display a Java JFrame using the following Matlab code:

% 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
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

Java JFrame created in Matlab (click for full-size image)
Java JFrame created in Matlab (click for full-size image)

The problem

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.

The solution

To fix this problem, it turns out that we only need to add the Matlab waitfor command after making the Java window visible:

% (earlier code as above)
% Display the Java window
if isdeployed

Java JFrame in deployed Matlab (click for full-size image)
Java JFrame in deployed Matlab (click for full-size image)

The waitfor 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 waitfor.

A zip file containing the Matlab source-code and its resulting executable can be downloaded from here. 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.

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’t close the Java window by clicking its “X” button etc., but rather by calling jFrame.dispose(); in your Matlab code or the Matlab Desktop’s Command Window.

Related posts:

  1. Removing user preferences from deployed apps An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...
  2. Disabling menu entries in deployed docked figures Matlab's standard menu items can and should be removed from deployed docked figures. This article explains how. ...
  3. Speeding up compiled apps startup The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....
  4. Splash window for deployed applications Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...
  5. FindJObj – find a Matlab component’s underlying Java object The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....
  6. Matlab-Java interface using a static control The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....

Categories: Guest bloggers, GUI, Java, Medium risk of breaking in future versions, Undocumented feature

Tags: , , , ,

Bookmark and SharePrint Print

11 Responses to Using pure Java GUI in deployed Matlab apps

  1. ddli says:

    uipanel not clipping child uipanel
    Asked by SK on 4 May 2014
    Latest activity Edited by Jan Simon on 4 May 2014

    h_figure = figure;
    this.h0 = uipanel('Parent', h_figure, 'Clipping', 'on');
    this.h1 = uipanel('Parent', h0);

    h1 is a uipanel that can be scrolling implemented with a slider.

    However the scrolling uipanel does not always get clipped to the parent panel.

    The clipping failure seems to occur if the scrolling panel is large (say around twice the dimensions of the figure).

    A few others have had the problem but no solutions appear to have been found. Does anyone know of one?

    added: On further experimentation, it is not the size of the panel that is the issue. For example, the clipping works when the scrolling uipanel contains line plots but fails to work when the scrolling uipanel contains bar plots.


    I have the same problem? Could you tell me the way to solve it ? use java GUI? thanks!~

  2. Alvaro says:

    Can I use JFrame directly from Matlab? Something like:

    frame = JFrame('otr');
    if isdeployed

    After compiling it with Matlab Compiler and executing, nothing appears.
    I’m using matlab 2014a under MacOs.

    Kind regards.

    • @Alvaro – did you remember to import javax.swing.*?
      Also, you cannot waitfor(frame). You’ll need to find another solution to prevent the deployed app from exiting immediately.

  3. Sanaz says:

    Hi Dear Yair
    I have problem about connect java and matlab ,I have a project web based that fill some information about specifications a pump and when click next button it must go to matlab and show plot via client.jar and transfer to web page like this link
    whether it needs to jmatlink and matlab server for this connection or not??
    I thanksful very much if u Guide me!!!!!

    Best Regards

    • @Sanaz – it is not clear from your description if you want to run Matlab from within Java, or the reverse. Contact me by email (altmany at gmail) if you would like my consulting for your specific project.

  4. Karai says:

    Hi Yair,
    Is it possible to download Java source code for ExampleWindow?
    I would be very grateful if you could send me a link.

    Kind regards

  5. Yumin Sun says:

    Hi Yair,

    I want to write a GUI in Java first and then call it from Matlab,is that possible? Because I need to load some pictures with transparent background and then let them move, just like to do a flash. But in Matlab, I have problem with the background, when I load the png pictures, theu always have black background, I have read many articles to solve that problem, but still I feel too hard to accomplish that, many new questions come out. So I want to ask you, is that possible, i write Java code for the GUI and then call them form Matlab?



  6. Matthew Harrison says:

    Ok using this method where I have a compiled jar file how do i get the CallbackProperties for the jFrame instead of a button? Do i need to write a function in Java just to return the JFrame or window to be able to get at the Callback Properties?

    • @Matthew – if your compiled JAR is of a Matlab function, then wiithin the Matlab function you can get the window handle as follows (slightly different syntax for R2014a and earlier):

      mjf = get(handle(gcf), 'JavaFrame');
      jWindow = handle(mjf.fHG2Client.getWindow, 'CallbackProperties');

      If your compiled JAR is of a pure-Java class, then you will need to modify the Java code accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *


<pre lang="matlab">
a = magic(3);