WARNING: This feature is not supported in MATLAB and the API and functionality may change in a future release. JAVACOMPONENT Create a Java AWT Component and put it in a figure JAVACOMPONENT(COMPONENTNAME) creates the Java component specified by the string COMPONENTNAME and places it in the current figure or creates a new figure if one is not available. The default position is [20 20 60 20]. NOTE: This is a thread safe way to create and embed a java component in a figure window. For more thread safe functions to create and modify java components, see AWTCREATE, AWTINVOKE. JAVACOMPONENT(HCOMPONENT) places the Java component HCOMPONENT in the current figure or creates a new figure if one is not available. The default position is [20 20 60 20]. JAVACOMPONENT(..., POSITION, PARENT) places the Java component in the specified PARENT at position POSITION. PARENT can be a Figure or a Uipanel. POSITION is in pixel units with the format [left, bottom, width, height]. JAVACOMPONENT(..., CONSTRAINT, PARENT) places the Java component next to the figure's drawing area using CONSTRAINT. CONSTRAINT that can be NORTH, SOUTH, EAST, OR WEST placement - following Java AWT's BorderLayout rules. The handle to the Java component is returned on success, empty is returned on error. If the parent is a uipanel, the component is placed in the parent figure of the uipanel. If parent is a uitoolbar handle, CONSTRAINT is ignored and the component is placed last in the child list for the given toolbar. [HCOMPONENT, HCONTAINER] = JAVACOMPONENT(...) returns the handle to the Java component in HCOMPONENT and its HG container in HCONTAINER. HCONTAINER is only returned when pixel positioning is used. It should be used to change the units, position, and visibility of the Java component after it is added to the figure. Examples: f = figure; b = javacomponent('javax.swing.JButton'); % Thread safe creation set(b,'ActionPerformedCallback','disp Hi!'); f = figure('WindowStyle', 'docked'); b1 = awtcreate('javax.swing.JButton', 'Ljava.lang.String;', 'Hi!'); set(b1,'ActionPerformedCallback','disp Hi!'); javacomponent(b1); f = figure; comp = awtcreate('javax.swing.JSpinner'); [comp, container] = javacomponent(comp); set(container,'Position', [100, 100, 100, 40]); set(container,'Units', 'normalized'); f = figure; p = uipanel('Position', [0 0 .2 1]); ppos = getpixelposition(p); tr = awtcreate('javax.swing.JTree'); [tree treecontainer] = javacomponent(tr, [0 0 ppos(3) ppos(4)], p); f = figure('WindowStyle', 'docked'); ta = awtcreate('javax.swing.JTable', 'II', 3, 10); table = javacomponent(ta, java.awt.BorderLayout.SOUTH, f); f = figure; tb = uitoolbar(f); b2 = awtcreate('javax.swing.JButton', 'Ljava.lang.String;', 'Hi again!'); b2 = javacomponent(b2, [], tb); % Note: Position is ignored. set(b2,'ActionPerformedCallback','disp(''Hi again!'')'); See also USEJAVACOMPONENT, AWTCREATE, AWTINVOKE Copyright 1984-2006 The MathWorks, Inc. $Revision: 1.13 $ $Date: 2008/03/31 10:27:39 $ Adapted for LTPDA $Id: ltpda_javacomponent.html,v 1.13 2008/03/31 10:27:39 hewitson Exp $
0001 function [hcomponent, hcontainer] = ltpda_javacomponent(component, position, parent) 0002 % WARNING: This feature is not supported in MATLAB 0003 % and the API and functionality may change in a future release. 0004 % 0005 % JAVACOMPONENT Create a Java AWT Component and put it in a figure 0006 % 0007 % JAVACOMPONENT(COMPONENTNAME) creates the Java component specified by the 0008 % string COMPONENTNAME and places it in the current figure or creates a new 0009 % figure if one is not available. The default position is [20 20 60 20]. 0010 % NOTE: This is a thread safe way to create and embed a java component in a 0011 % figure window. For more thread safe functions to create and modify java 0012 % components, see AWTCREATE, AWTINVOKE. 0013 % 0014 % JAVACOMPONENT(HCOMPONENT) places the Java component HCOMPONENT in the 0015 % current figure or creates a new figure if one is not available. The 0016 % default position is [20 20 60 20]. 0017 % 0018 % JAVACOMPONENT(..., POSITION, PARENT) places the Java component in the 0019 % specified PARENT at position POSITION. PARENT can be a Figure or a 0020 % Uipanel. POSITION is in pixel units with the format [left, bottom, width, 0021 % height]. 0022 % 0023 % JAVACOMPONENT(..., CONSTRAINT, PARENT) places the Java component next to 0024 % the figure's drawing area using CONSTRAINT. CONSTRAINT that can be NORTH, 0025 % SOUTH, EAST, OR WEST placement - following Java AWT's BorderLayout rules. 0026 % The handle to the Java component is returned on success, empty is 0027 % returned on error. If the parent is a uipanel, the component is placed in 0028 % the parent figure of the uipanel. If parent is a uitoolbar handle, 0029 % CONSTRAINT is ignored and the component is placed last in the child list 0030 % for the given toolbar. 0031 % 0032 % [HCOMPONENT, HCONTAINER] = JAVACOMPONENT(...) 0033 % returns the handle to the Java component in HCOMPONENT and its HG 0034 % container in HCONTAINER. HCONTAINER is only returned when pixel 0035 % positioning is used. It should be used to change the units, position, 0036 % and visibility of the Java component after it is added to the figure. 0037 % 0038 % 0039 % Examples: 0040 % 0041 % f = figure; 0042 % b = javacomponent('javax.swing.JButton'); % Thread safe creation 0043 % set(b,'ActionPerformedCallback','disp Hi!'); 0044 % 0045 % f = figure('WindowStyle', 'docked'); 0046 % b1 = awtcreate('javax.swing.JButton', 'Ljava.lang.String;', 'Hi!'); 0047 % set(b1,'ActionPerformedCallback','disp Hi!'); 0048 % javacomponent(b1); 0049 % 0050 % f = figure; 0051 % comp = awtcreate('javax.swing.JSpinner'); 0052 % [comp, container] = javacomponent(comp); 0053 % set(container,'Position', [100, 100, 100, 40]); 0054 % set(container,'Units', 'normalized'); 0055 % 0056 % f = figure; 0057 % p = uipanel('Position', [0 0 .2 1]); 0058 % ppos = getpixelposition(p); 0059 % tr = awtcreate('javax.swing.JTree'); 0060 % [tree treecontainer] = javacomponent(tr, [0 0 ppos(3) ppos(4)], p); 0061 % 0062 % f = figure('WindowStyle', 'docked'); 0063 % ta = awtcreate('javax.swing.JTable', 'II', 3, 10); 0064 % table = javacomponent(ta, java.awt.BorderLayout.SOUTH, f); 0065 % 0066 % f = figure; 0067 % tb = uitoolbar(f); 0068 % b2 = awtcreate('javax.swing.JButton', 'Ljava.lang.String;', 'Hi again!'); 0069 % b2 = javacomponent(b2, [], tb); % Note: Position is ignored. 0070 % set(b2,'ActionPerformedCallback','disp(''Hi again!'')'); 0071 % 0072 % See also USEJAVACOMPONENT, AWTCREATE, AWTINVOKE 0073 % 0074 % Copyright 1984-2006 The MathWorks, Inc. 0075 % $Revision: 1.13 $ $Date: 2008/03/31 10:27:39 $ 0076 % 0077 % Adapted for LTPDA 0078 % 0079 % $Id: ltpda_javacomponent.html,v 1.13 2008/03/31 10:27:39 hewitson Exp $ 0080 % 0081 0082 if (usejavacomponent == 0) 0083 err.message = 'JAVACOMPONENT is not supported on this platform'; 0084 err.identifier = 'MATLAB:javacomponent:FeatureNotSupported'; 0085 error(err); 0086 end 0087 0088 if ~isempty(nargchk(1,3,nargin)) 0089 error('MATLAB:javacomponent',usage); 0090 end 0091 0092 if nargin < 3 0093 parent = gcf; 0094 end 0095 0096 if nargin < 2 0097 position = [20 20 60 20]; 0098 end 0099 0100 parentIsFigure = false; 0101 hParent = handle(parent); 0102 if ( isa(hParent, 'figure') || ... 0103 isa(hParent, 'uicontainer') || ... 0104 isa(hParent, 'uiflowcontainer') || ... 0105 isa(hParent, 'uigridcontainer')) 0106 parentIsFigure = true; 0107 peer = get(ancestor(parent,'figure'),'JavaFrame'); 0108 elseif isa(hParent, 'uitoolbar') 0109 peer = get(parent,'JavaContainer'); 0110 if isempty(peer) 0111 drawnow; 0112 peer = get(parent,'JavaContainer'); 0113 end 0114 else 0115 error('MATLAB:javacomponent:InvalidParentHandle', 'Invalid parent handle\n%s', usage) 0116 end 0117 0118 if isempty(peer) 0119 error('MATLAB:javacomponent:JavaFigsNotEnabled', 'Java figures are not enabled') 0120 end 0121 0122 hUicontainer = []; 0123 hgp = []; 0124 returnContainer = 1; 0125 0126 if ischar(component) 0127 component = awtcreate(component); 0128 end 0129 0130 % promote the component to a handle object first. 0131 % It seems once a java object is cast to a handle, you cannot get another 0132 % handle with 'callbackproperties'. So, do want you need outright and use 0133 % the same handle in the subsequent code. 0134 if ~isjava(component) 0135 component =java(component); 0136 end 0137 hcomponent = handle(component,'callbackProperties'); 0138 0139 if nargin == 1 0140 hgp = handle(peer.addchild(component)); 0141 % parent must be a figure, we default to gcf upstairs 0142 createPanel; 0143 hgp.setUIContainer(hUicontainer); 0144 else 0145 if parentIsFigure 0146 if isnumeric(position) 0147 if isempty(position) 0148 position = [20 20 60 20]; 0149 end 0150 % numeric position is not set here, rely on the uicontainer 0151 % listeners below. 0152 hgp = handle(peer.addchild(component)); 0153 createPanel; 0154 hgp.setUIContainer(hUicontainer); 0155 elseif ... 0156 isequal(char(position),char(java.awt.BorderLayout.NORTH)) || ... 0157 isequal(char(position),char(java.awt.BorderLayout.SOUTH)) || ... 0158 isequal(char(position),char(java.awt.BorderLayout.EAST)) || ... 0159 isequal(char(position),char(java.awt.BorderLayout.WEST)) 0160 hgp = handle(peer.addchild(component, position)); 0161 returnContainer = 0; 0162 createPanel; 0163 else 0164 error('MATLAB:javacomponent:InvalidPosition', 'Invalid component position\n%s', usage) 0165 end 0166 else 0167 % Adding component to the toolbar. 0168 % component position is ignored for now 0169 peer.add(component); 0170 hUicontainer = parent; % toolbar. 0171 handles = getappdata(hUicontainer, 'childhandles'); 0172 handles = [handles, hcomponent]; 0173 setappdata(hUicontainer, 'childhandles', handles); 0174 end 0175 0176 % make sure the component is on the screen so the 0177 % caller can interact with it right now. 0178 % drawnow; 0179 end 0180 0181 configureComponent; 0182 0183 if (returnContainer == 1) 0184 hcontainer = hUicontainer; 0185 else 0186 hcontainer = []; 0187 end 0188 0189 function createPanel 0190 % add delete listener 0191 hUicontainer = hgjavacomponent('Parent', parent, 'Units', 'Pixels'); 0192 0193 set(hUicontainer, 'UserData', char(component.getClass.getName)); % For findobj queries. 0194 if isa(java(hgp), 'com.mathworks.hg.peer.FigureChild') 0195 set(hUicontainer, 'FigureChild', hgp); 0196 end 0197 if isa(java(hcomponent), 'javax.swing.JComponent') 0198 % force component to be opaque if it's a JComponent. This prevents 0199 % lightweight component from showing the figure background (which 0200 % may never get a paint event) 0201 hcomponent.setOpaque(true); 0202 end 0203 set(hUicontainer, 'JavaPeer', hcomponent); 0204 0205 if (returnContainer == 1) 0206 % add resize listener to parent (parent must be a figure or this dies quietly) 0207 % this is for normalized units 0208 addlistener(hUicontainer, 'PixelBounds', 'PropertyPostSet', @handleResize); 0209 0210 % add visible listener 0211 addlistener(hUicontainer, 'Visible', 'PropertyPostSet', @handleVisible); 0212 0213 % add parent listener 0214 addlistener(hUicontainer, 'Parent', 'PropertyPreSet', @handlePreParent); 0215 0216 % add parent listener 0217 addlistener(hUicontainer, 'Parent', 'PropertyPostSet', @handlePostParent); 0218 0219 % force though 1st resize event 0220 set(hUicontainer,'Position', position); 0221 else 0222 % For the BorderLayout components, we dont really want the 0223 % hUicontainer to show. But, it provides a nice place for cleanup. 0224 set(hUicontainer,'Visible', 'off', 'Handlevisibility', 'off'); 0225 % Set position out of the figure to work around a current bug 0226 % due to which invisible uicontainers show up when renderer is 0227 % OpenGL (G. 0228 set(hUicontainer, 'Position', [1 1 0.01 0.01]); 0229 end 0230 0231 if isa(component,'com.mathworks.hg.peer.FigureChild') 0232 component.setUIContainer(hUicontainer); 0233 end 0234 0235 function handleResize(obj, evd) %#ok - mlint 0236 hgp.setPixelPosition(getpixelposition(hUicontainer, true)); 0237 end 0238 0239 function handleVisible(obj, evd) %#ok - mlint 0240 hgp.setVisible(strcmp(get(hUicontainer,'Visible'),'on')) 0241 end 0242 0243 function handlePreParent(obj, evd) %#ok - mlint 0244 oldfig = ancestor(parent, 'figure'); 0245 newfig = ancestor(evd.NewValue, 'figure'); 0246 if ~isempty(newfig) && ~isequal(oldfig, newfig) 0247 peer = get(oldfig,'JavaFrame'); 0248 peer.remove(component); 0249 end 0250 end 0251 0252 function handlePostParent(obj, evd) %#ok - mlint 0253 oldfig = ancestor(parent, 'figure'); 0254 newfig = ancestor(evd.NewValue, 'figure'); 0255 if ~isempty(newfig) && ~isequal(oldfig, newfig) 0256 peer = get(newfig,'JavaFrame'); 0257 hgp= handle(peer.addchild(component)); 0258 if isa(java(hgp), 'com.mathworks.hg.peer.FigureChild') 0259 % used by the uicontainer C-code 0260 setappdata(hUicontainer, 'FigureChild', java(hgp)); 0261 end 0262 parent = newfig; 0263 end 0264 hgp.setPixelPosition(getpixelposition(hUicontainer, true)); 0265 end 0266 end 0267 0268 function configureComponent 0269 set(hUicontainer,'DeleteFcn', {@containerDelete, hcomponent}); 0270 addlistener(hcomponent, 'ObjectBeingDestroyed', {@componentDelete, hUicontainer, parentIsFigure}); 0271 end 0272 0273 end 0274 0275 function containerDelete(obj, evd, hc) %#ok - mlint 0276 if isa(handle(obj), 'uitoolbar') 0277 childHandles = getappdata(obj, 'childhandles'); 0278 delete(childHandles); 0279 else 0280 if ishandle(hc) 0281 delete(hc); 0282 end 0283 end 0284 end 0285 0286 function componentDelete(obj, evd, hUicontainer, parentIsFigure) %#ok - mlint 0287 if (parentIsFigure) 0288 % This java component is always deleted before hUicontainer. It is 0289 % ensured by calling component deletion in function containerDelete. 0290 % hUicontainer becomes invalid when delete(hUicontainer) below is run. 0291 parent = ancestor(hUicontainer,'figure'); 0292 peer = get(parent, 'JavaFrame'); 0293 0294 if any(ishandle(obj)) 0295 removeobj = java(obj); 0296 if ~isempty(get(hUicontainer,'FigureChild')) 0297 removeobj = get(hUicontainer,'FigureChild'); 0298 end 0299 peer.remove(removeobj); 0300 end 0301 0302 % delete container if it exists 0303 if any(ishandle(hUicontainer)) 0304 delete(hUicontainer); 0305 end 0306 else 0307 parent = hUicontainer; % toolbar 0308 if ~ishandle(parent) || ~ishandle(obj) 0309 % The toolbar parent or the component has been deleted. Bail out. 0310 % Toolbar clears all javacomponents after itself. 0311 return; 0312 end 0313 0314 peer = get(parent, 'JavaContainer'); 0315 if ~isempty(peer) 0316 peer.remove(java(obj)); 0317 end 0318 end 0319 end 0320 0321 0322 function str=usage 0323 str = [sprintf('\n') ... 0324 'WARNING: This feature is not supported in MATLAB and the API and ' sprintf('\n') ... 0325 'functionality may change in a future release. ' sprintf('\n') ... 0326 ]; 0327 %{ 0328 'usage: javacomponent(hJavaComponent, position, parent) ' sprintf('\n') ... 0329 '- position can be [left bottom width height] in pixels ' sprintf('\n') ... 0330 ' or the string North, South, East, or West' sprintf('\n') ... 0331 '- parent can be a figure or a uitoolbar handle.']; 0332 %} 0333 end