GUI integrated browser control

Last week, I described the built-in PopupPanel object, 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 = {'','','myLocalwebpage.html',...
        '', ''};
hListbox = uicontrol('style','listbox', 'string',urls, ...
        'units','norm', 'pos',[0.05,0.05,0.2,0.9], ...
% 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 object integrated in Matlab GUI (click for large image)

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 – 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)

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. You probably know Ken from his good work on the Desktop and the official Matlab Desktop blog. 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.

Categories: Figure window, GUI, Handle graphics, Java, Medium risk of breaking in future versions, UI controls

Tags: , , , , ,

Bookmark and SharePrint Print

44 Responses to GUI integrated browser control

  1. Ken Orr says:

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

  2. Gunnar Ingestrom says:

    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. Sid says:

    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.

  4. Arnaud says:

    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 :


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


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

    • @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...');
          % do some long operation...
    • Arnaud says:

      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.


  5. Yonatan says:

    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: (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 :)

  6. Pingback: Customizing menu items part 3 | Undocumented Matlab

  7. Ivan says:

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

    • @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 here.

  8. thnxtay says:

    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:

    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.

    1) Used uiinspect.m ( 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 = {'','','myLocalwebpage.html',...
            '', ''};
    hListbox = uicontrol('style','listbox', 'string',urls, ...
            'units','norm', 'pos',[0.05,0.05,0.2,0.9], ...
    % 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;'...
    % context menu
    cmenu = uicontextmenu;
    hcb1 = ['set(f, ''Color'', [0.9 0.6 0.5])'];
    item1 = uimenu(cmenu, 'Label', 'change color', 'Callback', hcb1);
    • @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.

  9. kobbi says:


    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?


  10. Brian says:

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

    • @Brian –

      jObject = com.mathworks.mlwidgets.html.HTMLBrowserPanel;
      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]);
  11. Mike says:

    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.

  12. lichen says:

    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

  13. clara sergian says:

    hi yair.

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

    • Yair Altman says:

      @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 ($$$).

  14. Bryant says:

    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.(
    	at com.mathworks.html.jxbrowser.JxBrowserSettings.getBrowserTypeValues(
    	at com.mathworks.html.jxbrowser.JxBrowserSettings.buildSupportedProperties(
    	at com.mathworks.html.jxbrowser.JxBrowserSettings.(
    	at com.mathworks.html.BrowserSettings.getAllSettingsInstances(
    	at com.mathworks.html.BrowserSettings.(
    	at com.mathworks.mlwidgets.html.HtmlComponentFactory.(
    	at com.mathworks.mlwidgets.html.HTMLBrowserPanel.(
    	at com.mathworks.mlwidgets.html.HTMLBrowserPanel.(
    Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.BrowserType
    	at Method)
    	at java.lang.ClassLoader.loadClass(
    	at sun.misc.Launcher$AppClassLoader.loadClass(
    	at java.lang.ClassLoader.loadClass(
    	... 9 more
    • @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.

    • Reza says:

      I have the same problem. this Java widget got error in compiling via Matlab 2018!
      Could you please show me how to solve this?

  15. Ragni says:

    Could you tell how to download a video file to matlab from web browser?

    • @Ragni – don’t do it directly in Matlab: download the video using external software, save it on your disk, and then load this file in Matlab.

    • Ragni says:

      Thanks for your answer.
      I have one more doubt. I have live video streaming running in Google Chrome browser. How can i import/read this live streaming to MATLAB such that I can read it using matlab function “inputvideo”.

  16. Leinylson says:


    I have this error:

    This page does not display correctly in your version of the MATLAB Web Browser.
    Please use the link below to open the page in your default system browser:

    I’m using the 2013 version. thanks.

  17. Clement Val says:

    I have a little trouble using this component in a Matlab class UI I have developed: although I delete the java objects in my destructor, “jxBrowser Chromium Native Process” remain in memory, until I close Matlab. Any idea how I can properly cleanup that?

    • @Clement – apparently you can dispose the browser object using its dispose() method. You can simply call it when the window closes. For example:

      set(f, 'CloseRequestFcn', @(h,e) dispose(jObject));  % f is the figure handle
  18. Nurdan says:

    Hi Yair,
    Thanks very much I love your website, it is so helpful.
    I have a question.When I tried to run the above code it gives an error of Undefined variable”com” or class ”com.matworks.mlwidget.html.HTMLBrowserPanel” ıf you help that would be great. Thanks!

    • @Nurden – of course Matlab gives this error if you mistype the class name (com.mathworks.mlwidgets.html.HTMLBrowserPanel) !!!
      Next time copy-paste from the blog, don’t try to memorize long class names!

    • Nurdan says:

      Hi Yair,
      Sorry for taking your time, I found that mistake pretty shortly after I asked the question here. Now it works and I cant thank you enough.
      One more question; instead of a listbox, I would like to put a tree on the left side inside a panel so whenever I click on an item in the tree it would take me to a specific url that belongs to that item that I picked. I can create a tree with the help of other sources on the internet, but how can I put that tree in a uipanel? Any help will be appreciated. Thanks again!

    • search this website for “tree”

  19. Nurdan says:

    Hello Yair,
    Lets assume there is a link to ‘’ on the RIGHT side in the browser in above example(GUI integrated browser control) and when I click on it , it will start showing cnn website. How can I move the selection highlight (blue) on to ‘’?
    Thanks very much!

  20. Diego Shalom says:

    Hi Yair,
    It works great after creating the figure using:

    f=figure('Name','Browser GUI demo','Units','norm');

    in Matlab 2015a. Several times I found useful codelets in this page, thanks!

    I have a question. If the page is longer than the container, I need to scroll through. Can I track this scroll position in the webpage? Is this information available in any variable while scrolling?
    Thanks you very much!

  21. H.A. Camp says:

    It’s not clear to me how to interact with HTML components from the resulting browser object. For example, if my HTML contains a simple form input box, how do I return the current string from the box? I imagine the procedure would be the same for a drop-down menu, or any other form component.

    Is there a way to get/set HTML form component properties?

Leave a Reply

Your email address will not be published. Required fields are marked *