- Undocumented Matlab - http://undocumentedmatlab.com -

GUI integrated browser control

Posted By Yair Altman On December 9, 2009 @ 8:35 am In Figure window,GUI,Handle graphics,Java,Medium risk of breaking in future versions,UI controls | 26 Comments

Last week, I described the built-in PopupPanel object [3], and showed how it can be used to present popup messages with HTML content and even entire webpages. I explained that PopupPanel uses an internal browser object to achieve this. In fact, Matlab’s browser object predates PopupPanel by many years and quite a few releases. This browser object can be used as a stand-alone component that we can easily embed in our Matlab GUI applications.

Here is a simple example in which a Matlab Listbox uicontrol is used to select the contents of an adjacent browser component:

% Create a blank figure window
f=figure('Name','Browser GUI demo','Num','off','Units','norm');
 
% Add the browser object on the right
jObject = com.mathworks.mlwidgets.html.HTMLBrowserPanel;
[browser,container] = javacomponent(jObject, [], f);
set(container, 'Units','norm', 'Pos',[0.3,0.05,0.65,0.9]);
 
% Add the URLs listbox on the left
urls = {'www.cnn.com','www.bbc.co.uk','myLocalwebpage.html',...
        'www.Mathworks.com', 'undocumentedmatlab.com'};
hListbox = uicontrol('style','listbox', 'string',urls, ...
        'units','norm', 'pos',[0.05,0.05,0.2,0.9], ...
        'userdata',browser);
 
% Set the listbox's callback to update the browser contents
cbStr=['strs = get(gcbo,''string''); ' ...
      'url = strs{get(gcbo,''value'')};' ...
      'browser = get(gcbo,''userdata''); ' ...
      'msg=[''<html><h2>Loading '' url '' - please wait''];'...  % no need for </h2></html>
      'browser.setHtmlText(msg); pause(0.1); drawnow;'...
      'browser.setCurrentLocation(url);'];
set(hListbox,'Callback',cbStr);

Browser object integrated in Matlab GUI (click for large image) [4]

Browser object integrated in Matlab GUI

In this simple example, we can see how the Java browser object can easily be controlled by Matlab. Specifically, we use two modes of the browser: first we present an HTML message (‘Loading www.cnn.com – please wait‘) and then replacing this content with the actual webpage, if accessible. If the webpage is not accessible, an error message is displayed:

Browser message when webpage is missing (click for large image) [5]

Browser message when webpage is missing

We can easily expand this simple example to display any HTML message or webpage, in a seamless integration within our GUI.

Now, who ever said that Matlab GUI looks static or boring???

In an unrelated note, I would like to extend good wishes to Ken Orr, who has left the Mathworks Desktop development team to join Apple a few days ago [6]. You probably know Ken from his good work on the Desktop and the official Matlab Desktop blog [7]. Hopefully, in his new position Ken will be able to influence Mac Java in a way that will reduce the numerous recurring issues that afflict Matlab Mac releases [8].

Related posts:

  1. GUI integrated HTML panel [9] Simple HTML can be presented in a Java component integrated in Matlab GUI, without requiring the heavy browser control....
  2. Inactive Control Tooltips & Event Chaining [10] Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
  3. Matlab-Java interface using a static control [11] The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....
  4. Customizing Matlab labels [12] Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...
  5. Setting listbox mouse actions [13] Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....
  6. The javacomponent function [14] Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...


26 Comments (Open | Close)

26 Comments To "GUI integrated browser control"

#1 Comment By Ken Orr On December 15, 2009 @ 3:53 am

Thanks Yair! I’ll do what I can to help influence Java on the Mac.

#2 Comment By Gunnar Ingestrom On January 22, 2010 @ 10:04 am

Thanks for a great tip! I have been struggling with the limited HTML capabilities of the JEditorPane but now you gave me a solution that really works out of the box.

#3 Comment By Sid On February 8, 2010 @ 8:44 pm

Hi Yair,
Is it possible to view non-html links, say, a pdf file from within the matlab web browser? That should be without opening a new acrobat reader window.
Thanks
Sid

#4 Comment By Yair Altman On February 9, 2010 @ 2:41 am

@Sid – I don’t think so. Even regular browsers need to use Acrobat (or another PDF reader like Foxit) to display PDF.

#5 Comment By Arnaud On August 18, 2010 @ 4:58 am

Hi Yair,

I’m designing a dialog similar to a throbber, indicating that some computation is running, but autonomous, contrary to matlab’s native waitbar. In order to save CPU ressources, i try to take advantages of animated GIF. After having considered timer functions for refreshing an axes, i finaly came out to use an HTML browser that can render basic animated GIFs. Your tip provides me a great solution, however, i’m wondering how i can remove the scrollbars that appear when i set the url to any image file :

browser.setCurrentLocation('toto.gif');

I can’t see any method dealing with scrolling by typing :

methodsview(browser)

I will try to work on browser size, but i appreciate if you can provide some hints.
Thanks.
Arnaud

#6 Comment By Yair Altman On August 18, 2010 @ 9:34 am

@Arnaud – here’s a much lighter-weight alternative:

iconsClassName = 'com.mathworks.widgets.BusyAffordance$AffordanceSize';
iconsSizeEnums = javaMethod('values',iconsClassName);
SIZE_32x32 = iconsSizeEnums(2);  % (1) = 16x16
jObj = com.mathworks.widgets.BusyAffordance(SIZE_32x32,'testing...');
javacomponent(jObj.getComponent,[10,10,80,80],gcf);
jObj.start;
    % do some long operation...
jObj.stop;

#7 Comment By Yonatan On October 6, 2010 @ 9:29 pm

Hi,
This works great.
I do have a slight problem – if the page I’m viewing has some dhtml pop up window, it shows like an error message and there’s nothing I can do – the whole program is stuck until I turn it off (click ok or something).
Here’s an example of such a web page:
[15] (simply let the page load, and then refresh the screen to see the popup).
Is there a way to monitor such messages and handle them via the code? Or maybe disable them?
Thanks :)

#8 Pingback By Customizing menu items part 3 | Undocumented Matlab On May 9, 2012 @ 11:01 am

[...] Mike has been with MathWorks since 2005 and has been responsible for maintaining the official MATLAB Desktop blog, together with Ken Orr. I’m not sure yet which direction the Desktop blog will take, and by whom, but in any case it won’t be the same. [...]

#9 Comment By Ivan On December 25, 2012 @ 4:53 am

Hi, Yair!
Is it possible to track changes in browser object? For example, change listbox when we follow a link to another site.
Thanks.
Ivan.

#10 Comment By Yair Altman On December 27, 2012 @ 3:56 pm

@Ivan – the easiest way would be to check whether the browser object (or one of its sub-components) has a built-in event (Matlab callback) that you can use. Alternatively, you could try using a property-change listener as explained [16].

#11 Comment By Arnaud On January 24, 2013 @ 7:39 am

Hi Yair,

I’m just reading your response to my post I wrote… 2 years ago.
The need for a throbber was off the radar during this time, but finaly rises again, and your piece of code is actualy exactly what I was looking for.
Thank you for your help, and continue to keep this extremly usefull website alive.

Arnaud

#12 Comment By thnxtay On May 2, 2013 @ 12:33 pm

I am having an issue with grabbing the HTML text from a particular website using the embedded browser. I took your code and dropped in the following URL:

[17]

In the “Database” drop down under the “Input” cell, select any one of the databases. It will load the data for that particular study.

OBJECTIVE: Capture the text on the page for the time section of the study being viewed; the text in the blue bar just above the graphs (“Selected Input:”).

PROBLEM: HTML text field in browser object is not updated after navigating away from welcome page. Need to find a way to get the HTML code that is specific to the study data that is being displayed.

METHODS:
1) Used uiinspect.m ( [18]) to find the displayed HTML for the data page of the study. Can’t find any field containing the new info.
2) Right clicked on the page and used the “Page Source” option. It shows the original welcome screen html
3) Right clicked on the page and used the “Refresh” option. This did not update the HTML text field of the browser object. It did keep you on the study page, and did not refresh to the welcome page, even though it had to resend the POSTDATA query.
4) Viewed the same page in Firefox and inspected the page source code. This gave me what I want; HTML code specific to the study.

Any help would be greatly appreciated. Thank you.

Here is the modified m-code from this post that has a link to the page that I am working with:

% Create a blank figure window
f = figure('Name','Browser GUI demo','Num','off','Units','norm');
 
% Add the browser object on the right
jObject = com.mathworks.mlwidgets.html.HTMLBrowserPanel;
[browser,container] = javacomponent(jObject, [], f);
set(container, 'Units','norm', 'Pos',[0.3,0.05,0.65,0.9]);
 
% Add the URLs listbox on the left
urls = {'http://www.physionet.org/cgi-bin/atm/ATM','www.bbc.co.uk','myLocalwebpage.html',...
        'www.Mathworks.com', 'UndocumentedMatlab.com'};
hListbox = uicontrol('style','listbox', 'string',urls, ...
        'units','norm', 'pos',[0.05,0.05,0.2,0.9], ...
        'userdata',browser);
 
% Set the listbox's callback to update the browser contents
cbStr=['strs = get(gcbo,''string''); ' ...
      'url = strs{get(gcbo,''value'')};' ...
      'browser = get(gcbo,''userdata''); ' ...
      'msg=[''Loading '' url '' - please wait''];'...
      'browser.setHtmlText(msg); pause(0.1); drawnow;'...
      'browser.setCurrentLocation(url);'];
set(hListbox,'Callback',cbStr);
 
% context menu
cmenu = uicontextmenu;
hcb1 = ['set(f, ''Color'', [0.9 0.6 0.5])'];
item1 = uimenu(cmenu, 'Label', 'change color', 'Callback', hcb1);
set(container,'uicontextmenu',cmenu);

#13 Comment By kobbi On May 21, 2013 @ 4:32 am

Hi

I tried to use the code example above, to load an html file that I have created into the GUI, but I got the following message (inside the UI browser):

“This file exceeds the maximum size for the MATLAB Web Browser. It will open in your system browser.”

and indeed the html was opened with in internet explorer.

Any idea how to solve this problem?

Thanks
Kobbi

#14 Comment By Yair Altman On May 28, 2013 @ 5:02 pm

@thnxtay – I suggest that you load your webpage in some external browser that has more abilities to display the inner HTML code (e.g., Firebug on FireFox). Then you’d be able to see what goes on behind the scenes and try to use this info in the more-limited Matlab browser. If you would like further assistance on a consulting basis, please contact me via email.

#15 Comment By Yair Altman On May 28, 2013 @ 5:39 pm

@Kobbi – try to [19] your HTML

#16 Comment By Brian On June 24, 2013 @ 3:03 am

This works great, thanks! Is there any way to add the browser menu bar to this panel (i.e. forward/back buttons?)

#17 Comment By Yair Altman On June 24, 2013 @ 11:52 pm

@Brian -

jObject = com.mathworks.mlwidgets.html.HTMLBrowserPanel;
jObject.addToolbar()
jObject.addStatusBar()
jObject.addAddressBox(false);  % false=in toolbar row; true=in separate row
[browser,container] = javacomponent(jObject, [], gcf);
set(container, 'Units','norm', 'Pos',[0.3,0.05,0.65,0.9]);

#18 Comment By Mike On July 15, 2013 @ 12:53 pm

Yair,
Do you know how to disable or modify the popup context menu that I get when using com.mathworks.mlwidgets.html.HTMLBrowserPanel ? I’m deploying a compiled app with an HTML viewer component, and I don’t want the users to be able to click “Evaluate selection” on selected text.
Thanks,
Mike

#19 Comment By Yair Altman On July 15, 2013 @ 1:16 pm

@Mike – this will require investigation. If you would like me to investigate this for you (for a consulting fee), send me an email.

#20 Comment By lichen On October 18, 2013 @ 5:18 am

Hi,
As writen above, we can browse web page in the matlab GUI. Now there is a video frequency embed in the web page which is realized by applet(java), how can i process every frame image such as adding a moving mechanical arm to realize fusion

#21 Comment By Yair Altman On October 18, 2013 @ 5:58 am

@Lichen – you need to download the relevant video into Matlab and process it in Matlab. You cannot directly interact with it on the webpage.

#22 Comment By clara sergian On November 12, 2013 @ 7:09 pm

hi yair.

is it possible to integrate GUI on a web browser?? could you please show me how to solve this?

#23 Comment By Yair Altman On November 13, 2013 @ 10:14 am

@Clara – There is no simple answer. You can have your webserver call Matlab (on the server-side) to generate HTML/images. If you need interactivity then consider getting the Matlab Production Server ($$$).

#24 Comment By Yair Altman On April 16, 2014 @ 11:06 am

Followup article: [20]

#25 Comment By Bryant On October 6, 2014 @ 2:51 pm

Can this widget be used in a deployed application via MATLAB Compiler? I tried compiling your sample code in MATLAB R2013b and got the following error:

Error using playground (line 10)
Java exception occurred:
java.lang.NoClassDefFoundError: com/teamdev/jxbrowser/BrowserType
	at com.mathworks.html.jxbrowser.BrowserHolder$Type.(BrowserHolder.java:13)
	at com.mathworks.html.jxbrowser.JxBrowserSettings.getBrowserTypeValues(JxBrowserSettings.java:45)
	at com.mathworks.html.jxbrowser.JxBrowserSettings.buildSupportedProperties(JxBrowserSettings.java:33)
	at com.mathworks.html.jxbrowser.JxBrowserSettings.(JxBrowserSettings.java:23)
	at com.mathworks.html.BrowserSettings.getAllSettingsInstances(BrowserSettings.java:59)
	at com.mathworks.html.BrowserSettings.(BrowserSettings.java:22)
	at com.mathworks.mlwidgets.html.HtmlComponentFactory.(HtmlComponentFactory.java:54)
	at com.mathworks.mlwidgets.html.HTMLBrowserPanel.(HTMLBrowserPanel.java:75)
	at com.mathworks.mlwidgets.html.HTMLBrowserPanel.(HTMLBrowserPanel.java:65)
Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.BrowserType
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 9 more

MATLAB:Java:GenericException

#26 Comment By Yair Altman On October 6, 2014 @ 3:03 pm

@Bryant – this article is an oldie, written in 2009. Since then (I believe in R2012a) Matlab changed its internal browser to the new JXBrowser. This means that you will need to modify the above code. I did it some months ago and I remember that it wasn’t too hard, but I can’t remember at the moment exactly what I did and I do not have the code (it’s at a client). So either you try to modify the code like I did back then, or contact me by email for a short consultancy to recreate what I did.


Article printed from Undocumented Matlab: http://undocumentedmatlab.com

URL to article: http://undocumentedmatlab.com/blog/gui-integrated-browser-control

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] described the built-in PopupPanel object: http://undocumentedmatlab.com/blog/customizing-help-popup-contents/

[4] Image: http://images.undocumentedmatlab.com/Browser.png

[5] Image: http://images.undocumentedmatlab.com/Browser_404.png

[6] a few days ago: http://explodingpixels.wordpress.com/2009/12/04/heading-to-apple/

[7] Matlab Desktop blog: http://blogs.mathworks.com/desktop/

[8] numerous recurring issues that afflict Matlab Mac releases: http://blogs.mathworks.com/desktop/2009/08/31/pouncing-on-snow-leopard/

[9] GUI integrated HTML panel: http://undocumentedmatlab.com/blog/gui-integrated-html-panel

[10] Inactive Control Tooltips & Event Chaining: http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining

[11] Matlab-Java interface using a static control: http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control

[12] Customizing Matlab labels: http://undocumentedmatlab.com/blog/customizing-matlab-labels

[13] Setting listbox mouse actions: http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions

[14] The javacomponent function: http://undocumentedmatlab.com/blog/javacomponent

[15] : http://www.domainscalping.com/

[16] : http://undocumentedmatlab.com/blog/continuous-slider-callback/

[17] : http://www.physionet.org/cgi-bin/atm/ATM

[18] : http://undocumentedmatlab.com/blog/uiinspect/

[19] : http://en.wikipedia.org/wiki/Minification_(programming)

[20] : http://undocumentedmatlab.com/blog/animated-busy-spinning-icon

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.