- Undocumented Matlab - https://undocumentedmatlab.com -
Customizing help popup contents
Posted By Yair Altman On November 30, 2009 | 9 Comments
A few days ago, I was asked by a reader [1] how to programmatically display the popup help window and customize it with arbitrary contents. This help window displays the doc-page associated with the current Command Window or Editor text.
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jTextArea = jDesktop.getMainFrame.getFocusOwner;
jClassName = 'com.mathworks.mlwidgets.help.HelpPopup';
jPosition = java.awt.Rectangle(0,0,400,300);
helpTopic = 'surf';
javaMethodEDT('showHelp',jClassName,jTextArea,[],jPosition,helpTopic);
Where:
1) jPosition sets popup’s pixel size and position (X,Y,Width,Height). Remember that Java counts from the top down (contrary to Matlab) and is 0-based. Therefore, Rectangle(0,0,400,300) is a 400×300 window at the screen’s top-left corner.
2) helpTopic is the help topic of your choice (the output of the doc function). To display arbitrary text, you can create a simple .m file that only has a main help comment with your arbitrary text, which will be presented in the popup.
3) on R2011b, we could simplify the above code snippet with the following replacement:
helpUtils.errorDocCallback('surf')
4) on R2007b, we need to use the equivalent but more cumbersome awtinvoke function instead of javaMethodEDT:
jniSig = 'showHelp(Ljavax.swing.JComponent;Lcom.mathworks.mwswing.binding.KeyStrokeList;Ljava.awt.Rectangle;Ljava.lang.String;)';
awtinvoke(jClassName,jniSig,jTextArea,[],jPosition,helpTopic);
For example, if we had a sample.m file with the following contents:
function sample
% The text in this function's main comment will be presented in the
% help popup. Hyperlinks [3]
% are supported, but unfortunately not full-fledged HTML.
Then we would get this result:
The doc function is intended only for reference pages supplied by The MathWorks. The exception is the doc UserCreatedClassName syntax. doc does not display HTML files you create yourself. To display HTML files for functions you create, use the web function.
% Find the Help popup window
jWindows = com.mathworks.mwswing.MJDialog.getWindows;
jPopup = [];
for idx=1 : length(jWindows)
if strcmp(get(jWindows(idx),'Name'),'HelpPopup')
if jWindows(idx).isVisible
jPopup = jWindows(idx);
break;
end
end
end
% Update the popup with selected HTML
html=['Full HTML support: bold, '...
'italic, hyperlink [5], ' ...
'symbols (∀β) etc.'];
if ~isempty(jPopup)
browser = jPopup.getContentPane.getComponent(1).getComponent(0);
browser.setHtmlText(html);
end
browser.setHtmlTextAndHighlightKeywords(html,{'support','symbols'});
browser.setCurrentLocation('UndocumentedMatlab.com');
Categories: Desktop, High risk of breaking in future versions, Java, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/customizing-help-popup-contents
URLs in this post:
[1] asked by a reader: http://undocumentedmatlab.com/blog/context-sensitive-help/#comment-4691
[2] modeless MJDialog Java window: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/
[3] Hyperlinks: https://undocumentedmatlab.com
[4] Matlab documentation: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/doc.html
[5] hyperlink: http://undocumentedmatlab.commatlab:dir
[6] Image: http://undocumentedmatlab.com/images/HelpPopup6.png
[7] UiInspect utility: http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object
[8] downloaded from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/25975-popuppanel-displays-a-popup-panel-with-specified-help-topic-html-or-webpage
[9] context-sensitive help system: http://undocumentedmatlab.com/blog/context-sensitive-help/
[10] Matlab toolstrip – part 9 (popup figures) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures
[11] Setting system tray popup messages : https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages
[12] Rich-contents log panel : https://undocumentedmatlab.com/articles/rich-contents-log-panel
[13] Aligning uicontrol contents : https://undocumentedmatlab.com/articles/aligning-uicontrol-contents
[14] Rich Matlab editbox contents : https://undocumentedmatlab.com/articles/rich-matlab-editbox-contents
[15] Customizing Workspace context-menu : https://undocumentedmatlab.com/articles/customizing-workspace-context-menu
[16] : https://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/comment-page-1/#comment-6079
[17] : https://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
9 Comments To "Customizing help popup contents"
#1 Comment By Nathan On January 27, 2010 @ 16:18
I’m having troubles using this in a GUI that I’m coding by hand.
As a “help” pushbutton callback, I have the following code:
However, I am receiving an error stating that
“??? Error using ==> javaMethodEDT
Java exception occurred:
java.lang.IllegalArgumentException: ‘parent’ cannot be null”
When stepping through the debugger, I find that jTextArea is empty. However, if I re-type that line while in debug mode, it works just fine. Do you know any way that I can fix this problem?
-Nathan
#2 Comment By Yair Altman On January 27, 2010 @ 16:40
@Nathan – I assume you haven’t placed this in your GUIDE-generated _OpeningFcn(), as explained [16]. Another cause of this might be that when you run the code, jTextArea is still unavailable by the time the javaMethodEDT is called. In step-by-step debugging this is not an issue, since there is plenty of time for jTextArea to be set. It is also possible that there is no FocusOwner in the Desktop (unlike the case of step-by-step debugging) – this can be forced using the commandwindow function. As a final alternative, try using the getClient() approach to getting the Command-window TextArea, as I explained in my recent [17]:
#3 Comment By Nathan On January 27, 2010 @ 17:08
I am not using GUIDE (as implied by “coding by hand”), but your getClient() method worked for me. Thanks for the quick response and your help. This website is very insightful.
-Nathan
#4 Comment By Peng Liu On July 25, 2019 @ 11:00
Hi Yair,
When I try to use html in help popup, I was told there is no setHtmlText of browser. So I ask myself is there any update in matlab 2017b. But I tried getComponent with other indices which doesn’t work. Maybe you would have a clue. Thanks a lot.
#5 Comment By Yair Altman On July 26, 2019 @ 13:12
@Peng – the code above was relevant as of the time I wrote the article, 10 years ago (2009). In recent Matlab releases the code needs to be modified a bit:
#6 Comment By Peter On August 11, 2020 @ 22:14
I have a problem with putting this into the startup.m to be executed at the beginning of the MATLAB session.
My startup script uses similar code that opens and closes a dummy popup with custom window size that would be applied for subsequent F1-popups.
In R2018a and R2020b Prerelease the above example works well as standalone code. Nevertheless, when used in startup.m it does not create a popup during the startup (and for variant 1 it also results in “java.lang.IllegalArgumentException: ‘parent’ cannot be null” exception). I remember that in some earlier releases a similar solution used to work (pause(1) or drawnow were helpful in case of errors), but now I cannot find any fix. Maybe you would know if it is still possible to open a popup with the desired size during MATLAB startup.
#7 Comment By Yair Altman On August 14, 2020 @ 16:13
@Peter – the graphics system might not be fully initialized by the time that the startup script is being executed by the Matlab engine. I suggest placing the relevant code in a small function and in startup.m simply start a single-shot timer with a StartDelay of several seconds, that will run the new function in its TimerFcn callback.