MATLAB Answers

deprecated javacomponent and javaframe

210 views (last 30 days)
Bruno Luong
Bruno Luong on 12 Sep 2019
Commented: Walter Roberson on 10 Nov 2021 at 0:12
Starting from R2019b we are warned that javacomponent and javaframe property will be removed.
This is a bad news for me (and I guess also for Yair and his wonderful blog).
I would like to open this thread so tips and tricks for replacement during the transition.
For example I use javacomponent to drag the file/folder on windows explorer into a GUI, not sure how I can do that when it will be removed.
  8 Comments
Martin Lechner
Martin Lechner on 2 Nov 2019
Using "Using form input for selecting" from your mentioned HTML Rocks tutorial is working.
I'm no expert in HTML and JavaScript, but if I drop a file in your example somewhere in the browser window (out of your drop zone) the complete browser window is replaced by my content (only works in a browser, not in a Matlab figure). So I think this drag and drop support is disabled for this reason in Matlab UI figures.

Sign in to comment.

Answers (4)

Yair Altman
Yair Altman on 16 Sep 2019
I am very sorry to say that I am not aware of any easy way to make the transition from a Java-rich GUI to uifigures. All the main UI components are available (and then more), but many customizations of the components that are possible in Java are missing (for example, Border, Renderer, Editor etc.) not to mention the 30+ Swing callbacks for mouse/keyboard/action events that make Java GUIs "come to life".
In addition, there are simply too many fundamental differences that [IMHO] would foil any automated conversion attempt. For example, uifigure [still] do not have toolbars, toolstrips, customizable icon, or non-pixel units (esp. normalized).
Moreover, the fantastic GUILT (GUI Layout Toolbox), on which many existing GUIs depend, still does not have a uifigure variant (the new grid layout/container fills much of the need but a one-to-one variant of GUILT would remove a huge amout of necessary rework).
The new uihtml and uistyle functions look good at first glance, but in fact provide only a limited solution. We [still] have no documented way to customize existing uicomponents with our own CSS and JavaScript code, nor the ability to apply project/figure-wide CSS templates and JS functions. I can certainly understand MathWorks for not willing to commit its new UI framework to specific technologies such as CSS and JS, but the end result is that users are severly limited in their ability to customize their UIs.
The bottom line is that [at least at the current time] it looks like users need to manually redevelop their uifigure GUIs. Even with this manual redevelopment, we are severly limited in the new uifigure's customization compared to legacy Java-based UIs. Hopefully these limitations will be resolved over time, leaving us with just the benefits of uifigures over legacy figures (there are many of those as well, don't get me wrong).
Nobody should really be upset about this: we all knew that using undocumented Java in Matlab GUI carried a risk and would not last forever. It worked well for nearly 20 years (and possibly for several more years in the future). This is longer than the lifetime of most tecnologies - How long did DOS programs live before they had to be redeveloped for Windows, then again for the web, then again for mobile? In fact, Matlab's Java UI lifetime may turn out to be longer than the future lifetime of uifigures, before we will need to redevelop our GUIs yet again to fit some future new technology.
Speculating about the future, I hope that MathWorks will make a gradual transition i.e., first remove GUIDE but keep Java GUI running, then (in a subsequent release) make uifigure the default figure type but keep legacy figures running, then (in another subsequent release) remove the Java dependency in all of Matlab but still enable legacy Java-based GUIs to run using either the built-in JVM or (when they stop shipping Java) a user-installed JVM. Such gradual steps will make the transition from Java to web-based UIs much smoother over time and will allow users enough time to (re)develop the UIs without time pressure. The business benefit to MathWorks will be that users will keep upgrading their Matlab release, avoiding the debacle of R2014b in which many users stopped upgrading and stuck with 14a since the new HG2 broke (or froze) their programs. Having said that, MathWorks has many business and R&D considerations that I am not aware of, so they may decide not to follow my suggested gradual transition path: we might discover that R2020a brakes all legacy Java GUI - I hope not, but I really have no idea.
  5 Comments
Walter Roberson
Walter Roberson on 10 Nov 2021 at 0:12
Thank you for the update and analysis!

Sign in to comment.


Martin Lechner
Martin Lechner on 6 Nov 2019
Set the Icon of an uifigure
My colleague Andreas Justin had found a solution to get the webwindow of an uifigure (see the attached function). This solution don't throws the warning "Warning: The JavaFrame figure property will be removed in a future release." as the usage of mlapptools.getWebWindow(figWeb) does (see mlapptools).
This webwindows has the property Icon which can be changed by setting the path to a new icon (in Windows it must be an *.ico file).
% Web based Matlab figure (uifigure) -> webwindow is returned
figWeb = uifigure
ww = getWebWindowOfUiFigure(figWeb);
% set a new icon; on Windows systems it must be an .ico file
ww.Icon = fullfile(matlabroot, '\toolbox\matlab\toolstrip\web\image\uicontainer.ico')
Function to get the webwindow of an uifigure:
function webWindow = getWebWindowOfUiFigure(fig)
% returns the underlaying webwindow of the given figure handle in case of an uifigure, otherwise it returns an empty
% webwindow array
%
%%Example
%{
% standard Java based Matlab figure -> no webwindow is returned
fig = figure
ww = getWebWindowOfUiFigure(fig);
assert(isempty(ww))
% Web based Matlab figure (uifigure) -> webwindow is returned
figWeb = uifigure
ww = getWebWindowOfUiFigure(figWeb);
assert(numel(ww) == 1)
% change the figures icon
ww.Icon % returns the current path of the icon
% set a new icon; on Windows systems it must be an .ico file
ww.Icon = fullfile(matlabroot, '\toolbox\matlab\icons\file_open.png')
ww.Icon = fullfile(matlabroot, '\toolbox\matlab\icons\webicon.gif')
% "C:\MATLAB\R2019b\toolbox\compiler\Resources\default_icon.ico"
ww.Icon = fullfile(matlabroot, '\toolbox\matlab\toolstrip\web\image\uicontainer.ico')
%}
figNameOrig = fig.Name; % original figure name
figNameTemp = [fig.Name, tempname('$')]; % creaet a unique temporary figure name (in case of multiple figures with the same name exists
fig.Name = figNameTemp; % rename the figure in case of multiple figures with the same name exists
drawnow
ww = matlab.internal.webwindowmanager.instance.findAllWebwindows();
webWindow = matlab.internal.webwindow.empty();
for ii = 1:numel(ww)
if strcmp(ww(ii).Title, figNameTemp)
webWindow = ww(ii);
break;
end
end
fig.Name = figNameOrig; % restore orignal figure name
end

Mark Schoen
Mark Schoen on 30 Sep 2019
Edited: Mark Schoen on 30 Sep 2019
MATLAB has been working to encompass the functionality of the Swing-based framework (including "javacomponent" and "JavaFrame") by adding a whole host of new functions and features. The following link lists some of the most common features you can now access as alternatives to Java Swing:
If you have additional "javacomponent" or "JavaFrame" usage scenarios not covered in the table in the link above, please complete the survey near the top of the page (orange box) to help us better understand your needs. The survey should only take 1–2 minutes to finish.
  4 Comments
Jesse Hopkins
Jesse Hopkins on 9 Sep 2021
I'm pleased to learn that App Designer edit boxes indeed have a valid callback for each keystroke with reliable string values: ValueChangingFcn. Hooray!

Sign in to comment.


Xiangrui Li
Xiangrui Li on 3 Oct 2020
Edited: Xiangrui Li on 13 Oct 2020
Some missing functionality are added gradually. After some test with R2020b, I can conform the following:
  1. figure icon is implemented: hFig.Icon = 'fullFilename.png'
  2. uicontextmenu is there, but I have not used it yet
  3. print() won't work for fiie and clipboard export, but exportgraphics and copygraphics serve the purpose
  4. uitable allows to select cell/row by code, as well as single row selection, although not documented
  5. file/folder DnD can be done with some tricks
I just posted a simple code to do the file drag and drop to uifigure. Comments and suggestion are welcome.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by