How do I access a Java inner class from Matlab?
显示 更早的评论
As described in this MathWorks support node, it is not straightforward to access inner Java classes from Matlab...
采纳的回答
更多回答(1 个)
Yair Altman
2012-3-15
You don't need to create new instances, and in fact in some cases the class may not allow you to create a new instance. Here are alternative tricks from section 1.7 of my Matlab-Java book:
First create a tray-icon object (the outer class object):
myIcon = fullfile(matlabroot,'toolbox/matlab/icons/matlabicon.gif');
iconImage = java.awt.Toolkit.getDefaultToolkit.createImage(myIcon);
trayIcon = java.awt.TrayIcon(iconImage, 'initial tooltip');
Now access the TrayIcon.MessageType inner class:
>> trayIconClasses = trayIcon.getClass.getClasses;
>> trayIconClasses(1)
ans =
class java.awt.TrayIcon$MessageType <= hurray!!!
>> MessageTypes = trayIconClasses(1).getEnumConstants
MessageTypes =
java.awt.TrayIcon$MessageType[]:
[java.awt.TrayIcon$MessageType] <= 1: ERROR
[java.awt.TrayIcon$MessageType] <= 2: WARNING
[java.awt.TrayIcon$MessageType] <= 3: INFO
[java.awt.TrayIcon$MessageType] <= 4: NONE
We can also access Java enums using their built-in values() and valueOf() methods:
>> msgType=javaMethod('valueOf','java.awt.TrayIcon$MessageType','INFO')
msgType =
INFO <= a java.awt.TrayIcon$MessageType object
>> enums = cell(javaMethod('values','java.awt.TrayIcon$MessageType'));
>> msgType = enums{3}; % alternative way to find the INFO enum value
>> cellfun(@(c)c.toString.char, enums, 'uniform',false)'
ans =
'ERROR' 'WARNING' 'INFO' 'NONE'
More information about using system-tray icons in Matlab can be found in:
类别
在 帮助中心 和 File Exchange 中查找有关 Call Java from MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!