Built-In MATLAB
®
Widgets and
Java Classes
5
Chapter
K13163_Book.indb 241 11/8/2011 8:07:59 PM
Undocumented Secrets of MATLAB
®
-Java Programming242
5.1 Internal MATLAB Java Packages
MATLAB has several Java packages (com.mathworks.*) which contain Swing-extended com-
ponents and resources (icon images), which are used internally by the MATLAB application and
can also be used in user applications. All these components reside in packages that have the com.
mathworks domain prex. The packages are grouped based on utility and topic and are all located
in the %matlabroot%/java/jar/ folder in separate JAR (Java archive zip) les.
These Java archive
les are all included in the static Java classpath, within the classpath.txt le. This means that all
internal MATLAB classes are immediately accessible from within our MATLAB m-code.
Important warning: All classes included in MATLAB packages, their internal function-
alities, and even the packages themselves and the location of their containing JAR les,
are entirely unsupported and are prone to change without any warning between MATLAB
releases. Extensive use of exception handling must, therefore, be done in any code which uses
these classes/packages. The description in this chapter focuses on release R2008a (7.6); other
releases may well vary.
Note that in all the ensuing discussions, MATLABs internal classes are not hacked or
decompiled an act which could violate MATLABs license agreement. Instead, we shall
only use commands available from within MATLAB or snippets copied from openly accessible
MATLAB m-code les.
When asked about these packages, Mike Katz said the following on the ofcial MATLAB
Desktop blog (http://blogs.mathworks.com/desktop):
1
com.mathworks.* represent Java classes that we’ve built here. These mostly represent the
libraries and widgets used by the Desktop and toolboxes, and as such are not considered part
of the MATLAB language and are largely [un]documented. Occasionally we mention some of
these in technical support solutions as workarounds to bug xes. Some of the classes are docu-
mented for use for xml, web, and database I/O, and a whole bunch are meant for use in with the
MATLAB Builder JA product for deploying MATLAB code to work with Java programs.
5.1.1 Inspecting Package Contents
Since jar les are simple zip les, they can be inspected using any zip client application, includ-
ing MATLABs standard unzip command.
For example,
zipFilename = fullfile(matlabroot,'/java/jar/mwswing.jar');
classFilenames = unzip(zipFilename, outputFolder);
This will spill all of mwswing.jars les (which are the requested classes and some static
resources such as icons) into a tree structure in outputFolder (i.e., outputFolder/com/math-
works/mwswing/. . .) and return a cell-array of the le paths.
For example, C:\Program Files\MATLAB\R2008b\java\jar\mwswing.jar.
Matlab’s built-in unzip.m itself uses standard Java zip-processing classes and methods.
K13163_Book.indb 242 11/8/2011 8:07:59 PM
243Built-In MATLAB
®
Widgets and Java Classes
However, although it may help our understanding, we do not really need the physical class
les. We only need to know which les are available in the mwswing package. So, let us use
only a small segment of unzip.m:
>> zipJavaFile = java.io.File(zipFilename);
>> zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
>> files = zipFile.getEntries
files =
java.util.Hashtable$Enumerator@e4bf0e
>> while files.hasMoreElements, disp(files.nextElement.getName); end
com/mathworks/mwswing/MJMenu$1.class
com/mathworks/mwswing/MJOptionPane$ButtonAction.class
com/mathworks/mwswing/MJMultilineRadioButton.class
com/mathworks/mwswing/MJToolBar$Gap.class
com/mathworks/mwswing/MJMenu$MWindowsMenuUI.class
com/mathworks/mwswing/ScrollablePopupList$PopupDismisser.class
com/mathworks/mwswing/SelectAllOnFocusListener$1$1.class
com/mathworks/mwswing/MJScrollStrip.class
com/mathworks/mwswing/dialog/MJGotoDialog.class
...
com/mathworks/mwswing/MJToggleButton.class
com/mathworks/mwswing/MJSlider.class
com/mathworks/mwswing/MJSpinner.class
...
Remember to close the zipFile after reading is done, in order to free resources (this is done
by unzip.m in its cleanup phase):
>> zipFile.close(); % close file even if exception happens!
Note that the reverse operation, namely nding the source JAR of a specic Java object, can
be obtained using standard Java reection. For example, let us nd the JAR source of the Java
object that underlies a simple pushbutton uicontrol:
>> hButton = uicontrol('String','Yair');
>> jButton = java(findjobj(hButton))
jButton =
com.mathworks.hg.peer.PushButtonPeer$1[. . .]
>> jButton.getClass.getProtectionDomain.getCodeSource
ans =
(file:/C:/Program%20Files/MATLAB/R2010b/java/jar/hg.jar . . .)
5.1.2 Inspecting an Internal MATLAB Class
We are now faced with two important questions: Which of the internal MATLAB packages and
classes are useful, and how can we use them?
The rst question can be answered to a small degree by simple inspection of the le names.
It is pretty obvious that com.mathworks.mwswing.MJToggleButton implements a
K13163_Book.indb 243 11/8/2011 8:07:59 PM

Get Undocumented Secrets of MATLAB-Java Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.