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

GUI integrated HTML panel

Posted By Yair Altman On December 15, 2009 | 3 Comments

Last week, I explained how a browser control can be integrated in Matlab GUI applications [1]. Sometimes we only need to display simple HTML, for which a full browser seems like overkill. Moreover, we may wish to edit the displayed contents, which cannot be done using the browser control. The solution is to use a standard Java Swing JEditorPane [2] control, which is an editable HTML-aware control.
Oddly enough, it was only yesterday that Mikhail, a known Matlab Java specialist on the CSSM newsgroup [3], posted an example for this as answer to a question on the StackOverflow [4] forum (slightly edited for clarity):

mytext = ['' ...
          '' ...
          '' ...
          '
MonthSavings
January$100
']; % Create a figure with a scrollable JEditorPane hfig = figure(); je = javax.swing.JEditorPane('text/html', mytext); jp = javax.swing.JScrollPane(je); [hcomponent, hcontainer] = javacomponent(jp, [], hfig); set(hcontainer, 'units', 'normalized', 'position', [0,0,1,1]); % Turn anti-aliasing on (R2006a, Java 5.0) java.lang.System.setProperty('awt.useSystemAAFontSettings', 'on'); je.setFont(java.awt.Font('Arial', java.awt.Font.PLAIN, 13)); je.putClientProperty(javax.swing.JEditorPane.HONOR_DISPLAY_PROPERTIES, true); % This only works on Java 1.5 (Matlab R14SP2 to R2007a): je.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, true);

Editable HTML-aware JEditorPane
Editable HTML-aware JEditorPane

Mikhail’s code included setting SwingUtilities2’s AA_TEXT_PROPERTY_KEY property for anti-aliased fonts. Unfortunately, SwingUtilities2 was an unsupported and undocumented internal class in Java 1.5 [5] (undocumented/unsupported by Sun, not MathWorks for a change…) and completely disappeared in Java 1.6 (which is bundled with Matlab R2007b onward). Therefore, SwingUtilities2 and its antialias property can only be used on Matlab releases R14SP2 (7.0.4) through R2007a (7.4) – other Matlab versions will throw an error.
Alternately, use JIDE’s AA_TEXT_PROPERTY_KEY (JIDE is bundled with Matlab and this is supported even on new Matlab releases – I will present JIDE in future articles).

property = com.jidesoft.swing.JideSwingUtilities.AA_TEXT_PROPERTY_KEY;
je.putClientProperty(property, true);

Or, simply add the following switch to your java.opt file [6]:

-Dswing.aatext=true

With this switch, you no longer need to set anti-aliasing separately for each component. It is entirely harmless to set this switch even on Matlab/Java versions that do not support it (the switch is simply ignored in these cases).
Note that while JEditorPane’s support for HTML is extensive, it is incomplete. It also does not contain a JavaScript engine or other web-related features we have come to expect in a browser. For the more complex stuff we can use the browser control as explained in last week’s article.
Matlab’s own multi-line editbox uicontrol uses JEditorPane (or actually its derived-class JTextPane [7]) as an underlying component. This means that the simple-looking Matlab editbox is actually a powerful HTML-aware component. In order to use these hidden undocumented features we need the editbox’s underlying JTextPane handle. This is done using the FindJObj utility [8], which will be described in my next article. Following that, I will show how to customize Matlab’s dull-looking editbox [9] into something much more powerful. Here’s a sample, to help you stay tuned:

HTML contents in a regular Matlab editbox
HTML contents in a regular Matlab editbox

Categories: GUI, Java, Low risk of breaking in future versions, UI controls


3 Comments (Open | Close)

3 Comments To "GUI integrated HTML panel"

#1 Comment By Luigi Giaccari On December 29, 2009 @ 02:36

Yair,

I may fall in love with you for this post!!!!!!!!

😉

#2 Comment By Luigi Giaccari On December 29, 2009 @ 02:43

Correction to my previous comment,

I tried to insert a paypal button, but when I click on it a bell rings and nothing happens. NO warnings NO errors.

Do you have any idea?

#3 Comment By Arda On September 16, 2011 @ 03:12

The main problem i got with a JEditorPane was that it uses HTML 3.2 still. And most of the stuff you can achieve easily with HTML 4 is not even possible to do with HTML 3.2 …


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

URL to article: https://undocumentedmatlab.com/articles/gui-integrated-html-panel

URLs in this post:

[1] browser control can be integrated in Matlab GUI applications: http://undocumentedmatlab.com/blog/gui-integrated-browser-control/

[2] JEditorPane: http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html

[3] a known Matlab Java specialist on the CSSM newsgroup: https://www.mathworks.com/matlabcentral/newsreader/author/99871

[4] a question on the StackOverflow: http://stackoverflow.com/questions/1903516/matlab-displaying-markup-html-or-other-format/1903990#1903990

[5] SwingUtilities2 was an unsupported and undocumented internal class in Java 1.5: http://www.jroller.com/gfx/entry/be_ready_for_java_se

[6] java.opt file: http://www.mathworks.com/support/solutions/en/data/1-18I2C

[7] JTextPane: http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html

[8] the FindJObj utility: http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/

[9] customize Matlab’s dull-looking editbox: http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/

[10] GUI integrated browser control : https://undocumentedmatlab.com/articles/gui-integrated-browser-control

[11] Rich-contents log panel : https://undocumentedmatlab.com/articles/rich-contents-log-panel

[12] Panel-level uicontrols : https://undocumentedmatlab.com/articles/panel-level-uicontrols

[13] HTML support in Matlab uicomponents : https://undocumentedmatlab.com/articles/html-support-in-matlab-uicomponents

[14] Sending HTML emails from Matlab : https://undocumentedmatlab.com/articles/sending-html-emails-from-matlab

[15] GUI formatting using HTML : https://undocumentedmatlab.com/articles/gui-formatting-using-html

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