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.1 $ $Date: 2007/06/14 14:53:32 $
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.1 $ $Date: 2007/06/14 14:53:32 $ 0076 0077 if (usejavacomponent == 0) 0078 err.message = 'JAVACOMPONENT is not supported on this platform'; 0079 err.identifier = 'MATLAB:javacomponent:FeatureNotSupported'; 0080 error(err); 0081 end 0082 0083 if ~isempty(nargchk(1,3,nargin)) 0084 error('MATLAB:javacomponent',usage); 0085 end 0086 0087 if nargin < 3 0088 parent = gcf; 0089 end 0090 0091 if nargin < 2 0092 position = [20 20 60 20]; 0093 end 0094 0095 parentIsFigure = false; 0096 hParent = handle(parent); 0097 if ( isa(hParent, 'figure') || ... 0098 isa(hParent, 'uicontainer') || ... 0099 isa(hParent, 'uiflowcontainer') || ... 0100 isa(hParent, 'uigridcontainer')) 0101 parentIsFigure = true; 0102 peer = get(ancestor(parent,'figure'),'JavaFrame'); 0103 elseif isa(hParent, 'uitoolbar') 0104 peer = get(parent,'JavaContainer'); 0105 if isempty(peer) 0106 drawnow; 0107 peer = get(parent,'JavaContainer'); 0108 end 0109 else 0110 error('MATLAB:javacomponent:InvalidParentHandle', 'Invalid parent handle\n%s', usage) 0111 end 0112 0113 if isempty(peer) 0114 error('MATLAB:javacomponent:JavaFigsNotEnabled', 'Java figures are not enabled') 0115 end 0116 0117 hUicontainer = []; 0118 hgp = []; 0119 returnContainer = 1; 0120 0121 if ischar(component) 0122 component = awtcreate(component); 0123 end 0124 0125 % promote the component to a handle object first. 0126 % It seems once a java object is cast to a handle, you cannot get another 0127 % handle with 'callbackproperties'. So, do want you need outright and use 0128 % the same handle in the subsequent code. 0129 if ~isjava(component) 0130 component =java(component); 0131 end 0132 hcomponent = handle(component,'callbackProperties'); 0133 0134 if nargin == 1 0135 hgp = handle(peer.addchild(component)); 0136 % parent must be a figure, we default to gcf upstairs 0137 createPanel; 0138 hgp.setUIContainer(hUicontainer); 0139 else 0140 if parentIsFigure 0141 if isnumeric(position) 0142 if isempty(position) 0143 position = [20 20 60 20]; 0144 end 0145 % numeric position is not set here, rely on the uicontainer 0146 % listeners below. 0147 hgp = handle(peer.addchild(component)); 0148 createPanel; 0149 hgp.setUIContainer(hUicontainer); 0150 elseif ... 0151 isequal(char(position),char(java.awt.BorderLayout.NORTH)) || ... 0152 isequal(char(position),char(java.awt.BorderLayout.SOUTH)) || ... 0153 isequal(char(position),char(java.awt.BorderLayout.EAST)) || ... 0154 isequal(char(position),char(java.awt.BorderLayout.WEST)) 0155 hgp = handle(peer.addchild(component, position)); 0156 returnContainer = 0; 0157 createPanel; 0158 else 0159 error('MATLAB:javacomponent:InvalidPosition', 'Invalid component position\n%s', usage) 0160 end 0161 else 0162 % Adding component to the toolbar. 0163 % component position is ignored for now 0164 peer.add(component); 0165 hUicontainer = parent; % toolbar. 0166 handles = getappdata(hUicontainer, 'childhandles'); 0167 handles = [handles, hcomponent]; 0168 setappdata(hUicontainer, 'childhandles', handles); 0169 end 0170 0171 % make sure the component is on the screen so the 0172 % caller can interact with it right now. 0173 % drawnow; 0174 end 0175 0176 configureComponent; 0177 0178 if (returnContainer == 1) 0179 hcontainer = hUicontainer; 0180 else 0181 hcontainer = []; 0182 end 0183 0184 function createPanel 0185 % add delete listener 0186 hUicontainer = hgjavacomponent('Parent', parent, 'Units', 'Pixels'); 0187 0188 set(hUicontainer, 'UserData', char(component.getClass.getName)); % For findobj queries. 0189 if isa(java(hgp), 'com.mathworks.hg.peer.FigureChild') 0190 set(hUicontainer, 'FigureChild', hgp); 0191 end 0192 if isa(java(hcomponent), 'javax.swing.JComponent') 0193 % force component to be opaque if it's a JComponent. This prevents 0194 % lightweight component from showing the figure background (which 0195 % may never get a paint event) 0196 hcomponent.setOpaque(true); 0197 end 0198 set(hUicontainer, 'JavaPeer', hcomponent); 0199 0200 if (returnContainer == 1) 0201 % add resize listener to parent (parent must be a figure or this dies quietly) 0202 % this is for normalized units 0203 addlistener(hUicontainer, 'PixelBounds', 'PropertyPostSet', @handleResize); 0204 0205 % add visible listener 0206 addlistener(hUicontainer, 'Visible', 'PropertyPostSet', @handleVisible); 0207 0208 % add parent listener 0209 addlistener(hUicontainer, 'Parent', 'PropertyPreSet', @handlePreParent); 0210 0211 % add parent listener 0212 addlistener(hUicontainer, 'Parent', 'PropertyPostSet', @handlePostParent); 0213 0214 % force though 1st resize event 0215 set(hUicontainer,'Position', position); 0216 else 0217 % For the BorderLayout components, we dont really want the 0218 % hUicontainer to show. But, it provides a nice place for cleanup. 0219 set(hUicontainer,'Visible', 'off', 'Handlevisibility', 'off'); 0220 % Set position out of the figure to work around a current bug 0221 % due to which invisible uicontainers show up when renderer is 0222 % OpenGL (G. 0223 set(hUicontainer, 'Position', [1 1 0.01 0.01]); 0224 end 0225 0226 if isa(component,'com.mathworks.hg.peer.FigureChild') 0227 component.setUIContainer(hUicontainer); 0228 end 0229 0230 function handleResize(obj, evd) %#ok - mlint 0231 hgp.setPixelPosition(getpixelposition(hUicontainer, true)); 0232 end 0233 0234 function handleVisible(obj, evd) %#ok - mlint 0235 hgp.setVisible(strcmp(get(hUicontainer,'Visible'),'on')) 0236 end 0237 0238 function handlePreParent(obj, evd) %#ok - mlint 0239 oldfig = ancestor(parent, 'figure'); 0240 newfig = ancestor(evd.NewValue, 'figure'); 0241 if ~isempty(newfig) && ~isequal(oldfig, newfig) 0242 peer = get(oldfig,'JavaFrame'); 0243 peer.remove(component); 0244 end 0245 end 0246 0247 function handlePostParent(obj, evd) %#ok - mlint 0248 oldfig = ancestor(parent, 'figure'); 0249 newfig = ancestor(evd.NewValue, 'figure'); 0250 if ~isempty(newfig) && ~isequal(oldfig, newfig) 0251 peer = get(newfig,'JavaFrame'); 0252 hgp= handle(peer.addchild(component)); 0253 if isa(java(hgp), 'com.mathworks.hg.peer.FigureChild') 0254 % used by the uicontainer C-code 0255 setappdata(hUicontainer, 'FigureChild', java(hgp)); 0256 end 0257 parent = newfig; 0258 end 0259 hgp.setPixelPosition(getpixelposition(hUicontainer, true)); 0260 end 0261 end 0262 0263 function configureComponent 0264 set(hUicontainer,'DeleteFcn', {@containerDelete, hcomponent}); 0265 addlistener(hcomponent, 'ObjectBeingDestroyed', {@componentDelete, hUicontainer, parentIsFigure}); 0266 end 0267 0268 end 0269 0270 function containerDelete(obj, evd, hc) %#ok - mlint 0271 if isa(handle(obj), 'uitoolbar') 0272 childHandles = getappdata(obj, 'childhandles'); 0273 delete(childHandles); 0274 else 0275 if ishandle(hc) 0276 delete(hc); 0277 end 0278 end 0279 end 0280 0281 function componentDelete(obj, evd, hUicontainer, parentIsFigure) %#ok - mlint 0282 if (parentIsFigure) 0283 % This java component is always deleted before hUicontainer. It is 0284 % ensured by calling component deletion in function containerDelete. 0285 % hUicontainer becomes invalid when delete(hUicontainer) below is run. 0286 parent = ancestor(hUicontainer,'figure'); 0287 peer = get(parent, 'JavaFrame'); 0288 0289 if any(ishandle(obj)) 0290 removeobj = java(obj); 0291 if ~isempty(get(hUicontainer,'FigureChild')) 0292 removeobj = get(hUicontainer,'FigureChild'); 0293 end 0294 peer.remove(removeobj); 0295 end 0296 0297 % delete container if it exists 0298 if any(ishandle(hUicontainer)) 0299 delete(hUicontainer); 0300 end 0301 else 0302 parent = hUicontainer; % toolbar 0303 if ~ishandle(parent) || ~ishandle(obj) 0304 % The toolbar parent or the component has been deleted. Bail out. 0305 % Toolbar clears all javacomponents after itself. 0306 return; 0307 end 0308 0309 peer = get(parent, 'JavaContainer'); 0310 if ~isempty(peer) 0311 peer.remove(java(obj)); 0312 end 0313 end 0314 end 0315 0316 0317 function str=usage 0318 str = [sprintf('\n') ... 0319 'WARNING: This feature is not supported in MATLAB and the API and ' sprintf('\n') ... 0320 'functionality may change in a future release. ' sprintf('\n') ... 0321 ]; 0322 %{ 0323 'usage: javacomponent(hJavaComponent, position, parent) ' sprintf('\n') ... 0324 '- position can be [left bottom width height] in pixels ' sprintf('\n') ... 0325 ' or the string North, South, East, or West' sprintf('\n') ... 0326 '- parent can be a figure or a uitoolbar handle.']; 0327 %} 0328 end