MATLAB Answers

Bruno Luong
3

deprecated javacomponent and javaframe

Asked by Bruno Luong
on 12 Sep 2019
Latest activity Answered by Martin Lechner on 6 Nov 2019 at 10:47
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

This discussion needs to be understood in the context that Oracle made big changes to Java licensing that make it expensive to continue to use Java. In theory each customer needs to license Java per desktop (and probably per VM) for all new Java releases, so Mathworks is effectively frozen in Java release, and the older releases are now only getting critical security patches.
You will find that a lot of vendors are getting rid of java. Oracle broke the market.
There were other concerns about Java, so there had been grumbling about it anyhow, but mostly not enough to push people off it except some high performance markets. The licensing change was the push that forced the issue.
I thought uihtml in R2019b may make the file drag-n-drop easier. There are many example of code, like this one, working for web page. But when I tried it on uifigure, it won't accept the drop at all. Anyone knows if there is a switch to turn the drop on somewhere?
Edit: Here is the minimum code I tested:
<div id="dropZone" style="width: 120px; height: 100px; border: 2px solid red">Drop a file here</div>
<div id="textZone" style="width: 360px; height: 40px"></div>
<script type="text/javascript">
var dropZone = document.getElementById('dropZone');
dropZone.addEventListener('dragover', function(e) {e.preventDefault();});
dropZone.addEventListener('drop', function(e) {
e.preventDefault();
document.getElementById('textZone').innerHTML = e.dataTransfer.files[0].name;
});
</script>
Save the above as DnD_test.html into Matlab pwd. Open it in web browser, and drag and drop a file from OS file manager into the red box. The file name will show up, indicating DnD works.
In Matlab (R2019b+), load the html into a uifigure:
fig = uifigure('Position', [200 200 380 200]);
h = uihtml(fig, 'Position', [10 10 360 180]);
h.HTMLSource = './DnD_test.html';
When dragging a file into the box at uifigure, no-DnD symbol shows up.
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.

3 Answers

Answer by 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.

  1 Comment

Yair: "For example, uifigure [still] do not have toolbars, toolstrips, customizable icon, or non-pixel units (esp. normalized). "
UICONTEXTMENU is missing in UIFIGURE and AFAIK there is no easy workaround.
All that makes us still develop with GUIDE.

Sign in to comment.


Answer by Mark Schoen on 30 Sep 2019
Edited by 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.

  2 Comments

As Yair stated above, the main drawback of the new uifigures and apps is that there are no possiblities to add custom mouse/keyboard actions. The appdesigner already supports a context menu but a normal Matlab user doesn't have this possibility.
I think if Mathworks would use the appdesigner for developing all their GUI's without addionally tricks and enhancements the "normal Matlab user" would also be happy.
The above mentioned survey is more or less about missing components but there is still missing a lot of functionallity as state above. Mathworks should define what they have in minde to replace still missing features from the old style Java based GUI's (e.g. will be provieded in one of the next releases, not supported anymore).
For me very important features und functions are:
  • mouse/keyboard/action events
  • uicontextmenu
  • no accellerators on buttons or other GUI components (only menu supports accelerators)
  • customisation of the figure icon
Bruno Luong
on 22 Oct 2019 at 6:26
Completely agree with Martin. Those four missing listed features prevent me to make transition to AppDesigner.
Incompatible between AppDesigner figure and normal figure is also a headache to handle. In GUI users do not need to care about what type of figure he/she plots.

Sign in to comment.


Answer by Martin Lechner on 6 Nov 2019 at 10:47

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

  0 Comments

Sign in to comment.