MATLAB Answers

How can I suppress warnings about javacomponent being deprecated?

68 views (last 30 days)
Josh G.
Josh G. on 21 Jan 2020
Commented: Josh G. on 22 Jan 2020
I've got a GUI that uses Java functionality and requires use of the javacomponent function. While javacomponent is still available, though, I'd like to suppress the warnings about it being removed in a future release. There's no warning shown in the editor, with mlint, or with checkcode, so I can't suppress the warning like usual.
Is there a way to suppress this particular warning?

  3 Comments

Walter Roberson
Walter Roberson on 21 Jan 2020
warning('off', 'MATLAB:ui:javacomponent:FunctionToBeRemoved')
possibly.
Josh G.
Josh G. on 22 Jan 2020
Sindar, I've looked into that page but the limitations of uitree and MATLAB's slider offerings currently require me to use javacomponent.

Sign in to comment.

Accepted Answer

Yair Altman
Yair Altman on 22 Jan 2020
Edited: Yair Altman on 22 Jan 2020
Wrap your javacomponent code as follows:
oldWarningState = warning('off', 'MATLAB:ui:javacomponent:FunctionToBeRemoved');
[hjcomponent, hcontainer] = javacomponent(...);
warning(oldWarningState); % revert to displaying the warning
If you have multiple calls to javacomponent in your GUI code, you may wish to revert the warning state only at the end of your GUI creation code (take care to do it even in case of exceptions or early function bail-outs, so that you don't get stuck forever with the warning turned off.
If you want to permanently turn off this warning, run warning('off','MATLAB:ui:javacomponent:FunctionToBeRemoved') in your Matlab Command Window and then you won't need to modify the code at all.
General note: to know which warning identifier cause each warning that is displayed, run the following:
warning('on','verbose')
This will include the display of the warning ID ('MATLAB:ui:javacomponent:FunctionToBeRemoved' in this specific case) next to the warning message. For example:
Warning: JAVACOMPONENT will be removed in a future release. For more information see UI Alternatives for MATLAB Apps on mathworks.com.
(Type "warning off MATLAB:ui:javacomponent:FunctionToBeRemoved" to suppress this warning.)

  2 Comments

Steven Lord
Steven Lord on 22 Jan 2020
If you wanted to be exception safe, you could determine the state of that warning, create an onCleanup object to restore that state, then disable the warning. When the onCleanup object goes out of scope, its destructor would restore the warning state. [If MATLAB crashes, the onCleanup object may not get to run its destructor, but in that case does the warning state of your crashing MATLAB session really matter?]
id = 'MATLAB:ui:javacomponent:FunctionToBeRemoved';
oldState = warning('query', id);
restoreWarning = onCleanup(@() warning(oldState));
warning('off', id)
Josh G, while you can suppress the warning, you should start thinking now about how to transition away from javacomponent. This documentation page states "As a result, these capabilities will be removed in a future release." The warning is intended to give you time to transition to alternatives before those capabilities are removed.
Josh G.
Josh G. on 22 Jan 2020
Thanks Yair, that worked. Steven, I like the suggestion about using onCleanup; it's easier to maintain than manually turning the warning back on at the end of the function. I'm looking into alternatives, but in the meantime the warning is only telling me something I already know.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Jan 2020
What is the exact error? Anyway, look in this function - maybe it's in there. If not, follow the instructions in the file to determine the code to turn off the warning.
% Turn off benign warnings that you usually don't care about.
% http://www.mathworks.com/help/matlab/matlab_prog/suppress-warnings.html
function TurnOffWarnings
try
% To set the warning state, you must first know the message identifier for the one warning you want to enable.
% Query the last warning to acquire the identifier. For example:
% warnStruct = warning('query', 'last')
% messageID = warnStruct.identifier
% messageID =
% MATLAB:concatenation:integerInteraction
% Turn off this warning "Warning: Image is too big to fit on screen; displaying at 33% "
warning('off', 'Images:initSize:adjustingMag');
% Get rid of warning about directory already existing:
% "Warning: Directory already exists."
warning('off', 'MATLAB:MKDIR:DirectoryExists');
% Turn off note "Warning: Added specified worksheet." that appears in the command window.
warning('off', 'MATLAB:xlswrite:AddSheet');
% Get rid of warning about roipolyold being deprecated:
% "Warning: Function ROIPOLYOLD will be removed in the future. Use ROIPOLY instead"
warning('off', 'images:removing:function');
% Get rid of warning about wavread() being deprecated:
% "Warning: WAVREAD will be removed in a future release. Use AUDIOREAD instead."
warning('off', 'MATLAB:audiovideo:wavread:functionToBeRemoved');
% Turn off warning about variable names being modified if you use readtable() to read in column headers that have spaces in them.
% "Warning: Variable names were modified to make them valid MATLAB identifiers."
warning('off', 'MATLAB:table:ModifiedVarnames');
% Turn off warning about JavaFrame being obsolete.
% "Warning: figure JavaFrame property will be obsoleted in a future release. For more information see http://www.mathworks.com/javaframe, the JavaFrame resource on the Mathworks web site."
warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame')
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from TurnOffWarnings

  1 Comment

Josh G.
Josh G. on 22 Jan 2020
The exact error was:
Warning: JAVACOMPONENT will be removed in a future release. For more information, see Recommendations for Java and ActiveX Users on mathworks.com.
> In javacomponent (line 82)

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by