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 = {'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)

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)

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

45 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.
    Thanks
    Sid

  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 :

    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

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

      Arnaud

  5. Yonatan says:

    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:
    http://www.domainscalping.com/ (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.
    Thanks.
    Ivan.

    • @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:

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

    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 (https://undocumentedmatlab.com/blog/uiinspect/) 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);
    • @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:

    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

  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.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]);
  11. Mike says:

    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

  12. lichen says:

    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

  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.(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
    
    • @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:

      Hello,
      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:

    Hello,

    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:
    http://www.mathworks.com/index.html
    

    I’m using the 2013 version. thanks.

  17. Clement Val says:

    Hello,
    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?
    Thanks!

    • @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 ‘www.cnn.com’ 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 ‘www.cnn.com’?
    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?

  22. Nicholas says:

    It has come to my attention that in R2022a the content for HtmlText and CurrentLocation cannot be set for an HTMLBrowserPanel object. This can be easily demonstrated in R2022a with the following example:

    hFig = figure('Units','norm','Pos', [0.3, 0.2, 0.4, 0.4]); %create the figure
    [browser, bContainer] = javacomponent(com.mathworks.mlwidgets.html.HTMLBrowserPanel, [], hFig); %create the browser and container objects
    set(bContainer, 'Units', 'norm', 'Position', [0.1 0.1 0.9 0.9]); %expand the container to fit the figure
    javaMethodEDT('setHtmlText', browser,javaObjectEDT('java.lang.String', 'Testing HTML Text')); %use setHtmlText with EDT

    If we retrieve the html text, we find it is empty, as though it were never set:
    >> browser.HtmlText

    ans =

    0×0 empty char array

    The same thing happens if we try to use setCurrentLocation to set to an html file – the CurrentLocation property is not set. I have tried every which way I can think of to set these (set methods, EDT, direct set etc.) and nothing will stick. Finally, I have tried this in 2021b and it works wonderfully! The 2022b prelease does not work.

    Does anyone have any solutions?

    Please note that this is relevant to legacy code that relies heavily on HTMLBrowserPanel, and, for the moment, cannot be resolved with the more modern html tools that are available.

    Thank you!

Leave a Reply

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