4.5

4.5 | 6 ratings Rate this file 177 downloads (last 30 days) File Size: 16.32 KB File ID: #15580

Using java swing components in MATLAB

by Malcolm Lidierth

 

13 Jul 2007 (Updated 07 Apr 2009)

No BSD License  

A custom class that eases the use of java.awt and javax.swing components in MATLAB

Download Now | Watch this File

File Information
Description

JCONTROL provides an easy way to integrate a full range of java GUIs from the java.awt and javax.swing libraries into MATLAB.
 
Example:
obj=JCONTROL(Parent, Style);
obj=JCONTROL(Parent, Style,...
PropertyName1, PropertyValue1,...
PropertyName2, ProeprtyValue2....);
Inputs:
Parent: the handle of a MATLAB figure or other container for the resulting component
Style: string describing a java component e.g. 'javax.swing.JPanel',
        'javax.swing.JButton'

A JCONTROL object aggregates the MATLAB handle graphics container and the Java component (as returned by MATLAB's JAVACOMPONENT function) into a single object. Access to the JCONTROL's properties is provided by GET/SET calls. MATLAB dot notation may also be used. This notation also provides access to the java object's methods e.g.
pos=myobject.Position;
sz=myObject.getSize;
myobject.setEnabled(1);
myobject.setToolTipText('My tip');
myobject.setOpaque(1);

The class has been modified so that all jcontrol objects have a uipanel ancestor. This is to accommodate changes to the javacomponent.m function introduced in ML R2008b by TMW. The changes should be backwards compatible - tested with R2006a/R2008b.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
UICOMPONENT - expands uicontrol to all Java classes

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Platform independent.
Zip File Content  
Other Files
@jcontrol/ancestor.m,
@jcontrol/close.m,
@jcontrol/Contents.m,
@jcontrol/delete.m,
@jcontrol/get.m,
@jcontrol/getappdata.m,
@jcontrol/isappdata.m,
@jcontrol/jcontrol.m,
@jcontrol/private/VisibleProperty.m,
@jcontrol/README.TXT,
@jcontrol/rmappdata.m,
@jcontrol/set.m,
@jcontrol/setappdata.m,
@jcontrol/subsasgn.m,
@jcontrol/subsref.m,
jcontrolDemo.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
15 Jul 2007 Yair Altman

Excellent submission. Simple, easy to understand, richly documented and very effective. A great source of knowledge and examples for anyone interested in Java-Matlab integration as well as in object-oriented Matlab programming. This submission is one of, if not the, best that I've seen in fully utilizing Matlab's class properties (with some undocumented Matlab functionality), subscripted referencing & assignment, and method overloading. Aside from class methods which are missing here, this is a great example for Matlab OOP.

18 Jul 2007 Nick Clark

Wonderful work! The author emailed me with a fix to get this working if you're a 7.0.1 user. See the following:

*In your copy of MATLAB's javacomponent.m , change lines 66-68 to this:
if isa(handle(parent), 'figure') || isa(handle(parent), 'uicontainer')
    parentIsFigure = true;
    peer = get(ancestor(parent, 'figure' ), 'JavaFrame');

*In jcontrolDemo:
Comment out lines 46-48
and save/run it

13 Aug 2007 Mathias Ortner

I like this work : however I got a problem when trying the demo,

here is my udate of the javacomponent.m that made it work

if ( isa(hParent, 'figure') || ...
     isa(hParent, 'uicontainer') || ...
     isa(hParent, 'uiflowcontainer') || ...
     isa(hParent, 'uigridcontainer') ||...
     isa(hParent, 'hgjavacomponent'))
    parentIsFigure = true;
    peer = get(ancestor(parent,'figure'),'JavaFrame');
elseif isa(hParent, 'uitoolbar')
    peer = get(parent,'JavaContainer');
    if isempty(peer)
        drawnow;
        peer = get(parent,'JavaContainer');
    end
else
        error('MATLAB:javacomponent:InvalidParentHandle', 'Invalid parent handle\n%s', usage)
end

27 Nov 2008 Thierry Dalon

I could not find any demo in the package.(jControldemo missing?)

05 Dec 2008 Malcolm Lidierth

Thierry. Many thanks. Demo file now restored to zip.

07 Apr 2009 Jveer

way too much effort required to integrate within guide created GUIs. can't be asked.

27 Aug 2009 Ritesh Sood

I have only run the demo so far (which worked well), but it's surely a very useful contribution and I would like to thank the author for the same.

Although I haven't programmed awt/swing, or even much java, I recently wrote a fairly complex gui in perl-Gtk2, and I've some experience in gui programming.

My first question is if it's possible to have tabbed panes like we do in web browsers and a number of other guis? I don't think Matlab has it natively, and I'm sure swing has it. So how about our jobjects?

Second, one very desirable (even essential) feature in gui toolkits is widget packing. One programs the relative layout of the widgets and the toolkit computes the absolute positions and dimensions. I mean why should I be bothered with the arithmetic, let the computer do the math. Now that we can access swing via jobjects, is it possible to do automatic widget packing?

Thanks for your help!

06 Oct 2009 Malcolm Lidierth

PS That should be uitab + uitabgroup

06 Oct 2009 Malcolm Lidierth

Ritesh
See the undocumented MATLAB uitab and uitabgroup functions for tabbed panes
For widgets, I may have misunderstood. Jcontrols can be positioned using normalized units 0 to 1. MATLAB then does the work.

17 Nov 2009 Jose Miguel Ramon

Great work! I have a question about the problems with the containers in R2008b onwards. As you comment before, hgjavacomponent may not parent other hgjavacomponent. Does this mean I cannot use Jframe as parent for my Javacomponents in R2008b onwards?

Therefore the only one way to contain JavaComponents is a figure, isn’t it? Is there any restriction in terms of including Javacomponent if I use a figure as parent? Can I build complex GUI’s using figure as parent?

Thanks in advance

17 Nov 2009 Malcolm Lidierth

Jose
You will get Java exceptions if you try to create a JFrame ('adding a window to a container') so jcontrol does not allow that.

You can however, build very complex GUIs using a MATLAB figure or uipanel as parent and I hope to include tabbed panes once TWM provide full support for these. An advantage of using MATLAB containers is that you can mix and match jcontrols and MATLAB components including graphics in a single figure/panel.

Apart from using a java window, there are few restrictions. You can for example create a uipanel, add a swing JPanel to it, populate that with a JScrollPane and add a JTree to it along with a set of JButtons in the JPanel.
Regards
Malcolm

Please login to add a comment or rating.
Updates
21 Aug 2007

Fixes in subsref and setappdata.
Ancestor method added

26 Sep 2007

Now accepts pre-constructed java objects as input to jcontrol constructor.
Subsref speed substantially improved

21 Oct 2008

Modified for R2008b compatibility

11 Nov 2008

Fix performance when uipanel given explicitly as parent (a further R2008b compatibility update)

26 Nov 2008

Improved delete

05 Dec 2008

Restore jControlDemo to zip

07 Apr 2009

Background changes for compatability with R2009a javacomponent.m

Tag Activity for this File
Tag Applied By Date/Time
data exploration Malcolm Lidierth 22 Oct 2008 09:19:19
java Malcolm Lidierth 22 Oct 2008 09:19:20
swing Malcolm Lidierth 22 Oct 2008 09:19:20
awt Malcolm Lidierth 22 Oct 2008 09:19:20
graphics Malcolm Lidierth 22 Oct 2008 09:19:20
gui Malcolm Lidierth 22 Oct 2008 09:19:20
uicontrol Malcolm Lidierth 22 Oct 2008 09:19:20
awt koubaa kamal 28 Nov 2008 04:41:16
data exploration koubaa kamal 28 Nov 2008 04:41:23
gui Cristina McIntire 05 Dec 2008 14:34:04
graphics Cristina McIntire 05 Dec 2008 14:34:04
graphics Rodolphe Sitter 07 Dec 2008 08:04:10
gui Azim 09 Dec 2008 23:43:57
graphics Azim 09 Dec 2008 23:44:07
undocumented Yair Altman 26 Feb 2009 17:50:17
graphics James Destine Augustin 10 Mar 2009 19:05:11
undocumented Malcolm Lidierth 07 Apr 2009 12:36:42
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com