Open Dev-iL opened 8 years ago
A caveat to this conversion is that the GUIs will still likely require R2014b or newer. The App Designer makes extensive use of dot notation to access properties of UI objects rather than set
or get
. A warning to this effect will be included for users running mlapp2classdef
in MATLAB versions prior to R2014b.
Though a fun project idea on its own, creating a parser to robustly handle these compatibility issues is beyond the scope of this project.
Another consideration is that a lot of the object properties and callbacks are new to R2016a, which isn't something that can be reliably caught without significant effort
For posterity: it may be possible to convert some App Designer elements to Java Swing components, e.g.:
%% List of JComponents:
% https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html
%% Spinner - https://docs.oracle.com/javase/8/docs/api/javax/swing/JSpinner.html
jSp = javaObjectEDT('javax.swing.JSpinner');
% Get available methods & fields:
M = methods(jSp);
F = fields(jSp); % fields often contain the allowable settings for methods.
% Set some properties:
jSp.setAlignmentX(jSp.LEFT_ALIGNMENT);
% Display component
[jSpinner, hSpContainer] = javacomponent(jSp,'South');
% https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html
%% Scrollbar (example based on Altman-2012)
% Create and initialize a JScrollBar object
jSb = javaObjectEDT('javax.swing.JScrollBar');
% Set some properties:
jSb.setOrientation(jSb.HORIZONTAL);
% Display component
[jScrollbar, hSbContainer] = javacomponent(jSb,'North');
Here's a better demo of how to make a spinner and attach a callback to it:
function hFig = spinnerDemo
%% javacomponent "docs": Delete lines 2-3 from javacomponent.m to get access to docs using F1 (help browser)
%% Spinner - https://docs.oracle.com/javase/8/docs/api/javax/swing/JSpinner.html
hFig = figure();
% Create a spinner (w/o model):
jJSpinnerNM = javacomponent(javax.swing.JSpinner,[50,40,80,30],hFig);
jJSpinnerNM.StateChangedCallback = @(hObject,eventdata)onSpinnerValueChanged(hObject,eventdata);
% Create a spinner (w/ model):
spModel = javax.swing.SpinnerListModel({'item1','item2','item3'});
% https://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerModel.html
jJSpinnerM = javacomponent(javax.swing.JSpinner(spModel),[50,80,80,30],hFig);
jJSpinnerM.StateChangedCallback = @(hObject,eventdata)onSpinnerValueChanged(hObject,eventdata);
%% Get available methods & fields, and set some properties:
M = methods(jJSpinnerM);
F = fields(jJSpinnerM); % fields often contain the allowable settings for methods.
% Set some arbitrary property:
jJSpinnerNM.getEditor.getTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
% https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html
jJSpinnerM.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
% Crazier demo: http://www.mathworks.com/matlabcentral/fileexchange/26970-spinnerdemo
end
function onSpinnerValueChanged(hObject, eventdata)
disp(['Spinner value is now: ' num2str(hObject.Value)]);
end
For the record, HG1 (R2014a and older) also supports dot-notation, if you simply ensure that all the relevant GUI/graphic handles are handle() objects rather than numeric. For example, the following code runs ok on HG1:
hFig = handle(gcf); % trick to enable dot-notation on GUI/graphic objects
hFig.Color = 'w';
hFig.CloseRequestFcn = @myCallback;
The nice thing about this is that it's fully backward-compatible, meaning that hFig=handle(gcf)
is transparent to HG2 and meaningful to HG1, so it can be used in Matlab code on both HG1 and HG2.
Whoa, I didn't know that, very interesting! When I get some time this week I'll poke around and test out some changes. Haven't looked at this in a while...
It actually even improves performance in some cases: https://undocumentedmatlab.com/blog/performance-accessing-handle-properties
It is no secret that designing a UI in App Designer is much more convenient (one might even say fun) than using GUIDE. However, this comes at the price of lower customizability among other things.
It would be very useful to be able to design a UI in App Designer and then export it using "regular" elements (
uicontrol
).This functionality can conceptually be achieved by mapping various App Designer Component names to their corresponding
uicontrol
/figure
/axes
counterparts.