File Exchange

image thumbnail

UICOMPONENT - expands uicontrol to all Java classes

version 1.8 (59.6 KB) by

Enhanced replacement for uicontrol & javacomponent, accepting all Java (Swing/AWT) style components

23 Downloads

Updated

View License

UICOMPONENT is intended as a direct replacement of Matlab's built-in UICONTROL and JAVACOMPONENT functions. It accepts all parameters and styles that UICONTROL accepts, as well as any other displayable Java (Swing/AWT) component class. The calling convention and syntax of UICONTROL were preserved for full backwards compatibility.

UICOMPONENT uses the built-in UICONTROL whenever possible (i.e, for all the standard styles), and uses JAVACOMPONENT to create and display all the other java classes. UICOMPONENT supports the entire Swing/AWT and any user-defined classes, in several equivalent case-insensitive formats. For example, 'Spinner', 'spiNNer', 'JSpinner' and 'javax.swing.jspinner' are all equivalent and recognized.
Notable new styles that are now available (unavailable in built-in UICONTROL): spinner, slider, editable comboBox, passwordField, tree, table, fileChooser, colorChooser, progressBar, applet. But there are so many more - whatever is available in Java is now seamlessly available in your Matlab GUI.

Examples (more examples in attached screenshot & in the help comment):
- uicomponent('style','jspinner','value',7); % simple spinner with initial value
- uicomponent('style','javax.swing.jslider','tag','myObj'); % simple horizontal slider
- uicomponent('style','JComboBox',{1,pi,'text'},'editable',true); % editable drop-down
The screenshot can be recreated using the createScreenshot script that is included in this submission. It is provided to illustrate the use of UICOMPONENT for various different components.

Callbacks:
Over 30 callback hooks are exposed to the user for all components (the exact list depends on the specific java component used). These callbacks include mouse movement/clicks, keyboard events, focus gain/loss, data changes etc.

Warning:
This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!

Technical description:
http://undocumentedmatlab.com/blog/javacomponent

Change log:
See the bottom of this page
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Comments and Ratings (40)

Jakob Thumm

Guojin Feng

Many thanks for this toolbox. It's excellent and very helpful for making GUI in Matlab.
As a note on customising spinner, for example in floating point. Here is an example, which takes me a while to find out.
-------------------------
jModel = javax.swing.SpinnerNumberModel(0.5, 0.1, 1, 0.1); % value, min, max, step size
hspin = uicomponent('style','jspinner', jModel); % spinner with model specified
set(hspin ,'units','norm', 'position', [0.2,0.2,0.05,0.03]);
set(hspin ,'StateChangedCallback',{@spinner_Callback, 1}); % with argument 1 specified

function spinner_Callback(jSpinner,jEventData, state)

% Your code for the callback function

Gary Wolf

This is a really nice tool, and pretty straightforward to use. Thanks!!
I've been using the filechooser style, though I can't seem to be able to change the initial working directory. It seems to require a "sun.awt.shell.Win32ShellFolder2" class object, not sure how to do that.

Great upgrade Yair. Thank you. Is there any way you can add support for HG2 uicontainer subclasses by checking isa(hParent,'matlab.ui.container.Container')? This is an undocumented class, but it will enable us to use your toolbox in conjunction with GUI layout toolbox. This would be a perfect combination.

Amir Goren

Problem is with line 470:

elseif ~any(strcmpi(regexprep(class(hParent),'.*\.',''),{'figure','uicontainer','uiflowcontainer','uigridcontainer'}))

Should be:

elseif ~any(strcmpi(regexprep(class(hParent),'.*\.',''),{'figure','uicontainer','uiflowcontainer','uigridcontainer', 'panel'}))

Very useful and works fine with HG1 and HG2. A question how can I change BackGroundColor ForeGroundColor FontSize FontStyle... Such properties are not directly recognized through standard name/value pairs.
Many Thanks to Yair

Tommie Heard

It did fix the error, but the Layout.Cell(1,3) object is not visible on the figure. Any idea why.
Thanks for the input.

Yair Altman

Yair Altman (view profile)

Your specified parent handle is probably wrong in some way. Try using gcf instead of Layout.Cell(1,3) to verify this.

Tommie Heard

handles.txtSeqFile = uicomponent( ...
'style','javax.swing.JTextField', ...
'parent',Layout.Cell(1,3), ...
'Tag', 'txtSeqFile', ...
'MouseClickedCallback',{@(handle,event)xMouseClick(handle,1)});



%----error after executing above line----

Error using javacomponentdoc_helper>applyArgumentsAndShow (line 197)
Invalid input

Error in javacomponentdoc_helper (line 35)

Error in javacomponent (line 111)
[hcomponent, hcontainer] = javacomponentdoc_helper(varargin{:});

Error in uicomponent (line 197)
[jcomp, hcontainer] = javacomponent(javaComp,position,parent);

What is causing this????

Dan

Dan (view profile)

Thanks!!

Yair Altman

Yair Altman (view profile)

Fixed - download the latest and retry.

Dan

Dan (view profile)

Thanks for this great tool, Yair.

I have problems using the latest version, though: In both HG (2014a) and HG2 (2014b), i get an error using the following syntax, which worked before:

h.fig = figure();
h.menu = uicomponent('Parent',h.fig,'style','JComboBox',{'A','B','C'},'editable',true);
h.menu.JavaComponent.getSelectedItem

error is: Attempt to reference field of non-structure array.

Do I have to change the syntax in the new version?

Thanks, Daniel

Tom

Tom (view profile)

Thanks a lot Yair!

Yair Altman

Yair Altman (view profile)

@Tom - I've updated the utility for R2014b. Download the latest version. It should be backward compatible with earlier Matlab releases as well.

Tom

Tom (view profile)

Great submission. Getting errors when using with R2014b with all the graphics changes in that release. Any plans to update to support that release?

Amihay Blau

Wow!
A simple and amazing way to updgrade the UI.

Thank you

Amihay Blau

Wow!
A simple and amazing way to updgrade the UI.

Thank yoy

Pooya89

Caleb

Caleb (view profile)

Okay, I figured out the filter and the 'Open' and 'Cancel' buttons, but still can't figure out the icons.

Caleb

Caleb (view profile)

Thank you so much for this submission; I'm finding that it's very powerful and well thought out. I was wondering if someone could help me, though. I'm trying to create my own file chooser so that I'm able to add some check boxes to it. I've got that part down, but I'm not sure how to correctly create my own file filter, add callbacks to the 'Open' and 'Cancel' push buttons, and add icons to the buttons to the right of the directory selection drop down menu. I've tried setting the 'ActionPerformedCallback' property, but for some reason the callback function is only called when I click the 'Cancel' button.

Gaurav Gupta

Awesome job, but can u please tell me why uicontrol objects not hide when their position is out of parent uipanel area.

Isaac

Isaac (view profile)

It may not be obvious to the novice, but one experienced builder appreciates having access to all the "stuff" in a uniform interface. YMMV. (Thanks, Yair!)

Isaac

Isaac (view profile)

B_Richardson

Can someone explain to a novice GUI builder the significance of this download?

Chris Rodgers

Great idea!

shrey sinha

Evgeny Pr

Evgeny Pr (view profile)

It is fine!
I knew that in Matlab it is possible to do such, but you have made it very well! Thanks!

Hans

Hans (view profile)

Thanks for sharing this code with us, Yair. Whenever I need a java thingy that is not available in MATLAB, I use uicomponent.

Jveer

Jveer (view profile)

can these components be made transparent for e.g overlaying a transparent ticklabeled slider over a colorbar?

Jveer

Jveer (view profile)

Jveer

Jveer (view profile)

the best gui related submission yet

Jax Cao

Useful tool

Qun Han

Great!

Dawn Song

Oh, sorry, I mistyped, I meaned ! not ?

Dawn Song

Great TOOl ?

Heiko Marx

After changing line 256 from strcmpi to strncmpi, because I use 'Visible' instead of 'vis', this excellent tool has made it to my daily use. Thank you very much!

Malcolm Lidierth

Excellent. Produces great GUIs that work on both Windows and Mac without needing tweaking for each platform.

Arwel Hughes

This is an excellent tool. It works very well with a number of components I've tried.

Matthew Whitaker

This is really fascinating. Note your example 1 and 2 should read:
uicomponent('Style','edit','String','hello');

and
uicomponent('Parent', fig2, 'Style', 'edit','String','hello');

instead of 'uicontrol'

I have not found any obvious issues although with the extensive use of non-documented features I'll have to test more extensively but this is a really nice wrapper for uicontrol and javacomponent. A lot of goodies in the code for anyone interested in incorporating Swing components into their interfaces.

Updates

1.8

Added support for all HG2 containers (inc. uiextras) as parents, per suggestion by D. Sampson

1.7

Added missing Java files to the zip file; clarified about the createScreenshot script in main description text.

1.6

Added tabs and panels as acceptable parent containers per suggestion by Amir G.

1.2

Fixed a bug in setting the JavaComponent field

1.1

Fixed for R2014b (HG2)

Handle pre-existing Java components

Set large initial size for *Chooser classes; fixed Window subclasses issue; fixed help comment; enabled non-figure parent; enabled non-cell ctorArgs; set default Tag prop

MATLAB Release
MATLAB 7.4 (R2007a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video