Last week, I explained how a browser control can be integrated in Matlab GUI applications. 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 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, posted an example for this as answer to a question on the StackOverflow forum (slightly edited for clarity):
mytext = ['<html><body><table border="1">' ... '<tr><th>Month</th><th>Savings</th></tr>' ... '<tr><td>January</td><td>$100</td></tr>' ... '</table></body></html>']; % 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); |
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 (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:
-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) 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, which will be described in my next article. Following that, I will show how to customize Matlab’s dull-looking editbox into something much more powerful. Here’s a sample, to help you stay tuned:
Yair,
I may fall in love with you for this post!!!!!!!!
😉
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?
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 …